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 BOOLEAN mExitScript
;
30 STATIC CHAR16
*mProfileList
;
31 STATIC UINTN mProfileListSize
;
32 STATIC UINTN mFsMaxCount
= 0;
33 STATIC UINTN mBlkMaxCount
= 0;
34 STATIC BUFFER_LIST mFileHandleList
;
36 // global variables required by library class.
37 EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
= NULL
;
38 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*gDevPathToText
= NULL
;
39 SHELL_MAP_LIST gShellMapList
;
40 SHELL_MAP_LIST
*gShellCurDir
= NULL
;
42 CONST CHAR16
* SupportLevel
[] = {
50 Function to make sure that the global protocol pointers are valid.
51 must be called after constructor before accessing the pointers.
60 if (gUnicodeCollation
== NULL
) {
61 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
62 if (EFI_ERROR(Status
)) {
63 return (EFI_DEVICE_ERROR
);
66 if (gDevPathToText
== NULL
) {
67 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&gDevPathToText
);
68 if (EFI_ERROR(Status
)) {
69 return (EFI_DEVICE_ERROR
);
76 Constructor for the Shell Command library.
78 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
80 @param ImageHandle the image handle of the process
81 @param SystemTable the EFI System Table pointer
83 @retval EFI_SUCCESS the initialization was complete sucessfully
87 ShellCommandLibConstructor (
88 IN EFI_HANDLE ImageHandle
,
89 IN EFI_SYSTEM_TABLE
*SystemTable
93 InitializeListHead(&gShellMapList
.Link
);
94 InitializeListHead(&mCommandList
.Link
);
95 InitializeListHead(&mAliasList
.Link
);
96 InitializeListHead(&mScriptList
.Link
);
97 InitializeListHead(&mFileHandleList
.Link
);
100 mExitRequested
= FALSE
;
102 mProfileListSize
= 0;
105 if (gUnicodeCollation
== NULL
) {
106 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
107 if (EFI_ERROR(Status
)) {
108 return (EFI_DEVICE_ERROR
);
112 return (RETURN_SUCCESS
);
116 Destructor for the library. free any resources.
118 @param ImageHandle the image handle of the process
119 @param SystemTable the EFI System Table pointer
121 @retval RETURN_SUCCESS this function always returns success
125 ShellCommandLibDestructor (
126 IN EFI_HANDLE ImageHandle
,
127 IN EFI_SYSTEM_TABLE
*SystemTable
130 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
132 SCRIPT_FILE_LIST
*Node3
;
133 SHELL_MAP_LIST
*MapNode
;
135 // enumerate throught the list and free all the memory
137 while (!IsListEmpty (&mCommandList
.Link
)) {
138 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
139 RemoveEntryList(&Node
->Link
);
140 SHELL_FREE_NON_NULL(Node
->CommandString
);
142 DEBUG_CODE(Node
= NULL
;);
146 // enumerate through the init command list and free all memory
148 while (!IsListEmpty (&mAliasList
.Link
)) {
149 Node2
= (COMMAND_LIST
*)GetFirstNode(&mAliasList
.Link
);
150 RemoveEntryList(&Node2
->Link
);
151 SHELL_FREE_NON_NULL(Node2
->CommandString
);
153 DEBUG_CODE(Node2
= NULL
;);
157 // enumerate throught the list and free all the memory
159 while (!IsListEmpty (&mScriptList
.Link
)) {
160 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
161 RemoveEntryList(&Node3
->Link
);
162 DeleteScriptFileStruct(Node3
->Data
);
167 // enumerate throught the mappings list and free all the memory
169 if (!IsListEmpty(&gShellMapList
.Link
)) {
170 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
171 ; !IsListEmpty (&gShellMapList
.Link
)
172 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
174 ASSERT(MapNode
!= NULL
);
175 RemoveEntryList(&MapNode
->Link
);
176 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
177 SHELL_FREE_NON_NULL(MapNode
->MapName
);
178 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
182 if (!IsListEmpty(&mFileHandleList
.Link
)){
183 FreeBufferList(&mFileHandleList
);
186 if (mProfileList
!= NULL
) {
187 FreePool(mProfileList
);
190 gUnicodeCollation
= NULL
;
191 gDevPathToText
= NULL
;
194 return (RETURN_SUCCESS
);
198 Checks if a command is already on the list.
200 @param[in] CommandString The command string to check for on the list.
204 ShellCommandIsCommandOnList (
205 IN CONST CHAR16
*CommandString
208 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
211 // assert for NULL parameter
213 ASSERT(CommandString
!= NULL
);
216 // check for the command
218 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
219 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
220 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
222 ASSERT(Node
->CommandString
!= NULL
);
223 if (gUnicodeCollation
->StriColl(
225 (CHAR16
*)CommandString
,
226 Node
->CommandString
) == 0
235 Get the help text for a command.
237 @param[in] CommandString The command name.
239 @retval NULL No help text was found.
240 @return String of help text. Caller reuiqred to free.
244 ShellCommandGetCommandHelp (
245 IN CONST CHAR16
*CommandString
248 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
251 // assert for NULL parameter
253 ASSERT(CommandString
!= NULL
);
256 // check for the command
258 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
259 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
260 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
262 ASSERT(Node
->CommandString
!= NULL
);
263 if (gUnicodeCollation
->StriColl(
265 (CHAR16
*)CommandString
,
266 Node
->CommandString
) == 0
268 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
275 Registers handlers of type SHELL_RUN_COMMAND and
276 SHELL_GET_MAN_FILENAME for each shell command.
278 If the ShellSupportLevel is greater than the value of the
279 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
281 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
282 with the command specified by CommandString. If the command named by
283 CommandString has already been registered, then return
284 RETURN_ALREADY_STARTED.
286 If there are not enough resources available to register the handlers then
287 RETURN_OUT_OF_RESOURCES is returned.
289 If CommandString is NULL, then ASSERT().
290 If GetHelpInfoHandler is NULL, then ASSERT().
291 If CommandHandler is NULL, then ASSERT().
292 If ProfileName is NULL, then ASSERT().
294 @param[in] CommandString Pointer to the command name. This is the
295 name to look for on the command line in
297 @param[in] CommandHandler Pointer to a function that runs the
299 @param[in] GetManFileName Pointer to a function that provides man
301 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
303 @param[in] ProfileName profile name to require for support of this
305 @param[in] CanAffectLE indicates whether this command's return value
306 can change the LASTERROR environment variable.
307 @param[in] HiiHandle Handle of this command's HII entry.
308 @param[in] ManFormatHelp HII locator for the help text.
310 @retval RETURN_SUCCESS The handlers were registered.
311 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
312 register the shell command.
313 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
314 currently allowed support level.
315 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
316 is already registered. Only 1 handler set for
317 a given command is allowed.
318 @sa SHELL_GET_MAN_FILENAME
319 @sa SHELL_RUN_COMMAND
323 ShellCommandRegisterCommandName (
324 IN CONST CHAR16
*CommandString
,
325 IN SHELL_RUN_COMMAND CommandHandler
,
326 IN SHELL_GET_MAN_FILENAME GetManFileName
,
327 IN UINT32 ShellMinSupportLevel
,
328 IN CONST CHAR16
*ProfileName
,
329 IN CONST BOOLEAN CanAffectLE
,
330 IN CONST EFI_HANDLE HiiHandle
,
331 IN CONST EFI_STRING_ID ManFormatHelp
334 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
337 // ASSERTs for NULL parameters
339 ASSERT(CommandString
!= NULL
);
340 ASSERT(GetManFileName
!= NULL
);
341 ASSERT(CommandHandler
!= NULL
);
342 ASSERT(ProfileName
!= NULL
);
345 // check for shell support level
347 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
348 return (RETURN_UNSUPPORTED
);
352 // check for already on the list
354 if (ShellCommandIsCommandOnList(CommandString
)) {
355 return (RETURN_ALREADY_STARTED
);
359 // allocate memory for new struct
361 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
362 ASSERT(Node
!= NULL
);
363 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
364 ASSERT(Node
->CommandString
!= NULL
);
367 // populate the new struct
369 StrCpy(Node
->CommandString
, CommandString
);
371 Node
->GetManFileName
= GetManFileName
;
372 Node
->CommandHandler
= CommandHandler
;
373 Node
->LastError
= CanAffectLE
;
374 Node
->HiiHandle
= HiiHandle
;
375 Node
->ManFormatHelp
= ManFormatHelp
;
377 if ( StrLen(ProfileName
)>0
378 && ((mProfileList
!= NULL
379 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
381 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
382 if (mProfileList
== NULL
) {
384 // If this is the first make a leading ';'
386 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
388 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
389 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
393 // add the new struct to the list
395 InsertTailList (&mCommandList
.Link
, &Node
->Link
);
397 return (RETURN_SUCCESS
);
401 Function to get the current Profile string.
403 @retval NULL There are no installed profiles.
404 @return A semi-colon delimited list of profiles.
408 ShellCommandGetProfileList (
412 return (mProfileList
);
416 Checks if a command string has been registered for CommandString and if so it runs
417 the previously registered handler for that command with the command line.
419 If CommandString is NULL, then ASSERT().
421 If Sections is specified, then each section name listed will be compared in a casesensitive
422 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
423 it will be appended to the returned help text. If the section does not exist, no
424 information will be returned. If Sections is NULL, then all help text information
425 available will be returned.
427 @param[in] CommandString Pointer to the command name. This is the name
428 found on the command line in the shell.
429 @param[in,out] RetVal Pointer to the return vaule from the command handler.
431 @param[in,out] CanAffectLE indicates whether this command's return value
432 needs to be placed into LASTERROR environment variable.
434 @retval RETURN_SUCCESS The handler was run.
435 @retval RETURN_NOT_FOUND The CommandString did not match a registered
437 @sa SHELL_RUN_COMMAND
441 ShellCommandRunCommandHandler (
442 IN CONST CHAR16
*CommandString
,
443 IN OUT SHELL_STATUS
*RetVal
,
444 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
447 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
450 // assert for NULL parameters
452 ASSERT(CommandString
!= NULL
);
455 // check for the command
457 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
458 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
459 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
461 ASSERT(Node
->CommandString
!= NULL
);
462 if (gUnicodeCollation
->StriColl(
464 (CHAR16
*)CommandString
,
465 Node
->CommandString
) == 0
467 if (CanAffectLE
!= NULL
) {
468 *CanAffectLE
= Node
->LastError
;
470 if (RetVal
!= NULL
) {
471 *RetVal
= Node
->CommandHandler(NULL
, gST
);
473 Node
->CommandHandler(NULL
, gST
);
475 return (RETURN_SUCCESS
);
478 return (RETURN_NOT_FOUND
);
482 Checks if a command string has been registered for CommandString and if so it
483 returns the MAN filename specified for that command.
485 If CommandString is NULL, then ASSERT().
487 @param[in] CommandString Pointer to the command name. This is the name
488 found on the command line in the shell.\
490 @retval NULL the commandString was not a registered command.
491 @return other the name of the MAN file.
492 @sa SHELL_GET_MAN_FILENAME
496 ShellCommandGetManFileNameHandler (
497 IN CONST CHAR16
*CommandString
500 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
503 // assert for NULL parameters
505 ASSERT(CommandString
!= NULL
);
508 // check for the command
510 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
511 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
512 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
514 ASSERT(Node
->CommandString
!= NULL
);
515 if (gUnicodeCollation
->StriColl(
517 (CHAR16
*)CommandString
,
518 Node
->CommandString
) == 0
520 return (Node
->GetManFileName());
527 Get the list of all available shell internal commands. This is a linked list
528 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
529 list functions. do not modify the values.
531 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
533 @return a Linked list of all available shell commands.
537 ShellCommandGetCommandList (
538 IN CONST BOOLEAN Sort
542 // return ((COMMAND_LIST*)(&mCommandList));
544 return ((COMMAND_LIST
*)(&mCommandList
));
548 Registers aliases to be set as part of the initialization of the shell application.
550 If Command is NULL, then ASSERT().
551 If Alias is NULL, then ASSERT().
553 @param[in] Command Pointer to the Command
554 @param[in] Alias Pointer to Alias
556 @retval RETURN_SUCCESS The handlers were registered.
557 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
558 register the shell command.
562 ShellCommandRegisterAlias (
563 IN CONST CHAR16
*Command
,
564 IN CONST CHAR16
*Alias
572 ASSERT(Command
!= NULL
);
573 ASSERT(Alias
!= NULL
);
576 // allocate memory for new struct
578 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
579 ASSERT(Node
!= NULL
);
580 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
581 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
582 ASSERT(Node
->CommandString
!= NULL
);
583 ASSERT(Node
->Alias
!= NULL
);
586 // populate the new struct
588 StrCpy(Node
->CommandString
, Command
);
589 StrCpy(Node
->Alias
, Alias
);
592 // add the new struct to the list
594 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
596 return (RETURN_SUCCESS
);
600 Get the list of all shell alias commands. This is a linked list
601 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
602 list functions. do not modify the values.
604 @return a Linked list of all requested shell alias'.
608 ShellCommandGetInitAliasList (
612 return (&mAliasList
);
616 Determine if a given alias is on the list of built in alias'.
618 @param[in] Alias The alias to test for
620 @retval TRUE The alias is a built in alias
621 @retval FALSE The alias is not a built in alias
625 ShellCommandIsOnAliasList(
626 IN CONST CHAR16
*Alias
632 // assert for NULL parameter
634 ASSERT(Alias
!= NULL
);
637 // check for the Alias
639 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
640 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
641 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
643 ASSERT(Node
->CommandString
!= NULL
);
644 ASSERT(Node
->Alias
!= NULL
);
645 if (gUnicodeCollation
->StriColl(
648 Node
->CommandString
) == 0
652 if (gUnicodeCollation
->StriColl(
664 Function to determine current state of ECHO. Echo determins if lines from scripts
665 and ECHO commands are enabled.
667 @retval TRUE Echo is currently enabled
668 @retval FALSE Echo is currently disabled
672 ShellCommandGetEchoState(
680 Function to set current state of ECHO. Echo determins if lines from scripts
681 and ECHO commands are enabled.
683 If State is TRUE, Echo will be enabled.
684 If State is FALSE, Echo will be disabled.
686 @param[in] State How to set echo.
690 ShellCommandSetEchoState(
698 Indicate that the current shell or script should exit.
700 @param[in] ScriptOnly TRUE if only exiting a script, FALSE othrwise.
704 ShellCommandRegisterExit (
705 IN BOOLEAN ScriptOnly
708 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
709 if (mExitRequested
) {
710 mExitScript
= ScriptOnly
;
717 Retrieve the Exit indicator.
719 @retval TRUE Exit was indicated.
720 @retval FALSE Exis was not indicated.
724 ShellCommandGetExit (
728 return (mExitRequested
);
732 Retrieve the Exit script indicator.
734 If ShellCommandGetExit returns FALSE than the return from this is undefined.
736 @retval TRUE ScriptOnly was indicated.
737 @retval FALSE ScriptOnly was not indicated.
741 ShellCommandGetScriptExit (
745 return (mExitScript
);
749 Function to cleanup all memory from a SCRIPT_FILE structure.
751 @param[in] Script The pointer to the structure to cleanup.
755 DeleteScriptFileStruct (
756 IN SCRIPT_FILE
*Script
761 if (Script
== NULL
) {
765 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
766 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
768 if (Script
->Argv
!= NULL
) {
769 SHELL_FREE_NON_NULL(Script
->Argv
);
771 Script
->CurrentCommand
= NULL
;
772 while (!IsListEmpty (&Script
->CommandList
)) {
773 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
774 if (Script
->CurrentCommand
!= NULL
) {
775 RemoveEntryList(&Script
->CurrentCommand
->Link
);
776 if (Script
->CurrentCommand
->Cl
!= NULL
) {
777 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
779 if (Script
->CurrentCommand
->Data
!= NULL
) {
780 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
782 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
785 SHELL_FREE_NON_NULL(Script
->ScriptName
);
786 SHELL_FREE_NON_NULL(Script
);
790 Function to return a pointer to the currently running script file object.
792 @retval NULL A script file is not currently running.
793 @return A pointer to the current script file object.
797 ShellCommandGetCurrentScriptFile (
801 SCRIPT_FILE_LIST
*List
;
802 if (IsListEmpty (&mScriptList
.Link
)) {
805 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
810 Function to set a new script as the currently running one.
812 This function will correctly stack and unstack nested scripts.
814 @param[in] Script Pointer to new script information structure. if NULL
815 will remove and de-allocate the top-most Script structure.
817 @return A pointer to the current running script file after this
818 change. NULL if removing the final script.
822 ShellCommandSetNewScript (
823 IN SCRIPT_FILE
*Script OPTIONAL
826 SCRIPT_FILE_LIST
*Node
;
827 if (Script
== NULL
) {
828 if (IsListEmpty (&mScriptList
.Link
)) {
831 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
832 RemoveEntryList(&Node
->Link
);
833 DeleteScriptFileStruct(Node
->Data
);
836 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
841 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
843 return (ShellCommandGetCurrentScriptFile());
847 Function to generate the next default mapping name.
849 If the return value is not NULL then it must be callee freed.
851 @param Type What kind of mapping name to make.
853 @retval NULL a memory allocation failed.
854 @return a new map name string
858 ShellCommandCreateNewMappingName(
859 IN CONST SHELL_MAPPING_TYPE Type
863 ASSERT(Type
< MappingTypeMax
);
867 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
870 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
871 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
872 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
878 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
880 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
881 default paths on the new map name...
883 Path should be FALSE when this function is called from the protocol SetMap function.
885 @param[in] Name The human readable mapped name.
886 @param[in] DevicePath The Device Path for this map.
887 @param[in] Flags The Flags attribute for this map item.
888 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
890 @retval EFI_SUCCESS The addition was sucessful.
891 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
892 @retval EFI_INVALID_PARAMETER A parameter was invalid.
896 ShellCommandAddMapItemAndUpdatePath(
897 IN CONST CHAR16
*Name
,
898 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
899 IN CONST UINT64 Flags
,
900 IN CONST BOOLEAN Path
904 SHELL_MAP_LIST
*MapListNode
;
905 CONST CHAR16
*OriginalPath
;
912 Status
= EFI_SUCCESS
;
914 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
915 if (MapListNode
== NULL
) {
916 Status
= EFI_OUT_OF_RESOURCES
;
918 MapListNode
->Flags
= Flags
;
919 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
920 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
921 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
922 Status
= EFI_OUT_OF_RESOURCES
;
924 StrCpy(MapListNode
->MapName
, Name
);
925 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
928 if (EFI_ERROR(Status
)) {
929 if (MapListNode
!= NULL
) {
930 if (MapListNode
->DevicePath
!= NULL
) {
931 FreePool(MapListNode
->DevicePath
);
933 if (MapListNode
->MapName
!= NULL
) {
934 FreePool(MapListNode
->MapName
);
936 FreePool(MapListNode
);
940 // Since there was no error and Path was TRUE
941 // Now add the correct path for that mapping
943 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
944 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
945 if (OriginalPath
!= NULL
) {
946 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
948 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
950 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
951 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
952 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
953 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
954 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
955 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
956 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
958 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
959 ASSERT_EFI_ERROR(Status
);
966 Creates the default map names for each device path in the system with
967 a protocol depending on the Type.
969 Creates the consistent map names for each device path in the system with
970 a protocol depending on the Type.
972 Note: This will reset all mappings in the system("map -r").
974 Also sets up the default path environment variable if Type is FileSystem.
976 @retval EFI_SUCCESS All map names were created sucessfully.
977 @retval EFI_NOT_FOUND No protocols were found in the system.
978 @return Error returned from gBS->LocateHandle().
984 ShellCommandCreateInitialMappingsAndPaths(
989 EFI_HANDLE
*HandleList
;
991 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
992 CHAR16
*NewDefaultName
;
993 CHAR16
*NewConsistName
;
994 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
995 SHELL_MAP_LIST
*MapListNode
;
1000 // Reset the static members back to zero
1005 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1008 // First empty out the existing list.
1010 if (!IsListEmpty(&gShellMapList
.Link
)) {
1011 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1012 ; !IsListEmpty(&gShellMapList
.Link
)
1013 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1015 RemoveEntryList(&MapListNode
->Link
);
1016 FreePool(MapListNode
);
1021 // Find each handle with Simple File System
1023 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1024 if (HandleList
!= NULL
) {
1026 // Do a count of the handles
1028 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1031 // Get all Device Paths
1033 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1034 ASSERT(DevicePathList
!= NULL
);
1036 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1037 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1041 // Sort all DevicePaths
1043 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1045 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1047 // Assign new Mappings to all...
1049 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1051 // Get default name first
1053 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1054 ASSERT(NewDefaultName
!= NULL
);
1055 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1056 ASSERT_EFI_ERROR(Status
);
1057 FreePool(NewDefaultName
);
1060 // Now do consistent name
1062 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1063 if (NewConsistName
!= NULL
) {
1064 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1065 ASSERT_EFI_ERROR(Status
);
1066 FreePool(NewConsistName
);
1070 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1072 SHELL_FREE_NON_NULL(HandleList
);
1073 SHELL_FREE_NON_NULL(DevicePathList
);
1081 // Find each handle with Block Io
1083 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1084 if (HandleList
!= NULL
) {
1085 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1088 // Get all Device Paths
1090 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1091 ASSERT(DevicePathList
!= NULL
);
1093 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1094 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1098 // Sort all DevicePaths
1100 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1103 // Assign new Mappings to all...
1105 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1107 // Get default name first
1109 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1110 ASSERT(NewDefaultName
!= NULL
);
1111 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1112 ASSERT_EFI_ERROR(Status
);
1113 FreePool(NewDefaultName
);
1116 SHELL_FREE_NON_NULL(HandleList
);
1117 SHELL_FREE_NON_NULL(DevicePathList
);
1118 } else if (Count
== (UINTN
)-1) {
1119 return (EFI_NOT_FOUND
);
1122 return (EFI_SUCCESS
);
1126 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1128 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1130 @return a EFI_FILE_PROTOCOL* representing the same file.
1134 ConvertShellHandleToEfiFileProtocol(
1135 IN CONST SHELL_FILE_HANDLE Handle
1138 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1142 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1144 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1145 @param[in] Path The path to the file for verification.
1147 @return A SHELL_FILE_HANDLE representing the same file.
1148 @retval NULL There was not enough memory.
1152 ConvertEfiFileProtocolToShellHandle(
1153 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1154 IN CONST CHAR16
*Path
1157 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1158 BUFFER_LIST
*NewNode
;
1161 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1162 if (Buffer
== NULL
) {
1165 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1166 if (NewNode
== NULL
) {
1169 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1170 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1171 if (Buffer
->Path
== NULL
) {
1174 NewNode
->Buffer
= Buffer
;
1176 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1178 return ((SHELL_FILE_HANDLE
)(Handle
));
1182 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1184 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1186 @return A pointer to the path for the file.
1190 ShellFileHandleGetPath(
1191 IN CONST SHELL_FILE_HANDLE Handle
1196 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1197 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1198 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1200 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1201 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1208 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1210 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1212 @retval TRUE The item was removed.
1213 @retval FALSE The item was not found.
1217 ShellFileHandleRemove(
1218 IN CONST SHELL_FILE_HANDLE Handle
1223 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1224 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1225 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1227 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1228 RemoveEntryList(&Node
->Link
);
1229 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1230 SHELL_FREE_NON_NULL(Node
->Buffer
);
1231 SHELL_FREE_NON_NULL(Node
);
1239 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1241 This will NOT work on directories.
1243 If Handle is NULL, then ASSERT.
1245 @param[in] Handle the file handle
1247 @retval TRUE the position is at the end of the file
1248 @retval FALSE the position is not at the end of the file
1253 IN SHELL_FILE_HANDLE Handle
1256 EFI_FILE_INFO
*Info
;
1261 // ASSERT if Handle is NULL
1263 ASSERT(Handle
!= NULL
);
1265 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1266 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1267 ASSERT(Info
!= NULL
);
1268 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1274 if (Pos
== Info
->FileSize
) {
1286 Frees any BUFFER_LIST defined type.
1288 @param[in] List The BUFFER_LIST object to free.
1293 IN BUFFER_LIST
*List
1296 BUFFER_LIST
*BufferListEntry
;
1302 // enumerate through the buffer list and free all memory
1304 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1305 ; !IsListEmpty (&List
->Link
)
1306 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1308 RemoveEntryList(&BufferListEntry
->Link
);
1309 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1310 if (BufferListEntry
->Buffer
!= NULL
) {
1311 FreePool(BufferListEntry
->Buffer
);
1313 FreePool(BufferListEntry
);