2 Provides interface to shell internal functions for shell commands.
4 Copyright (c) 2009 - 2014, 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 Frees list of file handles.
106 @param[in] List The list to free.
114 BUFFER_LIST
*BufferListEntry
;
120 // enumerate through the buffer list and free all memory
122 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
123 ; !IsListEmpty (&List
->Link
)
124 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
126 RemoveEntryList(&BufferListEntry
->Link
);
127 ASSERT(BufferListEntry
->Buffer
!= NULL
);
128 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)(BufferListEntry
->Buffer
))->Path
);
129 SHELL_FREE_NON_NULL(BufferListEntry
->Buffer
);
130 SHELL_FREE_NON_NULL(BufferListEntry
);
135 Destructor for the library. free any resources.
137 @param ImageHandle the image handle of the process
138 @param SystemTable the EFI System Table pointer
140 @retval RETURN_SUCCESS this function always returns success
144 ShellCommandLibDestructor (
145 IN EFI_HANDLE ImageHandle
,
146 IN EFI_SYSTEM_TABLE
*SystemTable
149 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
151 SCRIPT_FILE_LIST
*Node3
;
152 SHELL_MAP_LIST
*MapNode
;
154 // enumerate throught the list and free all the memory
156 while (!IsListEmpty (&mCommandList
.Link
)) {
157 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
158 RemoveEntryList(&Node
->Link
);
159 SHELL_FREE_NON_NULL(Node
->CommandString
);
161 DEBUG_CODE(Node
= NULL
;);
165 // enumerate through the alias list and free all memory
167 while (!IsListEmpty (&mAliasList
.Link
)) {
168 Node2
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
);
169 RemoveEntryList(&Node2
->Link
);
170 SHELL_FREE_NON_NULL(Node2
->CommandString
);
171 SHELL_FREE_NON_NULL(Node2
->Alias
);
172 SHELL_FREE_NON_NULL(Node2
);
173 DEBUG_CODE(Node2
= NULL
;);
177 // enumerate throught the list and free all the memory
179 while (!IsListEmpty (&mScriptList
.Link
)) {
180 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
181 RemoveEntryList(&Node3
->Link
);
182 DeleteScriptFileStruct(Node3
->Data
);
187 // enumerate throught the mappings list and free all the memory
189 if (!IsListEmpty(&gShellMapList
.Link
)) {
190 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
191 ; !IsListEmpty (&gShellMapList
.Link
)
192 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
194 ASSERT(MapNode
!= NULL
);
195 RemoveEntryList(&MapNode
->Link
);
196 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
197 SHELL_FREE_NON_NULL(MapNode
->MapName
);
198 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
202 if (!IsListEmpty(&mFileHandleList
.Link
)){
203 FreeFileHandleList(&mFileHandleList
);
206 if (mProfileList
!= NULL
) {
207 FreePool(mProfileList
);
210 gUnicodeCollation
= NULL
;
213 return (RETURN_SUCCESS
);
217 Checks if a command is already on the list.
219 @param[in] CommandString The command string to check for on the list.
223 ShellCommandIsCommandOnList (
224 IN CONST CHAR16
*CommandString
227 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
230 // assert for NULL parameter
232 ASSERT(CommandString
!= NULL
);
235 // check for the command
237 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
238 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
239 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
241 ASSERT(Node
->CommandString
!= NULL
);
242 if (gUnicodeCollation
->StriColl(
244 (CHAR16
*)CommandString
,
245 Node
->CommandString
) == 0
254 Get the help text for a command.
256 @param[in] CommandString The command name.
258 @retval NULL No help text was found.
259 @return String of help text. Caller reuiqred to free.
263 ShellCommandGetCommandHelp (
264 IN CONST CHAR16
*CommandString
267 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
270 // assert for NULL parameter
272 ASSERT(CommandString
!= NULL
);
275 // check for the command
277 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
278 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
279 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
281 ASSERT(Node
->CommandString
!= NULL
);
282 if (gUnicodeCollation
->StriColl(
284 (CHAR16
*)CommandString
,
285 Node
->CommandString
) == 0
287 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
294 Registers handlers of type SHELL_RUN_COMMAND and
295 SHELL_GET_MAN_FILENAME for each shell command.
297 If the ShellSupportLevel is greater than the value of the
298 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
300 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
301 with the command specified by CommandString. If the command named by
302 CommandString has already been registered, then return
303 RETURN_ALREADY_STARTED.
305 If there are not enough resources available to register the handlers then
306 RETURN_OUT_OF_RESOURCES is returned.
308 If CommandString is NULL, then ASSERT().
309 If GetHelpInfoHandler is NULL, then ASSERT().
310 If CommandHandler is NULL, then ASSERT().
311 If ProfileName is NULL, then ASSERT().
313 @param[in] CommandString Pointer to the command name. This is the
314 name to look for on the command line in
316 @param[in] CommandHandler Pointer to a function that runs the
318 @param[in] GetManFileName Pointer to a function that provides man
320 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
322 @param[in] ProfileName profile name to require for support of this
324 @param[in] CanAffectLE indicates whether this command's return value
325 can change the LASTERROR environment variable.
326 @param[in] HiiHandle Handle of this command's HII entry.
327 @param[in] ManFormatHelp HII locator for the help text.
329 @retval RETURN_SUCCESS The handlers were registered.
330 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
331 register the shell command.
332 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
333 currently allowed support level.
334 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
335 is already registered. Only 1 handler set for
336 a given command is allowed.
337 @sa SHELL_GET_MAN_FILENAME
338 @sa SHELL_RUN_COMMAND
342 ShellCommandRegisterCommandName (
343 IN CONST CHAR16
*CommandString
,
344 IN SHELL_RUN_COMMAND CommandHandler
,
345 IN SHELL_GET_MAN_FILENAME GetManFileName
,
346 IN UINT32 ShellMinSupportLevel
,
347 IN CONST CHAR16
*ProfileName
,
348 IN CONST BOOLEAN CanAffectLE
,
349 IN CONST EFI_HANDLE HiiHandle
,
350 IN CONST EFI_STRING_ID ManFormatHelp
353 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
354 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Command
;
355 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*PrevCommand
;
356 INTN LexicalMatchValue
;
359 // Initialize local variables.
363 LexicalMatchValue
= 0;
366 // ASSERTs for NULL parameters
368 ASSERT(CommandString
!= NULL
);
369 ASSERT(GetManFileName
!= NULL
);
370 ASSERT(CommandHandler
!= NULL
);
371 ASSERT(ProfileName
!= NULL
);
374 // check for shell support level
376 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
377 return (RETURN_UNSUPPORTED
);
381 // check for already on the list
383 if (ShellCommandIsCommandOnList(CommandString
)) {
384 return (RETURN_ALREADY_STARTED
);
388 // allocate memory for new struct
390 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
391 ASSERT(Node
!= NULL
);
392 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
393 ASSERT(Node
->CommandString
!= NULL
);
396 // populate the new struct
398 StrCpy(Node
->CommandString
, CommandString
);
400 Node
->GetManFileName
= GetManFileName
;
401 Node
->CommandHandler
= CommandHandler
;
402 Node
->LastError
= CanAffectLE
;
403 Node
->HiiHandle
= HiiHandle
;
404 Node
->ManFormatHelp
= ManFormatHelp
;
406 if ( StrLen(ProfileName
)>0
407 && ((mProfileList
!= NULL
408 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
410 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
411 if (mProfileList
== NULL
) {
413 // If this is the first make a leading ';'
415 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
417 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
418 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
422 // Insert a new entry on top of the list
424 InsertHeadList (&mCommandList
.Link
, &Node
->Link
);
427 // Move a new registered command to its sorted ordered location in the list
429 for (Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
),
430 PrevCommand
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
)
431 ; !IsNull (&mCommandList
.Link
, &Command
->Link
)
432 ; Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode (&mCommandList
.Link
, &Command
->Link
)) {
435 // Get Lexical Comparison Value between PrevCommand and Command list entry
437 LexicalMatchValue
= gUnicodeCollation
->StriColl (
439 PrevCommand
->CommandString
,
440 Command
->CommandString
444 // Swap PrevCommand and Command list entry if PrevCommand list entry
445 // is alphabetically greater than Command list entry
447 if (LexicalMatchValue
> 0){
448 Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*) SwapListEntries (&PrevCommand
->Link
, &Command
->Link
);
449 } else if (LexicalMatchValue
< 0) {
451 // PrevCommand entry is lexically lower than Command entry
457 return (RETURN_SUCCESS
);
461 Function to get the current Profile string.
463 @retval NULL There are no installed profiles.
464 @return A semi-colon delimited list of profiles.
468 ShellCommandGetProfileList (
472 return (mProfileList
);
476 Checks if a command string has been registered for CommandString and if so it runs
477 the previously registered handler for that command with the command line.
479 If CommandString is NULL, then ASSERT().
481 If Sections is specified, then each section name listed will be compared in a casesensitive
482 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
483 it will be appended to the returned help text. If the section does not exist, no
484 information will be returned. If Sections is NULL, then all help text information
485 available will be returned.
487 @param[in] CommandString Pointer to the command name. This is the name
488 found on the command line in the shell.
489 @param[in, out] RetVal Pointer to the return vaule from the command handler.
491 @param[in, out] CanAffectLE indicates whether this command's return value
492 needs to be placed into LASTERROR environment variable.
494 @retval RETURN_SUCCESS The handler was run.
495 @retval RETURN_NOT_FOUND The CommandString did not match a registered
497 @sa SHELL_RUN_COMMAND
501 ShellCommandRunCommandHandler (
502 IN CONST CHAR16
*CommandString
,
503 IN OUT SHELL_STATUS
*RetVal
,
504 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
507 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
510 // assert for NULL parameters
512 ASSERT(CommandString
!= NULL
);
515 // check for the command
517 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
518 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
519 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
521 ASSERT(Node
->CommandString
!= NULL
);
522 if (gUnicodeCollation
->StriColl(
524 (CHAR16
*)CommandString
,
525 Node
->CommandString
) == 0
527 if (CanAffectLE
!= NULL
) {
528 *CanAffectLE
= Node
->LastError
;
530 if (RetVal
!= NULL
) {
531 *RetVal
= Node
->CommandHandler(NULL
, gST
);
533 Node
->CommandHandler(NULL
, gST
);
535 return (RETURN_SUCCESS
);
538 return (RETURN_NOT_FOUND
);
542 Checks if a command string has been registered for CommandString and if so it
543 returns the MAN filename specified for that command.
545 If CommandString is NULL, then ASSERT().
547 @param[in] CommandString Pointer to the command name. This is the name
548 found on the command line in the shell.\
550 @retval NULL the commandString was not a registered command.
551 @return other the name of the MAN file.
552 @sa SHELL_GET_MAN_FILENAME
556 ShellCommandGetManFileNameHandler (
557 IN CONST CHAR16
*CommandString
560 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
563 // assert for NULL parameters
565 ASSERT(CommandString
!= NULL
);
568 // check for the command
570 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
571 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
572 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
574 ASSERT(Node
->CommandString
!= NULL
);
575 if (gUnicodeCollation
->StriColl(
577 (CHAR16
*)CommandString
,
578 Node
->CommandString
) == 0
580 return (Node
->GetManFileName());
587 Get the list of all available shell internal commands. This is a linked list
588 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
589 list functions. do not modify the values.
591 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
593 @return a Linked list of all available shell commands.
597 ShellCommandGetCommandList (
598 IN CONST BOOLEAN Sort
602 // return ((COMMAND_LIST*)(&mCommandList));
604 return ((COMMAND_LIST
*)(&mCommandList
));
608 Registers aliases to be set as part of the initialization of the shell application.
610 If Command is NULL, then ASSERT().
611 If Alias is NULL, then ASSERT().
613 @param[in] Command Pointer to the Command
614 @param[in] Alias Pointer to Alias
616 @retval RETURN_SUCCESS The handlers were registered.
617 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
618 register the shell command.
622 ShellCommandRegisterAlias (
623 IN CONST CHAR16
*Command
,
624 IN CONST CHAR16
*Alias
632 ASSERT(Command
!= NULL
);
633 ASSERT(Alias
!= NULL
);
636 // allocate memory for new struct
638 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
639 ASSERT(Node
!= NULL
);
640 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
641 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
642 ASSERT(Node
->CommandString
!= NULL
);
643 ASSERT(Node
->Alias
!= NULL
);
646 // populate the new struct
648 StrCpy(Node
->CommandString
, Command
);
649 StrCpy(Node
->Alias
, Alias
);
652 // add the new struct to the list
654 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
656 return (RETURN_SUCCESS
);
660 Get the list of all shell alias commands. This is a linked list
661 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
662 list functions. do not modify the values.
664 @return a Linked list of all requested shell alias'.
668 ShellCommandGetInitAliasList (
672 return (&mAliasList
);
676 Determine if a given alias is on the list of built in alias'.
678 @param[in] Alias The alias to test for
680 @retval TRUE The alias is a built in alias
681 @retval FALSE The alias is not a built in alias
685 ShellCommandIsOnAliasList(
686 IN CONST CHAR16
*Alias
692 // assert for NULL parameter
694 ASSERT(Alias
!= NULL
);
697 // check for the Alias
699 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
700 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
701 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
703 ASSERT(Node
->CommandString
!= NULL
);
704 ASSERT(Node
->Alias
!= NULL
);
705 if (gUnicodeCollation
->StriColl(
708 Node
->CommandString
) == 0
712 if (gUnicodeCollation
->StriColl(
724 Function to determine current state of ECHO. Echo determins if lines from scripts
725 and ECHO commands are enabled.
727 @retval TRUE Echo is currently enabled
728 @retval FALSE Echo is currently disabled
732 ShellCommandGetEchoState(
740 Function to set current state of ECHO. Echo determins if lines from scripts
741 and ECHO commands are enabled.
743 If State is TRUE, Echo will be enabled.
744 If State is FALSE, Echo will be disabled.
746 @param[in] State How to set echo.
750 ShellCommandSetEchoState(
758 Indicate that the current shell or script should exit.
760 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
761 @param[in] ErrorCode The 64 bit error code to return.
765 ShellCommandRegisterExit (
766 IN BOOLEAN ScriptOnly
,
767 IN CONST UINT64 ErrorCode
770 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
771 if (mExitRequested
) {
772 mExitScript
= ScriptOnly
;
776 mExitCode
= ErrorCode
;
780 Retrieve the Exit indicator.
782 @retval TRUE Exit was indicated.
783 @retval FALSE Exis was not indicated.
787 ShellCommandGetExit (
791 return (mExitRequested
);
795 Retrieve the Exit code.
797 If ShellCommandGetExit returns FALSE than the return from this is undefined.
799 @return the value passed into RegisterExit.
803 ShellCommandGetExitCode (
810 Retrieve the Exit script indicator.
812 If ShellCommandGetExit returns FALSE than the return from this is undefined.
814 @retval TRUE ScriptOnly was indicated.
815 @retval FALSE ScriptOnly was not indicated.
819 ShellCommandGetScriptExit (
823 return (mExitScript
);
827 Function to cleanup all memory from a SCRIPT_FILE structure.
829 @param[in] Script The pointer to the structure to cleanup.
833 DeleteScriptFileStruct (
834 IN SCRIPT_FILE
*Script
839 if (Script
== NULL
) {
843 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
844 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
846 if (Script
->Argv
!= NULL
) {
847 SHELL_FREE_NON_NULL(Script
->Argv
);
849 Script
->CurrentCommand
= NULL
;
850 while (!IsListEmpty (&Script
->CommandList
)) {
851 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
852 if (Script
->CurrentCommand
!= NULL
) {
853 RemoveEntryList(&Script
->CurrentCommand
->Link
);
854 if (Script
->CurrentCommand
->Cl
!= NULL
) {
855 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
857 if (Script
->CurrentCommand
->Data
!= NULL
) {
858 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
860 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
863 SHELL_FREE_NON_NULL(Script
->ScriptName
);
864 SHELL_FREE_NON_NULL(Script
);
868 Function to return a pointer to the currently running script file object.
870 @retval NULL A script file is not currently running.
871 @return A pointer to the current script file object.
875 ShellCommandGetCurrentScriptFile (
879 SCRIPT_FILE_LIST
*List
;
880 if (IsListEmpty (&mScriptList
.Link
)) {
883 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
888 Function to set a new script as the currently running one.
890 This function will correctly stack and unstack nested scripts.
892 @param[in] Script Pointer to new script information structure. if NULL
893 will remove and de-allocate the top-most Script structure.
895 @return A pointer to the current running script file after this
896 change. NULL if removing the final script.
900 ShellCommandSetNewScript (
901 IN SCRIPT_FILE
*Script OPTIONAL
904 SCRIPT_FILE_LIST
*Node
;
905 if (Script
== NULL
) {
906 if (IsListEmpty (&mScriptList
.Link
)) {
909 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
910 RemoveEntryList(&Node
->Link
);
911 DeleteScriptFileStruct(Node
->Data
);
914 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
919 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
921 return (ShellCommandGetCurrentScriptFile());
925 Function to generate the next default mapping name.
927 If the return value is not NULL then it must be callee freed.
929 @param Type What kind of mapping name to make.
931 @retval NULL a memory allocation failed.
932 @return a new map name string
936 ShellCommandCreateNewMappingName(
937 IN CONST SHELL_MAPPING_TYPE Type
941 ASSERT(Type
< MappingTypeMax
);
945 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
948 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
949 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
950 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
956 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
958 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
959 default paths on the new map name...
961 Path should be FALSE when this function is called from the protocol SetMap function.
963 @param[in] Name The human readable mapped name.
964 @param[in] DevicePath The Device Path for this map.
965 @param[in] Flags The Flags attribute for this map item.
966 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
968 @retval EFI_SUCCESS The addition was sucessful.
969 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
970 @retval EFI_INVALID_PARAMETER A parameter was invalid.
974 ShellCommandAddMapItemAndUpdatePath(
975 IN CONST CHAR16
*Name
,
976 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
977 IN CONST UINT64 Flags
,
978 IN CONST BOOLEAN Path
982 SHELL_MAP_LIST
*MapListNode
;
983 CONST CHAR16
*OriginalPath
;
990 Status
= EFI_SUCCESS
;
992 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
993 if (MapListNode
== NULL
) {
994 Status
= EFI_OUT_OF_RESOURCES
;
996 MapListNode
->Flags
= Flags
;
997 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
998 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
999 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
1000 Status
= EFI_OUT_OF_RESOURCES
;
1002 StrCpy(MapListNode
->MapName
, Name
);
1003 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
1006 if (EFI_ERROR(Status
)) {
1007 if (MapListNode
!= NULL
) {
1008 if (MapListNode
->DevicePath
!= NULL
) {
1009 FreePool(MapListNode
->DevicePath
);
1011 if (MapListNode
->MapName
!= NULL
) {
1012 FreePool(MapListNode
->MapName
);
1014 FreePool(MapListNode
);
1018 // Since there was no error and Path was TRUE
1019 // Now add the correct path for that mapping
1021 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
1022 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
1023 if (OriginalPath
!= NULL
) {
1024 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
1026 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
1028 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
1029 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1030 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
1031 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1032 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
1033 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1034 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
1036 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
1037 ASSERT_EFI_ERROR(Status
);
1044 Creates the default map names for each device path in the system with
1045 a protocol depending on the Type.
1047 Creates the consistent map names for each device path in the system with
1048 a protocol depending on the Type.
1050 Note: This will reset all mappings in the system("map -r").
1052 Also sets up the default path environment variable if Type is FileSystem.
1054 @retval EFI_SUCCESS All map names were created sucessfully.
1055 @retval EFI_NOT_FOUND No protocols were found in the system.
1056 @return Error returned from gBS->LocateHandle().
1062 ShellCommandCreateInitialMappingsAndPaths(
1067 EFI_HANDLE
*HandleList
;
1069 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1070 CHAR16
*NewDefaultName
;
1071 CHAR16
*NewConsistName
;
1072 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1073 SHELL_MAP_LIST
*MapListNode
;
1078 // Reset the static members back to zero
1083 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1086 // First empty out the existing list.
1088 if (!IsListEmpty(&gShellMapList
.Link
)) {
1089 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1090 ; !IsListEmpty(&gShellMapList
.Link
)
1091 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1093 RemoveEntryList(&MapListNode
->Link
);
1094 FreePool(MapListNode
);
1099 // Find each handle with Simple File System
1101 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1102 if (HandleList
!= NULL
) {
1104 // Do a count of the handles
1106 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1109 // Get all Device Paths
1111 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1112 ASSERT(DevicePathList
!= NULL
);
1114 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1115 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1119 // Sort all DevicePaths
1121 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1123 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1125 // Assign new Mappings to all...
1127 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1129 // Get default name first
1131 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1132 ASSERT(NewDefaultName
!= NULL
);
1133 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1134 ASSERT_EFI_ERROR(Status
);
1135 FreePool(NewDefaultName
);
1138 // Now do consistent name
1140 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1141 if (NewConsistName
!= NULL
) {
1142 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1143 ASSERT_EFI_ERROR(Status
);
1144 FreePool(NewConsistName
);
1148 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1150 SHELL_FREE_NON_NULL(HandleList
);
1151 SHELL_FREE_NON_NULL(DevicePathList
);
1159 // Find each handle with Block Io
1161 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1162 if (HandleList
!= NULL
) {
1163 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1166 // Get all Device Paths
1168 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1169 ASSERT(DevicePathList
!= NULL
);
1171 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1172 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1176 // Sort all DevicePaths
1178 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1181 // Assign new Mappings to all...
1183 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1185 // Get default name first
1187 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1188 ASSERT(NewDefaultName
!= NULL
);
1189 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1190 ASSERT_EFI_ERROR(Status
);
1191 FreePool(NewDefaultName
);
1194 SHELL_FREE_NON_NULL(HandleList
);
1195 SHELL_FREE_NON_NULL(DevicePathList
);
1196 } else if (Count
== (UINTN
)-1) {
1197 return (EFI_NOT_FOUND
);
1200 return (EFI_SUCCESS
);
1204 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1206 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1208 @return a EFI_FILE_PROTOCOL* representing the same file.
1212 ConvertShellHandleToEfiFileProtocol(
1213 IN CONST SHELL_FILE_HANDLE Handle
1216 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1220 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1222 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1223 @param[in] Path The path to the file for verification.
1225 @return A SHELL_FILE_HANDLE representing the same file.
1226 @retval NULL There was not enough memory.
1230 ConvertEfiFileProtocolToShellHandle(
1231 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1232 IN CONST CHAR16
*Path
1235 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1236 BUFFER_LIST
*NewNode
;
1239 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1240 if (Buffer
== NULL
) {
1243 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1244 if (NewNode
== NULL
) {
1247 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1248 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1249 if (Buffer
->Path
== NULL
) {
1252 NewNode
->Buffer
= Buffer
;
1254 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1256 return ((SHELL_FILE_HANDLE
)(Handle
));
1260 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1262 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1264 @return A pointer to the path for the file.
1268 ShellFileHandleGetPath(
1269 IN CONST SHELL_FILE_HANDLE Handle
1274 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1275 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1276 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1278 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1279 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1286 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1288 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1290 @retval TRUE The item was removed.
1291 @retval FALSE The item was not found.
1295 ShellFileHandleRemove(
1296 IN CONST SHELL_FILE_HANDLE Handle
1301 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1302 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1303 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1305 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1306 RemoveEntryList(&Node
->Link
);
1307 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1308 SHELL_FREE_NON_NULL(Node
->Buffer
);
1309 SHELL_FREE_NON_NULL(Node
);
1317 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1319 This will NOT work on directories.
1321 If Handle is NULL, then ASSERT.
1323 @param[in] Handle the file handle
1325 @retval TRUE the position is at the end of the file
1326 @retval FALSE the position is not at the end of the file
1331 IN SHELL_FILE_HANDLE Handle
1334 EFI_FILE_INFO
*Info
;
1339 // ASSERT if Handle is NULL
1341 ASSERT(Handle
!= NULL
);
1343 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1344 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1345 ASSERT(Info
!= NULL
);
1346 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1352 if (Pos
== Info
->FileSize
) {
1364 Frees any BUFFER_LIST defined type.
1366 @param[in] List The BUFFER_LIST object to free.
1371 IN BUFFER_LIST
*List
1374 BUFFER_LIST
*BufferListEntry
;
1380 // enumerate through the buffer list and free all memory
1382 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1383 ; !IsListEmpty (&List
->Link
)
1384 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1386 RemoveEntryList(&BufferListEntry
->Link
);
1387 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1388 if (BufferListEntry
->Buffer
!= NULL
) {
1389 FreePool(BufferListEntry
->Buffer
);
1391 FreePool(BufferListEntry
);