2 Provides interface to shell internal functions for shell commands.
4 Copyright (c) 2009 - 2011, 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 /// The tag for use in identifying UNICODE files.
18 /// If the file is UNICODE, the first 16 bits of the file will equal this value.
20 gUnicodeFileTag
= 0xFEFF
23 // STATIC local variables
24 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList
;
25 STATIC SCRIPT_FILE_LIST mScriptList
;
26 STATIC ALIAS_LIST mAliasList
;
27 STATIC BOOLEAN mEchoState
;
28 STATIC BOOLEAN mExitRequested
;
29 STATIC UINT64 mExitCode
;
30 STATIC BOOLEAN mExitScript
;
31 STATIC CHAR16
*mProfileList
;
32 STATIC UINTN mProfileListSize
;
33 STATIC UINTN mFsMaxCount
= 0;
34 STATIC UINTN mBlkMaxCount
= 0;
35 STATIC BUFFER_LIST mFileHandleList
;
37 // global variables required by library class.
38 EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
= NULL
;
39 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*gDevPathToText
= NULL
;
40 SHELL_MAP_LIST gShellMapList
;
41 SHELL_MAP_LIST
*gShellCurDir
= NULL
;
43 CONST CHAR16
* SupportLevel
[] = {
51 Function to make sure that the global protocol pointers are valid.
52 must be called after constructor before accessing the pointers.
61 if (gUnicodeCollation
== NULL
) {
62 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
63 if (EFI_ERROR(Status
)) {
64 return (EFI_DEVICE_ERROR
);
67 if (gDevPathToText
== NULL
) {
68 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&gDevPathToText
);
69 if (EFI_ERROR(Status
)) {
70 return (EFI_DEVICE_ERROR
);
77 Constructor for the Shell Command library.
79 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
81 @param ImageHandle the image handle of the process
82 @param SystemTable the EFI System Table pointer
84 @retval EFI_SUCCESS the initialization was complete sucessfully
88 ShellCommandLibConstructor (
89 IN EFI_HANDLE ImageHandle
,
90 IN EFI_SYSTEM_TABLE
*SystemTable
94 InitializeListHead(&gShellMapList
.Link
);
95 InitializeListHead(&mCommandList
.Link
);
96 InitializeListHead(&mAliasList
.Link
);
97 InitializeListHead(&mScriptList
.Link
);
98 InitializeListHead(&mFileHandleList
.Link
);
101 mExitRequested
= FALSE
;
103 mProfileListSize
= 0;
106 if (gUnicodeCollation
== NULL
) {
107 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
108 if (EFI_ERROR(Status
)) {
109 return (EFI_DEVICE_ERROR
);
113 return (RETURN_SUCCESS
);
117 Destructor for the library. free any resources.
119 @param ImageHandle the image handle of the process
120 @param SystemTable the EFI System Table pointer
122 @retval RETURN_SUCCESS this function always returns success
126 ShellCommandLibDestructor (
127 IN EFI_HANDLE ImageHandle
,
128 IN EFI_SYSTEM_TABLE
*SystemTable
131 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
133 SCRIPT_FILE_LIST
*Node3
;
134 SHELL_MAP_LIST
*MapNode
;
136 // enumerate throught the list and free all the memory
138 while (!IsListEmpty (&mCommandList
.Link
)) {
139 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
140 RemoveEntryList(&Node
->Link
);
141 SHELL_FREE_NON_NULL(Node
->CommandString
);
143 DEBUG_CODE(Node
= NULL
;);
147 // enumerate through the init command list and free all memory
149 while (!IsListEmpty (&mAliasList
.Link
)) {
150 Node2
= (COMMAND_LIST
*)GetFirstNode(&mAliasList
.Link
);
151 RemoveEntryList(&Node2
->Link
);
152 SHELL_FREE_NON_NULL(Node2
->CommandString
);
154 DEBUG_CODE(Node2
= NULL
;);
158 // enumerate throught the list and free all the memory
160 while (!IsListEmpty (&mScriptList
.Link
)) {
161 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
162 RemoveEntryList(&Node3
->Link
);
163 DeleteScriptFileStruct(Node3
->Data
);
168 // enumerate throught the mappings list and free all the memory
170 if (!IsListEmpty(&gShellMapList
.Link
)) {
171 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
172 ; !IsListEmpty (&gShellMapList
.Link
)
173 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
175 ASSERT(MapNode
!= NULL
);
176 RemoveEntryList(&MapNode
->Link
);
177 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
178 SHELL_FREE_NON_NULL(MapNode
->MapName
);
179 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
183 if (!IsListEmpty(&mFileHandleList
.Link
)){
184 FreeBufferList(&mFileHandleList
);
187 if (mProfileList
!= NULL
) {
188 FreePool(mProfileList
);
191 gUnicodeCollation
= NULL
;
192 gDevPathToText
= NULL
;
195 return (RETURN_SUCCESS
);
199 Checks if a command is already on the list.
201 @param[in] CommandString The command string to check for on the list.
205 ShellCommandIsCommandOnList (
206 IN CONST CHAR16
*CommandString
209 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
212 // assert for NULL parameter
214 ASSERT(CommandString
!= NULL
);
217 // check for the command
219 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
220 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
221 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
223 ASSERT(Node
->CommandString
!= NULL
);
224 if (gUnicodeCollation
->StriColl(
226 (CHAR16
*)CommandString
,
227 Node
->CommandString
) == 0
236 Get the help text for a command.
238 @param[in] CommandString The command name.
240 @retval NULL No help text was found.
241 @return String of help text. Caller reuiqred to free.
245 ShellCommandGetCommandHelp (
246 IN CONST CHAR16
*CommandString
249 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
252 // assert for NULL parameter
254 ASSERT(CommandString
!= NULL
);
257 // check for the command
259 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
260 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
261 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
263 ASSERT(Node
->CommandString
!= NULL
);
264 if (gUnicodeCollation
->StriColl(
266 (CHAR16
*)CommandString
,
267 Node
->CommandString
) == 0
269 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
276 Registers handlers of type SHELL_RUN_COMMAND and
277 SHELL_GET_MAN_FILENAME for each shell command.
279 If the ShellSupportLevel is greater than the value of the
280 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
282 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
283 with the command specified by CommandString. If the command named by
284 CommandString has already been registered, then return
285 RETURN_ALREADY_STARTED.
287 If there are not enough resources available to register the handlers then
288 RETURN_OUT_OF_RESOURCES is returned.
290 If CommandString is NULL, then ASSERT().
291 If GetHelpInfoHandler is NULL, then ASSERT().
292 If CommandHandler is NULL, then ASSERT().
293 If ProfileName is NULL, then ASSERT().
295 @param[in] CommandString Pointer to the command name. This is the
296 name to look for on the command line in
298 @param[in] CommandHandler Pointer to a function that runs the
300 @param[in] GetManFileName Pointer to a function that provides man
302 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
304 @param[in] ProfileName profile name to require for support of this
306 @param[in] CanAffectLE indicates whether this command's return value
307 can change the LASTERROR environment variable.
308 @param[in] HiiHandle Handle of this command's HII entry.
309 @param[in] ManFormatHelp HII locator for the help text.
311 @retval RETURN_SUCCESS The handlers were registered.
312 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
313 register the shell command.
314 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
315 currently allowed support level.
316 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
317 is already registered. Only 1 handler set for
318 a given command is allowed.
319 @sa SHELL_GET_MAN_FILENAME
320 @sa SHELL_RUN_COMMAND
324 ShellCommandRegisterCommandName (
325 IN CONST CHAR16
*CommandString
,
326 IN SHELL_RUN_COMMAND CommandHandler
,
327 IN SHELL_GET_MAN_FILENAME GetManFileName
,
328 IN UINT32 ShellMinSupportLevel
,
329 IN CONST CHAR16
*ProfileName
,
330 IN CONST BOOLEAN CanAffectLE
,
331 IN CONST EFI_HANDLE HiiHandle
,
332 IN CONST EFI_STRING_ID ManFormatHelp
335 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
338 // ASSERTs for NULL parameters
340 ASSERT(CommandString
!= NULL
);
341 ASSERT(GetManFileName
!= NULL
);
342 ASSERT(CommandHandler
!= NULL
);
343 ASSERT(ProfileName
!= NULL
);
346 // check for shell support level
348 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
349 return (RETURN_UNSUPPORTED
);
353 // check for already on the list
355 if (ShellCommandIsCommandOnList(CommandString
)) {
356 return (RETURN_ALREADY_STARTED
);
360 // allocate memory for new struct
362 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
363 ASSERT(Node
!= NULL
);
364 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
365 ASSERT(Node
->CommandString
!= NULL
);
368 // populate the new struct
370 StrCpy(Node
->CommandString
, CommandString
);
372 Node
->GetManFileName
= GetManFileName
;
373 Node
->CommandHandler
= CommandHandler
;
374 Node
->LastError
= CanAffectLE
;
375 Node
->HiiHandle
= HiiHandle
;
376 Node
->ManFormatHelp
= ManFormatHelp
;
378 if ( StrLen(ProfileName
)>0
379 && ((mProfileList
!= NULL
380 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
382 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
383 if (mProfileList
== NULL
) {
385 // If this is the first make a leading ';'
387 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
389 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
390 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
394 // add the new struct to the list
396 InsertTailList (&mCommandList
.Link
, &Node
->Link
);
398 return (RETURN_SUCCESS
);
402 Function to get the current Profile string.
404 @retval NULL There are no installed profiles.
405 @return A semi-colon delimited list of profiles.
409 ShellCommandGetProfileList (
413 return (mProfileList
);
417 Checks if a command string has been registered for CommandString and if so it runs
418 the previously registered handler for that command with the command line.
420 If CommandString is NULL, then ASSERT().
422 If Sections is specified, then each section name listed will be compared in a casesensitive
423 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
424 it will be appended to the returned help text. If the section does not exist, no
425 information will be returned. If Sections is NULL, then all help text information
426 available will be returned.
428 @param[in] CommandString Pointer to the command name. This is the name
429 found on the command line in the shell.
430 @param[in, out] RetVal Pointer to the return vaule from the command handler.
432 @param[in, out] CanAffectLE indicates whether this command's return value
433 needs to be placed into LASTERROR environment variable.
435 @retval RETURN_SUCCESS The handler was run.
436 @retval RETURN_NOT_FOUND The CommandString did not match a registered
438 @sa SHELL_RUN_COMMAND
442 ShellCommandRunCommandHandler (
443 IN CONST CHAR16
*CommandString
,
444 IN OUT SHELL_STATUS
*RetVal
,
445 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
448 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
451 // assert for NULL parameters
453 ASSERT(CommandString
!= NULL
);
456 // check for the command
458 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
459 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
460 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
462 ASSERT(Node
->CommandString
!= NULL
);
463 if (gUnicodeCollation
->StriColl(
465 (CHAR16
*)CommandString
,
466 Node
->CommandString
) == 0
468 if (CanAffectLE
!= NULL
) {
469 *CanAffectLE
= Node
->LastError
;
471 if (RetVal
!= NULL
) {
472 *RetVal
= Node
->CommandHandler(NULL
, gST
);
474 Node
->CommandHandler(NULL
, gST
);
476 return (RETURN_SUCCESS
);
479 return (RETURN_NOT_FOUND
);
483 Checks if a command string has been registered for CommandString and if so it
484 returns the MAN filename specified for that command.
486 If CommandString is NULL, then ASSERT().
488 @param[in] CommandString Pointer to the command name. This is the name
489 found on the command line in the shell.\
491 @retval NULL the commandString was not a registered command.
492 @return other the name of the MAN file.
493 @sa SHELL_GET_MAN_FILENAME
497 ShellCommandGetManFileNameHandler (
498 IN CONST CHAR16
*CommandString
501 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
504 // assert for NULL parameters
506 ASSERT(CommandString
!= NULL
);
509 // check for the command
511 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
512 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
513 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
515 ASSERT(Node
->CommandString
!= NULL
);
516 if (gUnicodeCollation
->StriColl(
518 (CHAR16
*)CommandString
,
519 Node
->CommandString
) == 0
521 return (Node
->GetManFileName());
528 Get the list of all available shell internal commands. This is a linked list
529 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
530 list functions. do not modify the values.
532 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
534 @return a Linked list of all available shell commands.
538 ShellCommandGetCommandList (
539 IN CONST BOOLEAN Sort
543 // return ((COMMAND_LIST*)(&mCommandList));
545 return ((COMMAND_LIST
*)(&mCommandList
));
549 Registers aliases to be set as part of the initialization of the shell application.
551 If Command is NULL, then ASSERT().
552 If Alias is NULL, then ASSERT().
554 @param[in] Command Pointer to the Command
555 @param[in] Alias Pointer to Alias
557 @retval RETURN_SUCCESS The handlers were registered.
558 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
559 register the shell command.
563 ShellCommandRegisterAlias (
564 IN CONST CHAR16
*Command
,
565 IN CONST CHAR16
*Alias
573 ASSERT(Command
!= NULL
);
574 ASSERT(Alias
!= NULL
);
577 // allocate memory for new struct
579 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
580 ASSERT(Node
!= NULL
);
581 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
582 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
583 ASSERT(Node
->CommandString
!= NULL
);
584 ASSERT(Node
->Alias
!= NULL
);
587 // populate the new struct
589 StrCpy(Node
->CommandString
, Command
);
590 StrCpy(Node
->Alias
, Alias
);
593 // add the new struct to the list
595 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
597 return (RETURN_SUCCESS
);
601 Get the list of all shell alias commands. This is a linked list
602 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
603 list functions. do not modify the values.
605 @return a Linked list of all requested shell alias'.
609 ShellCommandGetInitAliasList (
613 return (&mAliasList
);
617 Determine if a given alias is on the list of built in alias'.
619 @param[in] Alias The alias to test for
621 @retval TRUE The alias is a built in alias
622 @retval FALSE The alias is not a built in alias
626 ShellCommandIsOnAliasList(
627 IN CONST CHAR16
*Alias
633 // assert for NULL parameter
635 ASSERT(Alias
!= NULL
);
638 // check for the Alias
640 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
641 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
642 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
644 ASSERT(Node
->CommandString
!= NULL
);
645 ASSERT(Node
->Alias
!= NULL
);
646 if (gUnicodeCollation
->StriColl(
649 Node
->CommandString
) == 0
653 if (gUnicodeCollation
->StriColl(
665 Function to determine current state of ECHO. Echo determins if lines from scripts
666 and ECHO commands are enabled.
668 @retval TRUE Echo is currently enabled
669 @retval FALSE Echo is currently disabled
673 ShellCommandGetEchoState(
681 Function to set current state of ECHO. Echo determins if lines from scripts
682 and ECHO commands are enabled.
684 If State is TRUE, Echo will be enabled.
685 If State is FALSE, Echo will be disabled.
687 @param[in] State How to set echo.
691 ShellCommandSetEchoState(
699 Indicate that the current shell or script should exit.
701 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
702 @param[in] ErrorCode The 64 bit error code to return.
706 ShellCommandRegisterExit (
707 IN BOOLEAN ScriptOnly
,
708 IN CONST UINT64 ErrorCode
711 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
712 if (mExitRequested
) {
713 mExitScript
= ScriptOnly
;
717 mExitCode
= ErrorCode
;
721 Retrieve the Exit indicator.
723 @retval TRUE Exit was indicated.
724 @retval FALSE Exis was not indicated.
728 ShellCommandGetExit (
732 return (mExitRequested
);
736 Retrieve the Exit code.
738 If ShellCommandGetExit returns FALSE than the return from this is undefined.
740 @return the value passed into RegisterExit.
744 ShellCommandGetExitCode (
751 Retrieve the Exit script indicator.
753 If ShellCommandGetExit returns FALSE than the return from this is undefined.
755 @retval TRUE ScriptOnly was indicated.
756 @retval FALSE ScriptOnly was not indicated.
760 ShellCommandGetScriptExit (
764 return (mExitScript
);
768 Function to cleanup all memory from a SCRIPT_FILE structure.
770 @param[in] Script The pointer to the structure to cleanup.
774 DeleteScriptFileStruct (
775 IN SCRIPT_FILE
*Script
780 if (Script
== NULL
) {
784 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
785 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
787 if (Script
->Argv
!= NULL
) {
788 SHELL_FREE_NON_NULL(Script
->Argv
);
790 Script
->CurrentCommand
= NULL
;
791 while (!IsListEmpty (&Script
->CommandList
)) {
792 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
793 if (Script
->CurrentCommand
!= NULL
) {
794 RemoveEntryList(&Script
->CurrentCommand
->Link
);
795 if (Script
->CurrentCommand
->Cl
!= NULL
) {
796 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
798 if (Script
->CurrentCommand
->Data
!= NULL
) {
799 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
801 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
804 SHELL_FREE_NON_NULL(Script
->ScriptName
);
805 SHELL_FREE_NON_NULL(Script
);
809 Function to return a pointer to the currently running script file object.
811 @retval NULL A script file is not currently running.
812 @return A pointer to the current script file object.
816 ShellCommandGetCurrentScriptFile (
820 SCRIPT_FILE_LIST
*List
;
821 if (IsListEmpty (&mScriptList
.Link
)) {
824 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
829 Function to set a new script as the currently running one.
831 This function will correctly stack and unstack nested scripts.
833 @param[in] Script Pointer to new script information structure. if NULL
834 will remove and de-allocate the top-most Script structure.
836 @return A pointer to the current running script file after this
837 change. NULL if removing the final script.
841 ShellCommandSetNewScript (
842 IN SCRIPT_FILE
*Script OPTIONAL
845 SCRIPT_FILE_LIST
*Node
;
846 if (Script
== NULL
) {
847 if (IsListEmpty (&mScriptList
.Link
)) {
850 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
851 RemoveEntryList(&Node
->Link
);
852 DeleteScriptFileStruct(Node
->Data
);
855 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
860 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
862 return (ShellCommandGetCurrentScriptFile());
866 Function to generate the next default mapping name.
868 If the return value is not NULL then it must be callee freed.
870 @param Type What kind of mapping name to make.
872 @retval NULL a memory allocation failed.
873 @return a new map name string
877 ShellCommandCreateNewMappingName(
878 IN CONST SHELL_MAPPING_TYPE Type
882 ASSERT(Type
< MappingTypeMax
);
886 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
889 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
890 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
891 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
897 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
899 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
900 default paths on the new map name...
902 Path should be FALSE when this function is called from the protocol SetMap function.
904 @param[in] Name The human readable mapped name.
905 @param[in] DevicePath The Device Path for this map.
906 @param[in] Flags The Flags attribute for this map item.
907 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
909 @retval EFI_SUCCESS The addition was sucessful.
910 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
911 @retval EFI_INVALID_PARAMETER A parameter was invalid.
915 ShellCommandAddMapItemAndUpdatePath(
916 IN CONST CHAR16
*Name
,
917 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
918 IN CONST UINT64 Flags
,
919 IN CONST BOOLEAN Path
923 SHELL_MAP_LIST
*MapListNode
;
924 CONST CHAR16
*OriginalPath
;
931 Status
= EFI_SUCCESS
;
933 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
934 if (MapListNode
== NULL
) {
935 Status
= EFI_OUT_OF_RESOURCES
;
937 MapListNode
->Flags
= Flags
;
938 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
939 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
940 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
941 Status
= EFI_OUT_OF_RESOURCES
;
943 StrCpy(MapListNode
->MapName
, Name
);
944 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
947 if (EFI_ERROR(Status
)) {
948 if (MapListNode
!= NULL
) {
949 if (MapListNode
->DevicePath
!= NULL
) {
950 FreePool(MapListNode
->DevicePath
);
952 if (MapListNode
->MapName
!= NULL
) {
953 FreePool(MapListNode
->MapName
);
955 FreePool(MapListNode
);
959 // Since there was no error and Path was TRUE
960 // Now add the correct path for that mapping
962 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
963 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
964 if (OriginalPath
!= NULL
) {
965 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
967 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
969 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
970 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
971 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
972 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
973 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
974 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
975 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
977 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
978 ASSERT_EFI_ERROR(Status
);
985 Creates the default map names for each device path in the system with
986 a protocol depending on the Type.
988 Creates the consistent map names for each device path in the system with
989 a protocol depending on the Type.
991 Note: This will reset all mappings in the system("map -r").
993 Also sets up the default path environment variable if Type is FileSystem.
995 @retval EFI_SUCCESS All map names were created sucessfully.
996 @retval EFI_NOT_FOUND No protocols were found in the system.
997 @return Error returned from gBS->LocateHandle().
1003 ShellCommandCreateInitialMappingsAndPaths(
1008 EFI_HANDLE
*HandleList
;
1010 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1011 CHAR16
*NewDefaultName
;
1012 CHAR16
*NewConsistName
;
1013 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1014 SHELL_MAP_LIST
*MapListNode
;
1019 // Reset the static members back to zero
1024 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1027 // First empty out the existing list.
1029 if (!IsListEmpty(&gShellMapList
.Link
)) {
1030 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1031 ; !IsListEmpty(&gShellMapList
.Link
)
1032 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1034 RemoveEntryList(&MapListNode
->Link
);
1035 FreePool(MapListNode
);
1040 // Find each handle with Simple File System
1042 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1043 if (HandleList
!= NULL
) {
1045 // Do a count of the handles
1047 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1050 // Get all Device Paths
1052 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1053 ASSERT(DevicePathList
!= NULL
);
1055 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1056 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1060 // Sort all DevicePaths
1062 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1064 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1066 // Assign new Mappings to all...
1068 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1070 // Get default name first
1072 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1073 ASSERT(NewDefaultName
!= NULL
);
1074 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1075 ASSERT_EFI_ERROR(Status
);
1076 FreePool(NewDefaultName
);
1079 // Now do consistent name
1081 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1082 if (NewConsistName
!= NULL
) {
1083 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1084 ASSERT_EFI_ERROR(Status
);
1085 FreePool(NewConsistName
);
1089 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1091 SHELL_FREE_NON_NULL(HandleList
);
1092 SHELL_FREE_NON_NULL(DevicePathList
);
1100 // Find each handle with Block Io
1102 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1103 if (HandleList
!= NULL
) {
1104 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1107 // Get all Device Paths
1109 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1110 ASSERT(DevicePathList
!= NULL
);
1112 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1113 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1117 // Sort all DevicePaths
1119 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1122 // Assign new Mappings to all...
1124 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1126 // Get default name first
1128 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1129 ASSERT(NewDefaultName
!= NULL
);
1130 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1131 ASSERT_EFI_ERROR(Status
);
1132 FreePool(NewDefaultName
);
1135 SHELL_FREE_NON_NULL(HandleList
);
1136 SHELL_FREE_NON_NULL(DevicePathList
);
1137 } else if (Count
== (UINTN
)-1) {
1138 return (EFI_NOT_FOUND
);
1141 return (EFI_SUCCESS
);
1145 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1147 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1149 @return a EFI_FILE_PROTOCOL* representing the same file.
1153 ConvertShellHandleToEfiFileProtocol(
1154 IN CONST SHELL_FILE_HANDLE Handle
1157 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1161 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1163 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1164 @param[in] Path The path to the file for verification.
1166 @return A SHELL_FILE_HANDLE representing the same file.
1167 @retval NULL There was not enough memory.
1171 ConvertEfiFileProtocolToShellHandle(
1172 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1173 IN CONST CHAR16
*Path
1176 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1177 BUFFER_LIST
*NewNode
;
1180 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1181 if (Buffer
== NULL
) {
1184 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1185 if (NewNode
== NULL
) {
1188 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1189 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1190 if (Buffer
->Path
== NULL
) {
1193 NewNode
->Buffer
= Buffer
;
1195 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1197 return ((SHELL_FILE_HANDLE
)(Handle
));
1201 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1203 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1205 @return A pointer to the path for the file.
1209 ShellFileHandleGetPath(
1210 IN CONST SHELL_FILE_HANDLE Handle
1215 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1216 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1217 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1219 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1220 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1227 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1229 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1231 @retval TRUE The item was removed.
1232 @retval FALSE The item was not found.
1236 ShellFileHandleRemove(
1237 IN CONST SHELL_FILE_HANDLE Handle
1242 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1243 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1244 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1246 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1247 RemoveEntryList(&Node
->Link
);
1248 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1249 SHELL_FREE_NON_NULL(Node
->Buffer
);
1250 SHELL_FREE_NON_NULL(Node
);
1258 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1260 This will NOT work on directories.
1262 If Handle is NULL, then ASSERT.
1264 @param[in] Handle the file handle
1266 @retval TRUE the position is at the end of the file
1267 @retval FALSE the position is not at the end of the file
1272 IN SHELL_FILE_HANDLE Handle
1275 EFI_FILE_INFO
*Info
;
1280 // ASSERT if Handle is NULL
1282 ASSERT(Handle
!= NULL
);
1284 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1285 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1286 ASSERT(Info
!= NULL
);
1287 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1293 if (Pos
== Info
->FileSize
) {
1305 Frees any BUFFER_LIST defined type.
1307 @param[in] List The BUFFER_LIST object to free.
1312 IN BUFFER_LIST
*List
1315 BUFFER_LIST
*BufferListEntry
;
1321 // enumerate through the buffer list and free all memory
1323 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1324 ; !IsListEmpty (&List
->Link
)
1325 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1327 RemoveEntryList(&BufferListEntry
->Link
);
1328 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1329 if (BufferListEntry
->Buffer
!= NULL
) {
1330 FreePool(BufferListEntry
->Buffer
);
1332 FreePool(BufferListEntry
);