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 alias list and free all memory
149 while (!IsListEmpty (&mAliasList
.Link
)) {
150 Node2
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
);
151 RemoveEntryList(&Node2
->Link
);
152 SHELL_FREE_NON_NULL(Node2
->CommandString
);
153 SHELL_FREE_NON_NULL(Node2
->Alias
);
154 SHELL_FREE_NON_NULL(Node2
);
155 DEBUG_CODE(Node2
= NULL
;);
159 // enumerate throught the list and free all the memory
161 while (!IsListEmpty (&mScriptList
.Link
)) {
162 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
163 RemoveEntryList(&Node3
->Link
);
164 DeleteScriptFileStruct(Node3
->Data
);
169 // enumerate throught the mappings list and free all the memory
171 if (!IsListEmpty(&gShellMapList
.Link
)) {
172 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
173 ; !IsListEmpty (&gShellMapList
.Link
)
174 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
176 ASSERT(MapNode
!= NULL
);
177 RemoveEntryList(&MapNode
->Link
);
178 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
179 SHELL_FREE_NON_NULL(MapNode
->MapName
);
180 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
184 if (!IsListEmpty(&mFileHandleList
.Link
)){
185 FreeBufferList(&mFileHandleList
);
188 if (mProfileList
!= NULL
) {
189 FreePool(mProfileList
);
192 gUnicodeCollation
= NULL
;
193 gDevPathToText
= NULL
;
196 return (RETURN_SUCCESS
);
200 Checks if a command is already on the list.
202 @param[in] CommandString The command string to check for on the list.
206 ShellCommandIsCommandOnList (
207 IN CONST CHAR16
*CommandString
210 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
213 // assert for NULL parameter
215 ASSERT(CommandString
!= NULL
);
218 // check for the command
220 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
221 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
222 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
224 ASSERT(Node
->CommandString
!= NULL
);
225 if (gUnicodeCollation
->StriColl(
227 (CHAR16
*)CommandString
,
228 Node
->CommandString
) == 0
237 Get the help text for a command.
239 @param[in] CommandString The command name.
241 @retval NULL No help text was found.
242 @return String of help text. Caller reuiqred to free.
246 ShellCommandGetCommandHelp (
247 IN CONST CHAR16
*CommandString
250 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
253 // assert for NULL parameter
255 ASSERT(CommandString
!= NULL
);
258 // check for the command
260 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
261 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
262 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
264 ASSERT(Node
->CommandString
!= NULL
);
265 if (gUnicodeCollation
->StriColl(
267 (CHAR16
*)CommandString
,
268 Node
->CommandString
) == 0
270 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
277 Registers handlers of type SHELL_RUN_COMMAND and
278 SHELL_GET_MAN_FILENAME for each shell command.
280 If the ShellSupportLevel is greater than the value of the
281 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
283 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
284 with the command specified by CommandString. If the command named by
285 CommandString has already been registered, then return
286 RETURN_ALREADY_STARTED.
288 If there are not enough resources available to register the handlers then
289 RETURN_OUT_OF_RESOURCES is returned.
291 If CommandString is NULL, then ASSERT().
292 If GetHelpInfoHandler is NULL, then ASSERT().
293 If CommandHandler is NULL, then ASSERT().
294 If ProfileName is NULL, then ASSERT().
296 @param[in] CommandString Pointer to the command name. This is the
297 name to look for on the command line in
299 @param[in] CommandHandler Pointer to a function that runs the
301 @param[in] GetManFileName Pointer to a function that provides man
303 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
305 @param[in] ProfileName profile name to require for support of this
307 @param[in] CanAffectLE indicates whether this command's return value
308 can change the LASTERROR environment variable.
309 @param[in] HiiHandle Handle of this command's HII entry.
310 @param[in] ManFormatHelp HII locator for the help text.
312 @retval RETURN_SUCCESS The handlers were registered.
313 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
314 register the shell command.
315 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
316 currently allowed support level.
317 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
318 is already registered. Only 1 handler set for
319 a given command is allowed.
320 @sa SHELL_GET_MAN_FILENAME
321 @sa SHELL_RUN_COMMAND
325 ShellCommandRegisterCommandName (
326 IN CONST CHAR16
*CommandString
,
327 IN SHELL_RUN_COMMAND CommandHandler
,
328 IN SHELL_GET_MAN_FILENAME GetManFileName
,
329 IN UINT32 ShellMinSupportLevel
,
330 IN CONST CHAR16
*ProfileName
,
331 IN CONST BOOLEAN CanAffectLE
,
332 IN CONST EFI_HANDLE HiiHandle
,
333 IN CONST EFI_STRING_ID ManFormatHelp
336 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
339 // ASSERTs for NULL parameters
341 ASSERT(CommandString
!= NULL
);
342 ASSERT(GetManFileName
!= NULL
);
343 ASSERT(CommandHandler
!= NULL
);
344 ASSERT(ProfileName
!= NULL
);
347 // check for shell support level
349 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
350 return (RETURN_UNSUPPORTED
);
354 // check for already on the list
356 if (ShellCommandIsCommandOnList(CommandString
)) {
357 return (RETURN_ALREADY_STARTED
);
361 // allocate memory for new struct
363 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
364 ASSERT(Node
!= NULL
);
365 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
366 ASSERT(Node
->CommandString
!= NULL
);
369 // populate the new struct
371 StrCpy(Node
->CommandString
, CommandString
);
373 Node
->GetManFileName
= GetManFileName
;
374 Node
->CommandHandler
= CommandHandler
;
375 Node
->LastError
= CanAffectLE
;
376 Node
->HiiHandle
= HiiHandle
;
377 Node
->ManFormatHelp
= ManFormatHelp
;
379 if ( StrLen(ProfileName
)>0
380 && ((mProfileList
!= NULL
381 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
383 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
384 if (mProfileList
== NULL
) {
386 // If this is the first make a leading ';'
388 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
390 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
391 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
395 // add the new struct to the list
397 InsertTailList (&mCommandList
.Link
, &Node
->Link
);
399 return (RETURN_SUCCESS
);
403 Function to get the current Profile string.
405 @retval NULL There are no installed profiles.
406 @return A semi-colon delimited list of profiles.
410 ShellCommandGetProfileList (
414 return (mProfileList
);
418 Checks if a command string has been registered for CommandString and if so it runs
419 the previously registered handler for that command with the command line.
421 If CommandString is NULL, then ASSERT().
423 If Sections is specified, then each section name listed will be compared in a casesensitive
424 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
425 it will be appended to the returned help text. If the section does not exist, no
426 information will be returned. If Sections is NULL, then all help text information
427 available will be returned.
429 @param[in] CommandString Pointer to the command name. This is the name
430 found on the command line in the shell.
431 @param[in, out] RetVal Pointer to the return vaule from the command handler.
433 @param[in, out] CanAffectLE indicates whether this command's return value
434 needs to be placed into LASTERROR environment variable.
436 @retval RETURN_SUCCESS The handler was run.
437 @retval RETURN_NOT_FOUND The CommandString did not match a registered
439 @sa SHELL_RUN_COMMAND
443 ShellCommandRunCommandHandler (
444 IN CONST CHAR16
*CommandString
,
445 IN OUT SHELL_STATUS
*RetVal
,
446 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
449 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
452 // assert for NULL parameters
454 ASSERT(CommandString
!= NULL
);
457 // check for the command
459 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
460 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
461 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
463 ASSERT(Node
->CommandString
!= NULL
);
464 if (gUnicodeCollation
->StriColl(
466 (CHAR16
*)CommandString
,
467 Node
->CommandString
) == 0
469 if (CanAffectLE
!= NULL
) {
470 *CanAffectLE
= Node
->LastError
;
472 if (RetVal
!= NULL
) {
473 *RetVal
= Node
->CommandHandler(NULL
, gST
);
475 Node
->CommandHandler(NULL
, gST
);
477 return (RETURN_SUCCESS
);
480 return (RETURN_NOT_FOUND
);
484 Checks if a command string has been registered for CommandString and if so it
485 returns the MAN filename specified for that command.
487 If CommandString is NULL, then ASSERT().
489 @param[in] CommandString Pointer to the command name. This is the name
490 found on the command line in the shell.\
492 @retval NULL the commandString was not a registered command.
493 @return other the name of the MAN file.
494 @sa SHELL_GET_MAN_FILENAME
498 ShellCommandGetManFileNameHandler (
499 IN CONST CHAR16
*CommandString
502 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
505 // assert for NULL parameters
507 ASSERT(CommandString
!= NULL
);
510 // check for the command
512 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
513 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
514 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
516 ASSERT(Node
->CommandString
!= NULL
);
517 if (gUnicodeCollation
->StriColl(
519 (CHAR16
*)CommandString
,
520 Node
->CommandString
) == 0
522 return (Node
->GetManFileName());
529 Get the list of all available shell internal commands. This is a linked list
530 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
531 list functions. do not modify the values.
533 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
535 @return a Linked list of all available shell commands.
539 ShellCommandGetCommandList (
540 IN CONST BOOLEAN Sort
544 // return ((COMMAND_LIST*)(&mCommandList));
546 return ((COMMAND_LIST
*)(&mCommandList
));
550 Registers aliases to be set as part of the initialization of the shell application.
552 If Command is NULL, then ASSERT().
553 If Alias is NULL, then ASSERT().
555 @param[in] Command Pointer to the Command
556 @param[in] Alias Pointer to Alias
558 @retval RETURN_SUCCESS The handlers were registered.
559 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
560 register the shell command.
564 ShellCommandRegisterAlias (
565 IN CONST CHAR16
*Command
,
566 IN CONST CHAR16
*Alias
574 ASSERT(Command
!= NULL
);
575 ASSERT(Alias
!= NULL
);
578 // allocate memory for new struct
580 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
581 ASSERT(Node
!= NULL
);
582 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
583 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
584 ASSERT(Node
->CommandString
!= NULL
);
585 ASSERT(Node
->Alias
!= NULL
);
588 // populate the new struct
590 StrCpy(Node
->CommandString
, Command
);
591 StrCpy(Node
->Alias
, Alias
);
594 // add the new struct to the list
596 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
598 return (RETURN_SUCCESS
);
602 Get the list of all shell alias commands. This is a linked list
603 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
604 list functions. do not modify the values.
606 @return a Linked list of all requested shell alias'.
610 ShellCommandGetInitAliasList (
614 return (&mAliasList
);
618 Determine if a given alias is on the list of built in alias'.
620 @param[in] Alias The alias to test for
622 @retval TRUE The alias is a built in alias
623 @retval FALSE The alias is not a built in alias
627 ShellCommandIsOnAliasList(
628 IN CONST CHAR16
*Alias
634 // assert for NULL parameter
636 ASSERT(Alias
!= NULL
);
639 // check for the Alias
641 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
642 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
643 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
645 ASSERT(Node
->CommandString
!= NULL
);
646 ASSERT(Node
->Alias
!= NULL
);
647 if (gUnicodeCollation
->StriColl(
650 Node
->CommandString
) == 0
654 if (gUnicodeCollation
->StriColl(
666 Function to determine current state of ECHO. Echo determins if lines from scripts
667 and ECHO commands are enabled.
669 @retval TRUE Echo is currently enabled
670 @retval FALSE Echo is currently disabled
674 ShellCommandGetEchoState(
682 Function to set current state of ECHO. Echo determins if lines from scripts
683 and ECHO commands are enabled.
685 If State is TRUE, Echo will be enabled.
686 If State is FALSE, Echo will be disabled.
688 @param[in] State How to set echo.
692 ShellCommandSetEchoState(
700 Indicate that the current shell or script should exit.
702 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
703 @param[in] ErrorCode The 64 bit error code to return.
707 ShellCommandRegisterExit (
708 IN BOOLEAN ScriptOnly
,
709 IN CONST UINT64 ErrorCode
712 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
713 if (mExitRequested
) {
714 mExitScript
= ScriptOnly
;
718 mExitCode
= ErrorCode
;
722 Retrieve the Exit indicator.
724 @retval TRUE Exit was indicated.
725 @retval FALSE Exis was not indicated.
729 ShellCommandGetExit (
733 return (mExitRequested
);
737 Retrieve the Exit code.
739 If ShellCommandGetExit returns FALSE than the return from this is undefined.
741 @return the value passed into RegisterExit.
745 ShellCommandGetExitCode (
752 Retrieve the Exit script indicator.
754 If ShellCommandGetExit returns FALSE than the return from this is undefined.
756 @retval TRUE ScriptOnly was indicated.
757 @retval FALSE ScriptOnly was not indicated.
761 ShellCommandGetScriptExit (
765 return (mExitScript
);
769 Function to cleanup all memory from a SCRIPT_FILE structure.
771 @param[in] Script The pointer to the structure to cleanup.
775 DeleteScriptFileStruct (
776 IN SCRIPT_FILE
*Script
781 if (Script
== NULL
) {
785 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
786 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
788 if (Script
->Argv
!= NULL
) {
789 SHELL_FREE_NON_NULL(Script
->Argv
);
791 Script
->CurrentCommand
= NULL
;
792 while (!IsListEmpty (&Script
->CommandList
)) {
793 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
794 if (Script
->CurrentCommand
!= NULL
) {
795 RemoveEntryList(&Script
->CurrentCommand
->Link
);
796 if (Script
->CurrentCommand
->Cl
!= NULL
) {
797 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
799 if (Script
->CurrentCommand
->Data
!= NULL
) {
800 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
802 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
805 SHELL_FREE_NON_NULL(Script
->ScriptName
);
806 SHELL_FREE_NON_NULL(Script
);
810 Function to return a pointer to the currently running script file object.
812 @retval NULL A script file is not currently running.
813 @return A pointer to the current script file object.
817 ShellCommandGetCurrentScriptFile (
821 SCRIPT_FILE_LIST
*List
;
822 if (IsListEmpty (&mScriptList
.Link
)) {
825 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
830 Function to set a new script as the currently running one.
832 This function will correctly stack and unstack nested scripts.
834 @param[in] Script Pointer to new script information structure. if NULL
835 will remove and de-allocate the top-most Script structure.
837 @return A pointer to the current running script file after this
838 change. NULL if removing the final script.
842 ShellCommandSetNewScript (
843 IN SCRIPT_FILE
*Script OPTIONAL
846 SCRIPT_FILE_LIST
*Node
;
847 if (Script
== NULL
) {
848 if (IsListEmpty (&mScriptList
.Link
)) {
851 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
852 RemoveEntryList(&Node
->Link
);
853 DeleteScriptFileStruct(Node
->Data
);
856 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
861 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
863 return (ShellCommandGetCurrentScriptFile());
867 Function to generate the next default mapping name.
869 If the return value is not NULL then it must be callee freed.
871 @param Type What kind of mapping name to make.
873 @retval NULL a memory allocation failed.
874 @return a new map name string
878 ShellCommandCreateNewMappingName(
879 IN CONST SHELL_MAPPING_TYPE Type
883 ASSERT(Type
< MappingTypeMax
);
887 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
890 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
891 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
892 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
898 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
900 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
901 default paths on the new map name...
903 Path should be FALSE when this function is called from the protocol SetMap function.
905 @param[in] Name The human readable mapped name.
906 @param[in] DevicePath The Device Path for this map.
907 @param[in] Flags The Flags attribute for this map item.
908 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
910 @retval EFI_SUCCESS The addition was sucessful.
911 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
912 @retval EFI_INVALID_PARAMETER A parameter was invalid.
916 ShellCommandAddMapItemAndUpdatePath(
917 IN CONST CHAR16
*Name
,
918 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
919 IN CONST UINT64 Flags
,
920 IN CONST BOOLEAN Path
924 SHELL_MAP_LIST
*MapListNode
;
925 CONST CHAR16
*OriginalPath
;
932 Status
= EFI_SUCCESS
;
934 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
935 if (MapListNode
== NULL
) {
936 Status
= EFI_OUT_OF_RESOURCES
;
938 MapListNode
->Flags
= Flags
;
939 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
940 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
941 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
942 Status
= EFI_OUT_OF_RESOURCES
;
944 StrCpy(MapListNode
->MapName
, Name
);
945 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
948 if (EFI_ERROR(Status
)) {
949 if (MapListNode
!= NULL
) {
950 if (MapListNode
->DevicePath
!= NULL
) {
951 FreePool(MapListNode
->DevicePath
);
953 if (MapListNode
->MapName
!= NULL
) {
954 FreePool(MapListNode
->MapName
);
956 FreePool(MapListNode
);
960 // Since there was no error and Path was TRUE
961 // Now add the correct path for that mapping
963 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
964 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
965 if (OriginalPath
!= NULL
) {
966 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
968 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
970 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
971 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
972 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
973 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
974 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
975 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
976 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
978 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
979 ASSERT_EFI_ERROR(Status
);
986 Creates the default map names for each device path in the system with
987 a protocol depending on the Type.
989 Creates the consistent map names for each device path in the system with
990 a protocol depending on the Type.
992 Note: This will reset all mappings in the system("map -r").
994 Also sets up the default path environment variable if Type is FileSystem.
996 @retval EFI_SUCCESS All map names were created sucessfully.
997 @retval EFI_NOT_FOUND No protocols were found in the system.
998 @return Error returned from gBS->LocateHandle().
1004 ShellCommandCreateInitialMappingsAndPaths(
1009 EFI_HANDLE
*HandleList
;
1011 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1012 CHAR16
*NewDefaultName
;
1013 CHAR16
*NewConsistName
;
1014 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1015 SHELL_MAP_LIST
*MapListNode
;
1020 // Reset the static members back to zero
1025 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1028 // First empty out the existing list.
1030 if (!IsListEmpty(&gShellMapList
.Link
)) {
1031 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1032 ; !IsListEmpty(&gShellMapList
.Link
)
1033 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1035 RemoveEntryList(&MapListNode
->Link
);
1036 FreePool(MapListNode
);
1041 // Find each handle with Simple File System
1043 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1044 if (HandleList
!= NULL
) {
1046 // Do a count of the handles
1048 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1051 // Get all Device Paths
1053 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1054 ASSERT(DevicePathList
!= NULL
);
1056 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1057 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1061 // Sort all DevicePaths
1063 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1065 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1067 // Assign new Mappings to all...
1069 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1071 // Get default name first
1073 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1074 ASSERT(NewDefaultName
!= NULL
);
1075 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1076 ASSERT_EFI_ERROR(Status
);
1077 FreePool(NewDefaultName
);
1080 // Now do consistent name
1082 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1083 if (NewConsistName
!= NULL
) {
1084 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1085 ASSERT_EFI_ERROR(Status
);
1086 FreePool(NewConsistName
);
1090 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1092 SHELL_FREE_NON_NULL(HandleList
);
1093 SHELL_FREE_NON_NULL(DevicePathList
);
1101 // Find each handle with Block Io
1103 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1104 if (HandleList
!= NULL
) {
1105 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1108 // Get all Device Paths
1110 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1111 ASSERT(DevicePathList
!= NULL
);
1113 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1114 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1118 // Sort all DevicePaths
1120 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1123 // Assign new Mappings to all...
1125 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1127 // Get default name first
1129 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1130 ASSERT(NewDefaultName
!= NULL
);
1131 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1132 ASSERT_EFI_ERROR(Status
);
1133 FreePool(NewDefaultName
);
1136 SHELL_FREE_NON_NULL(HandleList
);
1137 SHELL_FREE_NON_NULL(DevicePathList
);
1138 } else if (Count
== (UINTN
)-1) {
1139 return (EFI_NOT_FOUND
);
1142 return (EFI_SUCCESS
);
1146 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1148 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1150 @return a EFI_FILE_PROTOCOL* representing the same file.
1154 ConvertShellHandleToEfiFileProtocol(
1155 IN CONST SHELL_FILE_HANDLE Handle
1158 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1162 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1164 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1165 @param[in] Path The path to the file for verification.
1167 @return A SHELL_FILE_HANDLE representing the same file.
1168 @retval NULL There was not enough memory.
1172 ConvertEfiFileProtocolToShellHandle(
1173 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1174 IN CONST CHAR16
*Path
1177 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1178 BUFFER_LIST
*NewNode
;
1181 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1182 if (Buffer
== NULL
) {
1185 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1186 if (NewNode
== NULL
) {
1189 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1190 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1191 if (Buffer
->Path
== NULL
) {
1194 NewNode
->Buffer
= Buffer
;
1196 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1198 return ((SHELL_FILE_HANDLE
)(Handle
));
1202 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1204 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1206 @return A pointer to the path for the file.
1210 ShellFileHandleGetPath(
1211 IN CONST SHELL_FILE_HANDLE Handle
1216 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1217 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1218 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1220 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1221 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1228 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1230 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1232 @retval TRUE The item was removed.
1233 @retval FALSE The item was not found.
1237 ShellFileHandleRemove(
1238 IN CONST SHELL_FILE_HANDLE Handle
1243 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1244 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1245 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1247 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1248 RemoveEntryList(&Node
->Link
);
1249 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1250 SHELL_FREE_NON_NULL(Node
->Buffer
);
1251 SHELL_FREE_NON_NULL(Node
);
1259 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1261 This will NOT work on directories.
1263 If Handle is NULL, then ASSERT.
1265 @param[in] Handle the file handle
1267 @retval TRUE the position is at the end of the file
1268 @retval FALSE the position is not at the end of the file
1273 IN SHELL_FILE_HANDLE Handle
1276 EFI_FILE_INFO
*Info
;
1281 // ASSERT if Handle is NULL
1283 ASSERT(Handle
!= NULL
);
1285 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1286 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1287 ASSERT(Info
!= NULL
);
1288 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1294 if (Pos
== Info
->FileSize
) {
1306 Frees any BUFFER_LIST defined type.
1308 @param[in] List The BUFFER_LIST object to free.
1313 IN BUFFER_LIST
*List
1316 BUFFER_LIST
*BufferListEntry
;
1322 // enumerate through the buffer list and free all memory
1324 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1325 ; !IsListEmpty (&List
->Link
)
1326 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1328 RemoveEntryList(&BufferListEntry
->Link
);
1329 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1330 if (BufferListEntry
->Buffer
!= NULL
) {
1331 FreePool(BufferListEntry
->Buffer
);
1333 FreePool(BufferListEntry
);