2 Provides interface to shell internal functions for shell commands.
4 Copyright (c) 2009 - 2010, 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 UnicodeFileTag
= 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_SHELL_PROTOCOL
*gEfiShellProtocol
= NULL
;
38 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
= NULL
;
39 EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
= NULL
;
40 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*gDevPathToText
= NULL
;
41 SHELL_MAP_LIST gShellMapList
;
42 SHELL_MAP_LIST
*gShellCurDir
= NULL
;
44 CONST CHAR16
* SupportLevel
[] = {
52 Function to make sure that the global protocol pointers are valid.
53 must be called after constructor before accessing the pointers.
62 if (gEfiShellParametersProtocol
== NULL
) {
63 Status
= gBS
->OpenProtocol(gImageHandle
,
64 &gEfiShellParametersProtocolGuid
,
65 (VOID
**)&gEfiShellParametersProtocol
,
68 EFI_OPEN_PROTOCOL_GET_PROTOCOL
70 if (EFI_ERROR(Status
)) {
71 return (EFI_DEVICE_ERROR
);
74 if (gEfiShellProtocol
== NULL
) {
75 Status
= gBS
->LocateProtocol(&gEfiShellProtocolGuid
, NULL
, (VOID
**)&gEfiShellProtocol
);
76 if (EFI_ERROR(Status
)) {
77 return (EFI_DEVICE_ERROR
);
80 if (gUnicodeCollation
== NULL
) {
81 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
82 if (EFI_ERROR(Status
)) {
83 return (EFI_DEVICE_ERROR
);
86 if (gDevPathToText
== NULL
) {
87 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&gDevPathToText
);
88 if (EFI_ERROR(Status
)) {
89 return (EFI_DEVICE_ERROR
);
96 Constructor for the Shell Command library.
98 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
100 @param ImageHandle the image handle of the process
101 @param SystemTable the EFI System Table pointer
103 @retval EFI_SUCCESS the initialization was complete sucessfully
107 ShellCommandLibConstructor (
108 IN EFI_HANDLE ImageHandle
,
109 IN EFI_SYSTEM_TABLE
*SystemTable
113 InitializeListHead(&gShellMapList
.Link
);
114 InitializeListHead(&mCommandList
.Link
);
115 InitializeListHead(&mAliasList
.Link
);
116 InitializeListHead(&mScriptList
.Link
);
117 InitializeListHead(&mFileHandleList
.Link
);
120 mExitRequested
= FALSE
;
122 mProfileListSize
= 0;
125 if (gUnicodeCollation
== NULL
) {
126 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
127 if (EFI_ERROR(Status
)) {
128 return (EFI_DEVICE_ERROR
);
132 return (RETURN_SUCCESS
);
136 Destructor for the library. free any resources.
138 @param ImageHandle the image handle of the process
139 @param SystemTable the EFI System Table pointer
141 @retval RETURN_SUCCESS this function always returns success
145 ShellCommandLibDestructor (
146 IN EFI_HANDLE ImageHandle
,
147 IN EFI_SYSTEM_TABLE
*SystemTable
150 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
152 SCRIPT_FILE_LIST
*Node3
;
153 SHELL_MAP_LIST
*MapNode
;
155 // enumerate throught the list and free all the memory
157 while (!IsListEmpty (&mCommandList
.Link
)) {
158 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
159 RemoveEntryList(&Node
->Link
);
160 SHELL_FREE_NON_NULL(Node
->CommandString
);
162 DEBUG_CODE(Node
= NULL
;);
166 // enumerate through the init command list and free all memory
168 while (!IsListEmpty (&mAliasList
.Link
)) {
169 Node2
= (COMMAND_LIST
*)GetFirstNode(&mAliasList
.Link
);
170 RemoveEntryList(&Node2
->Link
);
171 SHELL_FREE_NON_NULL(Node2
->CommandString
);
173 DEBUG_CODE(Node2
= NULL
;);
177 // enumerate throught the list and free all the memory
179 while (!IsListEmpty (&mScriptList
.Link
)) {
180 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
181 RemoveEntryList(&Node3
->Link
);
182 DeleteScriptFileStruct(Node3
->Data
);
187 // enumerate throught the mappings list and free all the memory
189 if (!IsListEmpty(&gShellMapList
.Link
)) {
190 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
191 ; !IsListEmpty (&gShellMapList
.Link
)
192 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
194 ASSERT(MapNode
!= NULL
);
195 RemoveEntryList(&MapNode
->Link
);
196 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
197 SHELL_FREE_NON_NULL(MapNode
->MapName
);
198 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
202 if (!IsListEmpty(&mFileHandleList
.Link
)){
203 FreeBufferList(&mFileHandleList
);
206 if (mProfileList
!= NULL
) {
207 FreePool(mProfileList
);
210 return (RETURN_SUCCESS
);
214 Checks if a command is already on the list.
216 @param[in] CommandString The command string to check for on the list.
220 ShellCommandIsCommandOnList (
221 IN CONST CHAR16
*CommandString
224 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
227 // assert for NULL parameter
229 ASSERT(CommandString
!= NULL
);
232 // check for the command
234 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
235 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
236 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
238 ASSERT(Node
->CommandString
!= NULL
);
239 if (gUnicodeCollation
->StriColl(
241 (CHAR16
*)CommandString
,
242 Node
->CommandString
) == 0
251 Get the help text for a command.
253 @param[in] CommandString The command name.
255 @retval NULL No help text was found.
256 @return String of help text. Caller reuiqred to free.
260 ShellCommandGetCommandHelp (
261 IN CONST CHAR16
*CommandString
264 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
267 // assert for NULL parameter
269 ASSERT(CommandString
!= NULL
);
272 // check for the command
274 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
275 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
276 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
278 ASSERT(Node
->CommandString
!= NULL
);
279 if (gUnicodeCollation
->StriColl(
281 (CHAR16
*)CommandString
,
282 Node
->CommandString
) == 0
284 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
291 Registers handlers of type SHELL_RUN_COMMAND and
292 SHELL_GET_MAN_FILENAME for each shell command.
294 If the ShellSupportLevel is greater than the value of the
295 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
297 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
298 with the command specified by CommandString. If the command named by
299 CommandString has already been registered, then return
300 RETURN_ALREADY_STARTED.
302 If there are not enough resources available to register the handlers then
303 RETURN_OUT_OF_RESOURCES is returned.
305 If CommandString is NULL, then ASSERT().
306 If GetHelpInfoHandler is NULL, then ASSERT().
307 If CommandHandler is NULL, then ASSERT().
308 If ProfileName is NULL, then ASSERT().
310 @param[in] CommandString Pointer to the command name. This is the
311 name to look for on the command line in
313 @param[in] CommandHandler Pointer to a function that runs the
315 @param[in] GetManFileName Pointer to a function that provides man
317 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
319 @param[in] ProfileName profile name to require for support of this
321 @param[in] CanAffectLE indicates whether this command's return value
322 can change the LASTERROR environment variable.
323 @param[in] HiiHandle Handle of this command's HII entry.
324 @param[in] ManFormatHelp HII locator for the help text.
326 @retval RETURN_SUCCESS The handlers were registered.
327 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
328 register the shell command.
329 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
330 currently allowed support level.
331 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
332 is already registered. Only 1 handler set for
333 a given command is allowed.
334 @sa SHELL_GET_MAN_FILENAME
335 @sa SHELL_RUN_COMMAND
339 ShellCommandRegisterCommandName (
340 IN CONST CHAR16
*CommandString
,
341 IN SHELL_RUN_COMMAND CommandHandler
,
342 IN SHELL_GET_MAN_FILENAME GetManFileName
,
343 IN UINT32 ShellMinSupportLevel
,
344 IN CONST CHAR16
*ProfileName
,
345 IN CONST BOOLEAN CanAffectLE
,
346 IN CONST EFI_HANDLE HiiHandle
,
347 IN CONST EFI_STRING_ID ManFormatHelp
350 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
353 // ASSERTs for NULL parameters
355 ASSERT(CommandString
!= NULL
);
356 ASSERT(GetManFileName
!= NULL
);
357 ASSERT(CommandHandler
!= NULL
);
358 ASSERT(ProfileName
!= NULL
);
361 // check for shell support level
363 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
364 return (RETURN_UNSUPPORTED
);
368 // check for already on the list
370 if (ShellCommandIsCommandOnList(CommandString
)) {
371 return (RETURN_ALREADY_STARTED
);
375 // allocate memory for new struct
377 Node
= AllocatePool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
378 ASSERT(Node
!= NULL
);
379 Node
->CommandString
= AllocatePool(StrSize(CommandString
));
380 ASSERT(Node
->CommandString
!= NULL
);
383 // populate the new struct
385 StrCpy(Node
->CommandString
, CommandString
);
387 Node
->GetManFileName
= GetManFileName
;
388 Node
->CommandHandler
= CommandHandler
;
389 Node
->LastError
= CanAffectLE
;
390 Node
->HiiHandle
= HiiHandle
;
391 Node
->ManFormatHelp
= ManFormatHelp
;
393 if ( StrLen(ProfileName
)>0
394 && ((mProfileList
!= NULL
395 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
397 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
398 if (mProfileList
== NULL
) {
400 // If this is the first make a leading ';'
402 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
404 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
405 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
409 // add the new struct to the list
411 InsertTailList (&mCommandList
.Link
, &Node
->Link
);
413 return (RETURN_SUCCESS
);
417 Function to get the current Profile string.
419 @retval NULL There are no installed profiles.
420 @return A semi-colon delimited list of profiles.
424 ShellCommandGetProfileList (
428 return (mProfileList
);
432 Checks if a command string has been registered for CommandString and if so it runs
433 the previously registered handler for that command with the command line.
435 If CommandString is NULL, then ASSERT().
437 If Sections is specified, then each section name listed will be compared in a casesensitive
438 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
439 it will be appended to the returned help text. If the section does not exist, no
440 information will be returned. If Sections is NULL, then all help text information
441 available will be returned.
443 @param Sections pointer to string representing which section to get help on.
445 @param[in] CommandString Pointer to the command name. This is the name
446 found on the command line in the shell.
447 @param[in,out] RetVal Pointer to the return vaule from the command handler.
449 @param[in,out] CanAffectLE indicates whether this command's return value
450 needs to be placed into LASTERROR environment variable.
452 @retval RETURN_SUCCESS The handler was run.
453 @retval RETURN_NOT_FOUND The CommandString did not match a registered
455 @sa SHELL_RUN_COMMAND
459 ShellCommandRunCommandHandler (
460 IN CONST CHAR16
*CommandString
,
461 IN OUT SHELL_STATUS
*RetVal
,
462 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
465 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
468 // assert for NULL parameters
470 ASSERT(CommandString
!= NULL
);
473 // check for the command
475 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
476 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
477 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
479 ASSERT(Node
->CommandString
!= NULL
);
480 if (gUnicodeCollation
->StriColl(
482 (CHAR16
*)CommandString
,
483 Node
->CommandString
) == 0
485 if (CanAffectLE
!= NULL
) {
486 *CanAffectLE
= Node
->LastError
;
488 if (RetVal
!= NULL
) {
489 *RetVal
= Node
->CommandHandler(NULL
, gST
);
491 Node
->CommandHandler(NULL
, gST
);
493 return (RETURN_SUCCESS
);
496 return (RETURN_NOT_FOUND
);
500 Checks if a command string has been registered for CommandString and if so it
501 returns the MAN filename specified for that command.
503 If CommandString is NULL, then ASSERT().
505 @param[in] CommandString Pointer to the command name. This is the name
506 found on the command line in the shell.\
508 @retval NULL the commandString was not a registered command.
509 @return other the name of the MAN file.
510 @sa SHELL_GET_MAN_FILENAME
514 ShellCommandGetManFileNameHandler (
515 IN CONST CHAR16
*CommandString
518 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
521 // assert for NULL parameters
523 ASSERT(CommandString
!= NULL
);
526 // check for the command
528 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
529 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
530 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
532 ASSERT(Node
->CommandString
!= NULL
);
533 if (gUnicodeCollation
->StriColl(
535 (CHAR16
*)CommandString
,
536 Node
->CommandString
) == 0
538 return (Node
->GetManFileName());
545 Get the list of all available shell internal commands. This is a linked list
546 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
547 list functions. do not modify the values.
549 @return a Linked list of all available shell commands.
553 ShellCommandGetCommandList (
556 return ((COMMAND_LIST
*)(&mCommandList
));
560 Registers aliases to be set as part of the initialization of the shell application.
562 If Command is NULL, then ASSERT().
563 If Alias is NULL, then ASSERT().
565 @param[in] Command Pointer to the Command
566 @param[in] Alias Pointer to Alias
568 @retval RETURN_SUCCESS The handlers were registered.
569 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
570 register the shell command.
574 ShellCommandRegisterAlias (
575 IN CONST CHAR16
*Command
,
576 IN CONST CHAR16
*Alias
584 ASSERT(Command
!= NULL
);
585 ASSERT(Alias
!= NULL
);
588 // allocate memory for new struct
590 Node
= AllocatePool(sizeof(ALIAS_LIST
));
591 ASSERT(Node
!= NULL
);
592 Node
->CommandString
= AllocatePool(StrSize(Command
));
593 Node
->Alias
= AllocatePool(StrSize(Alias
));
594 ASSERT(Node
->CommandString
!= NULL
);
595 ASSERT(Node
->Alias
!= NULL
);
598 // populate the new struct
600 StrCpy(Node
->CommandString
, Command
);
601 StrCpy(Node
->Alias
, Alias
);
604 // add the new struct to the list
606 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
608 return (RETURN_SUCCESS
);
612 Get the list of all shell alias commands. This is a linked list
613 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
614 list functions. do not modify the values.
616 @return a Linked list of all requested shell alias'.
620 ShellCommandGetInitAliasList (
624 return (&mAliasList
);
628 Determine if a given alias is on the list of built in alias'
630 @param[in] Alias The alias to test for
632 @retval TRUE The alias is a built in alias
633 @retval FALSE The alias is not a built in alias
637 ShellCommandIsOnAliasList(
638 IN CONST CHAR16
*Alias
644 // assert for NULL parameter
646 ASSERT(Alias
!= NULL
);
649 // check for the Alias
651 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
652 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
653 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
655 ASSERT(Node
->CommandString
!= NULL
);
656 ASSERT(Node
->Alias
!= NULL
);
657 if (gUnicodeCollation
->StriColl(
660 Node
->CommandString
) == 0
664 if (gUnicodeCollation
->StriColl(
676 Function to determine current state of ECHO. Echo determins if lines from scripts
677 and ECHO commands are enabled.
679 @retval TRUE Echo is currently enabled
680 @retval FALSE Echo is currently disabled
684 ShellCommandGetEchoState(
692 Function to set current state of ECHO. Echo determins if lines from scripts
693 and ECHO commands are enabled.
695 If State is TRUE, Echo will be enabled.
696 If State is FALSE, Echo will be disabled.
700 ShellCommandSetEchoState(
708 Indicate that the current shell or script should exit.
710 @param[in] ScriptOnly TRUE if only exiting a script, FALSE othrwise.
714 ShellCommandRegisterExit (
715 IN BOOLEAN ScriptOnly
718 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
719 if (mExitRequested
) {
720 mExitScript
= ScriptOnly
;
727 Retrieve the Exit indicator.
729 @retval TRUE Exit was indicated.
730 @retval FALSE Exis was not indicated.
734 ShellCommandGetExit (
738 return (mExitRequested
);
742 Retrieve the Exit script indicator.
744 If ShellCommandGetExit returns FALSE than the return from this is undefined.
746 @retval TRUE ScriptOnly was indicated.
747 @retval FALSE ScriptOnly was not indicated.
751 ShellCommandGetScriptExit (
755 return (mExitScript
);
759 Function to cleanup all memory from a SCRIPT_FILE structure.
761 @param[in] Script The pointer to the structure to cleanup.
765 DeleteScriptFileStruct (
766 IN SCRIPT_FILE
*Script
770 ASSERT(Script
!= NULL
);
771 ASSERT(Script
->ScriptName
!= NULL
);
772 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
773 FreePool(Script
->Argv
[LoopVar
]);
775 if (Script
->Argv
!= NULL
) {
776 FreePool(Script
->Argv
);
778 Script
->CurrentCommand
= NULL
;
779 while (!IsListEmpty (&Script
->CommandList
)) {
780 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
781 if (Script
->CurrentCommand
!= NULL
) {
782 RemoveEntryList(&Script
->CurrentCommand
->Link
);
783 if (Script
->CurrentCommand
->Cl
!= NULL
) {
784 FreePool(Script
->CurrentCommand
->Cl
);
786 if (Script
->CurrentCommand
->Data
!= NULL
) {
787 FreePool(Script
->CurrentCommand
->Data
);
789 FreePool(Script
->CurrentCommand
);
792 FreePool(Script
->ScriptName
);
797 Function to return a pointer to the currently running script file object.
799 @retval NULL A script file is not currently running.
800 @return A pointer to the current script file object.
804 ShellCommandGetCurrentScriptFile (
808 SCRIPT_FILE_LIST
*List
;
809 if (IsListEmpty (&mScriptList
.Link
)) {
812 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
817 Function to set a new script as the currently running one.
819 This function will correctly stack and unstack nested scripts.
821 @param[in] Script Pointer to new script information structure. if NULL
822 will remove and de-allocate the top-most Script structure.
824 @return A pointer to the current running script file after this
825 change. NULL if removing the final script.
829 ShellCommandSetNewScript (
830 IN SCRIPT_FILE
*Script OPTIONAL
833 SCRIPT_FILE_LIST
*Node
;
834 if (Script
== NULL
) {
835 if (IsListEmpty (&mScriptList
.Link
)) {
839 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
840 RemoveEntryList(&Node
->Link
);
841 DeleteScriptFileStruct(Node
->Data
);
844 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
846 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
848 return (ShellCommandGetCurrentScriptFile());
852 Function to generate the next default mapping name.
854 If the return value is not NULL then it must be callee freed.
856 @param Type What kind of mapping name to make.
858 @retval NULL a memory allocation failed.
859 @return a new map name string
863 ShellCommandCreateNewMappingName(
864 IN CONST SHELL_MAPPING_TYPE Type
868 ASSERT(Type
< MappingTypeMax
);
872 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
875 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
876 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
877 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
883 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
885 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
886 default paths on the new map name...
888 Path should be FALSE when this function is called from the protocol SetMap function.
890 @param[in] Name The human readable mapped name.
891 @param[in] DevicePath The Device Path for this map.
892 @param[in] Flags The Flags attribute for this map item.
893 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
895 @retval EFI_SUCCESS The addition was sucessful.
896 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
897 @retval EFI_INVALID_PARAMETER A parameter was invalid.
901 ShellCommandAddMapItemAndUpdatePath(
902 IN CONST CHAR16
*Name
,
903 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
904 IN CONST UINT64 Flags
,
905 IN CONST BOOLEAN Path
909 SHELL_MAP_LIST
*MapListNode
;
910 CONST CHAR16
*OriginalPath
;
917 Status
= EFI_SUCCESS
;
919 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
920 if (MapListNode
== NULL
) {
921 Status
= EFI_OUT_OF_RESOURCES
;
923 MapListNode
->Flags
= Flags
;
924 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
925 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
926 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
927 Status
= EFI_OUT_OF_RESOURCES
;
929 StrCpy(MapListNode
->MapName
, Name
);
930 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
933 if (EFI_ERROR(Status
)) {
934 if (MapListNode
!= NULL
) {
935 if (MapListNode
->DevicePath
!= NULL
) {
936 FreePool(MapListNode
->DevicePath
);
938 if (MapListNode
->MapName
!= NULL
) {
939 FreePool(MapListNode
->MapName
);
941 FreePool(MapListNode
);
945 // Since there was no error and Path was TRUE
946 // Now add the correct path for that mapping
948 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
949 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
950 if (OriginalPath
!= NULL
) {
951 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
953 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
955 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
956 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
957 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
958 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
959 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
960 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
961 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
963 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
964 ASSERT_EFI_ERROR(Status
);
971 Creates the default map names for each device path in the system with
972 a protocol depending on the Type.
974 Creates the consistent map names for each device path in the system with
975 a protocol depending on the Type.
977 Note: This will reset all mappings in the system("map -r").
979 Also sets up the default path environment variable if Type is FileSystem.
981 @retval EFI_SUCCESS All map names were created sucessfully.
982 @retval EFI_NOT_FOUND No protocols were found in the system.
983 @return Error returned from gBS->LocateHandle().
989 ShellCommandCreateInitialMappingsAndPaths(
994 EFI_HANDLE
*HandleList
;
996 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
997 CHAR16
*NewDefaultName
;
998 CHAR16
*NewConsistName
;
999 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1000 SHELL_MAP_LIST
*MapListNode
;
1005 // Reset the static members back to zero
1010 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1013 // First empty out the existing list.
1015 if (!IsListEmpty(&gShellMapList
.Link
)) {
1016 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1017 ; !IsListEmpty(&gShellMapList
.Link
)
1018 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1020 RemoveEntryList(&MapListNode
->Link
);
1021 FreePool(MapListNode
);
1026 // Find each handle with Simple File System
1028 HandleList
= GetHandleListByPotocol(&gEfiSimpleFileSystemProtocolGuid
);
1029 if (HandleList
!= NULL
) {
1031 // Do a count of the handles
1033 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1036 // Get all Device Paths
1038 DevicePathList
= AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1039 ASSERT(DevicePathList
!= NULL
);
1041 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1042 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1046 // Sort all DevicePaths
1048 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1050 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1052 // Assign new Mappings to all...
1054 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1056 // Get default name first
1058 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1059 ASSERT(NewDefaultName
!= NULL
);
1060 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1061 ASSERT_EFI_ERROR(Status
);
1062 FreePool(NewDefaultName
);
1065 // Now do consistent name
1067 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1068 if (NewConsistName
!= NULL
) {
1069 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1070 ASSERT_EFI_ERROR(Status
);
1071 FreePool(NewConsistName
);
1075 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1077 SHELL_FREE_NON_NULL(HandleList
);
1078 SHELL_FREE_NON_NULL(DevicePathList
);
1086 // Find each handle with Block Io
1088 HandleList
= GetHandleListByPotocol(&gEfiBlockIoProtocolGuid
);
1089 if (HandleList
!= NULL
) {
1090 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1093 // Get all Device Paths
1095 DevicePathList
= AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1096 ASSERT(DevicePathList
!= NULL
);
1098 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1099 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1103 // Sort all DevicePaths
1105 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1108 // Assign new Mappings to all...
1110 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1112 // Get default name first
1114 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1115 ASSERT(NewDefaultName
!= NULL
);
1116 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1117 ASSERT_EFI_ERROR(Status
);
1118 FreePool(NewDefaultName
);
1121 SHELL_FREE_NON_NULL(HandleList
);
1122 SHELL_FREE_NON_NULL(DevicePathList
);
1123 } else if (Count
== (UINTN
)-1) {
1124 return (EFI_NOT_FOUND
);
1127 return (EFI_SUCCESS
);
1132 ShellCommandCleanPath (
1138 for (Path2
= Path
; Path2
!= NULL
&& *Path2
!= CHAR_NULL
; Path2
++) {
1139 if (*Path2
== L
'/') {
1148 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1150 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1152 @return a EFI_FILE_PROTOCOL* representing the same file.
1156 ConvertShellHandleToEfiFileProtocol(
1157 IN CONST SHELL_FILE_HANDLE Handle
1160 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1164 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1166 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1167 @param[in] Path The path to the file for verification.
1169 @return a SHELL_FILE_HANDLE representing the same file.
1173 ConvertEfiFileProtocolToShellHandle(
1174 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1175 IN CONST CHAR16
*Path
1178 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1179 BUFFER_LIST
*NewNode
;
1182 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1183 ASSERT(Buffer
!= NULL
);
1184 NewNode
= AllocatePool(sizeof(BUFFER_LIST
));
1185 ASSERT(NewNode
!= NULL
);
1186 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1187 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1188 NewNode
->Buffer
= Buffer
;
1190 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1192 return ((SHELL_FILE_HANDLE
)(Handle
));
1196 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1198 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1200 @return A pointer to the path for the file.
1204 ShellFileHandleGetPath(
1205 IN CONST SHELL_FILE_HANDLE Handle
1210 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1211 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1212 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1214 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1215 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1222 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.
1224 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1226 @retval TRUE The item was removed.
1227 @retval FALSE The item was not found.
1231 ShellFileHandleRemove(
1232 IN CONST SHELL_FILE_HANDLE Handle
1237 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1238 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1239 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1241 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1242 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1243 RemoveEntryList(&Node
->Link
);
1251 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1253 This will NOT work on directories.
1255 If Handle is NULL, then ASSERT.
1257 @param[in] Handle the file handle
1259 @retval TRUE the position is at the end of the file
1260 @retval FALSE the position is not at the end of the file
1265 IN SHELL_FILE_HANDLE Handle
1268 EFI_FILE_INFO
*Info
;
1273 // ASSERT if Handle is NULL
1275 ASSERT(Handle
!= NULL
);
1277 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1278 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1279 ASSERT(Info
!= NULL
);
1280 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1286 if (Pos
== Info
->FileSize
) {
1298 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
1299 buffer. The returned buffer must be callee freed.
1301 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1302 maintained and not changed for all operations with the same file.
1304 @param[in] Handle SHELL_FILE_HANDLE to read from.
1305 @param[in,out] Ascii Boolean value for indicating whether the file is
1306 Ascii (TRUE) or UCS2 (FALSE).
1308 @return The line of text from the file.
1310 @sa ShellFileHandleReadLine
1314 ShellFileHandleReturnLine(
1315 IN SHELL_FILE_HANDLE Handle
,
1316 IN OUT BOOLEAN
*Ascii
1326 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
1327 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1328 RetVal
= AllocatePool(Size
);
1329 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
1331 ASSERT_EFI_ERROR(Status
);
1332 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
1340 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
1342 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1343 maintained and not changed for all operations with the same file.
1345 @param[in] Handle SHELL_FILE_HANDLE to read from.
1346 @param[in,out] Buffer The pointer to buffer to read into.
1347 @param[in,out] Size The pointer to number of bytes in Buffer.
1348 @param[in] Truncate If the buffer is large enough, this has no effect.
1349 If the buffer is is too small and Truncate is TRUE,
1350 the line will be truncated.
1351 If the buffer is is too small and Truncate is FALSE,
1352 then no read will occur.
1354 @param[in,out] Ascii Boolean value for indicating whether the file is
1355 Ascii (TRUE) or UCS2 (FALSE).
1357 @retval EFI_SUCCESS The operation was successful. The line is stored in
1359 @retval EFI_INVALID_PARAMETER Handle was NULL.
1360 @retval EFI_INVALID_PARAMETER Size was NULL.
1361 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
1362 Size was updated to the minimum space required.
1363 @sa ShellFileHandleRead
1367 ShellFileHandleReadLine(
1368 IN SHELL_FILE_HANDLE Handle
,
1369 IN OUT CHAR16
*Buffer
,
1371 IN BOOLEAN Truncate
,
1372 IN OUT BOOLEAN
*Ascii
1379 UINT64 OriginalFilePosition
;
1385 return (EFI_INVALID_PARAMETER
);
1387 if (Buffer
== NULL
) {
1390 *Buffer
= CHAR_NULL
;
1392 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
1393 if (OriginalFilePosition
== 0) {
1394 CharSize
= sizeof(CHAR16
);
1395 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
1396 ASSERT_EFI_ERROR(Status
);
1397 if (CharBuffer
== UnicodeFileTag
) {
1401 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
1405 for (CountSoFar
= 0;;CountSoFar
++){
1408 CharSize
= sizeof(CHAR8
);
1410 CharSize
= sizeof(CHAR16
);
1412 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
1413 if ( EFI_ERROR(Status
)
1415 || (CharBuffer
== L
'\n' && !(*Ascii
))
1416 || (CharBuffer
== '\n' && *Ascii
)
1421 // if we have space save it...
1423 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
1424 ASSERT(Buffer
!= NULL
);
1425 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
1426 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
1431 // if we ran out of space tell when...
1433 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
1434 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
1436 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
1438 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
1440 return (EFI_BUFFER_TOO_SMALL
);
1442 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
1443 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
1449 Frees any BUFFER_LIST defined type.
1454 IN BUFFER_LIST
*List
1457 BUFFER_LIST
*BufferListEntry
;
1463 // enumerate through the buffer list and free all memory
1465 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1466 ; !IsListEmpty (&List
->Link
)
1467 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1469 RemoveEntryList(&BufferListEntry
->Link
);
1470 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1471 if (BufferListEntry
->Buffer
!= NULL
) {
1472 FreePool(BufferListEntry
->Buffer
);
1474 FreePool(BufferListEntry
);
1479 Chops off last directory or file entry in a path leaving the trailing slash
1483 @retval FALSE No directory was found to chop off.
1484 @retval TRUE A directory was chopped off.
1489 IN OUT CHAR16
*PathToReturn
1493 CHAR16
*LastSlash
= NULL
;
1495 // get directory name from path... ('chop' off extra)
1497 for ( Walker
= PathToReturn
1498 ; Walker
!= NULL
&& *Walker
!= CHAR_NULL
1501 if (*Walker
== L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
1502 LastSlash
= Walker
+1;
1505 if (LastSlash
!= NULL
) {
1506 *LastSlash
= CHAR_NULL
;