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
771 if (Script
== NULL
) {
775 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
776 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
778 if (Script
->Argv
!= NULL
) {
779 SHELL_FREE_NON_NULL(Script
->Argv
);
781 Script
->CurrentCommand
= NULL
;
782 while (!IsListEmpty (&Script
->CommandList
)) {
783 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
784 if (Script
->CurrentCommand
!= NULL
) {
785 RemoveEntryList(&Script
->CurrentCommand
->Link
);
786 if (Script
->CurrentCommand
->Cl
!= NULL
) {
787 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
789 if (Script
->CurrentCommand
->Data
!= NULL
) {
790 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
792 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
795 SHELL_FREE_NON_NULL(Script
->ScriptName
);
796 SHELL_FREE_NON_NULL(Script
);
800 Function to return a pointer to the currently running script file object.
802 @retval NULL A script file is not currently running.
803 @return A pointer to the current script file object.
807 ShellCommandGetCurrentScriptFile (
811 SCRIPT_FILE_LIST
*List
;
812 if (IsListEmpty (&mScriptList
.Link
)) {
815 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
820 Function to set a new script as the currently running one.
822 This function will correctly stack and unstack nested scripts.
824 @param[in] Script Pointer to new script information structure. if NULL
825 will remove and de-allocate the top-most Script structure.
827 @return A pointer to the current running script file after this
828 change. NULL if removing the final script.
832 ShellCommandSetNewScript (
833 IN SCRIPT_FILE
*Script OPTIONAL
836 SCRIPT_FILE_LIST
*Node
;
837 if (Script
== NULL
) {
838 if (IsListEmpty (&mScriptList
.Link
)) {
841 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
842 RemoveEntryList(&Node
->Link
);
843 DeleteScriptFileStruct(Node
->Data
);
846 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
851 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
853 return (ShellCommandGetCurrentScriptFile());
857 Function to generate the next default mapping name.
859 If the return value is not NULL then it must be callee freed.
861 @param Type What kind of mapping name to make.
863 @retval NULL a memory allocation failed.
864 @return a new map name string
868 ShellCommandCreateNewMappingName(
869 IN CONST SHELL_MAPPING_TYPE Type
873 ASSERT(Type
< MappingTypeMax
);
877 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
880 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
881 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
882 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
888 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
890 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
891 default paths on the new map name...
893 Path should be FALSE when this function is called from the protocol SetMap function.
895 @param[in] Name The human readable mapped name.
896 @param[in] DevicePath The Device Path for this map.
897 @param[in] Flags The Flags attribute for this map item.
898 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
900 @retval EFI_SUCCESS The addition was sucessful.
901 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
902 @retval EFI_INVALID_PARAMETER A parameter was invalid.
906 ShellCommandAddMapItemAndUpdatePath(
907 IN CONST CHAR16
*Name
,
908 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
909 IN CONST UINT64 Flags
,
910 IN CONST BOOLEAN Path
914 SHELL_MAP_LIST
*MapListNode
;
915 CONST CHAR16
*OriginalPath
;
922 Status
= EFI_SUCCESS
;
924 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
925 if (MapListNode
== NULL
) {
926 Status
= EFI_OUT_OF_RESOURCES
;
928 MapListNode
->Flags
= Flags
;
929 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
930 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
931 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
932 Status
= EFI_OUT_OF_RESOURCES
;
934 StrCpy(MapListNode
->MapName
, Name
);
935 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
938 if (EFI_ERROR(Status
)) {
939 if (MapListNode
!= NULL
) {
940 if (MapListNode
->DevicePath
!= NULL
) {
941 FreePool(MapListNode
->DevicePath
);
943 if (MapListNode
->MapName
!= NULL
) {
944 FreePool(MapListNode
->MapName
);
946 FreePool(MapListNode
);
950 // Since there was no error and Path was TRUE
951 // Now add the correct path for that mapping
953 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
954 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
955 if (OriginalPath
!= NULL
) {
956 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
958 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
960 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
961 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
962 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
963 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
964 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
965 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
966 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
968 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
969 ASSERT_EFI_ERROR(Status
);
976 Creates the default map names for each device path in the system with
977 a protocol depending on the Type.
979 Creates the consistent map names for each device path in the system with
980 a protocol depending on the Type.
982 Note: This will reset all mappings in the system("map -r").
984 Also sets up the default path environment variable if Type is FileSystem.
986 @retval EFI_SUCCESS All map names were created sucessfully.
987 @retval EFI_NOT_FOUND No protocols were found in the system.
988 @return Error returned from gBS->LocateHandle().
994 ShellCommandCreateInitialMappingsAndPaths(
999 EFI_HANDLE
*HandleList
;
1001 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1002 CHAR16
*NewDefaultName
;
1003 CHAR16
*NewConsistName
;
1004 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1005 SHELL_MAP_LIST
*MapListNode
;
1010 // Reset the static members back to zero
1015 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1018 // First empty out the existing list.
1020 if (!IsListEmpty(&gShellMapList
.Link
)) {
1021 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1022 ; !IsListEmpty(&gShellMapList
.Link
)
1023 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1025 RemoveEntryList(&MapListNode
->Link
);
1026 FreePool(MapListNode
);
1031 // Find each handle with Simple File System
1033 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1034 if (HandleList
!= NULL
) {
1036 // Do a count of the handles
1038 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1041 // Get all Device Paths
1043 DevicePathList
= AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1044 ASSERT(DevicePathList
!= NULL
);
1046 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1047 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1051 // Sort all DevicePaths
1053 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1055 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1057 // Assign new Mappings to all...
1059 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1061 // Get default name first
1063 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1064 ASSERT(NewDefaultName
!= NULL
);
1065 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1066 ASSERT_EFI_ERROR(Status
);
1067 FreePool(NewDefaultName
);
1070 // Now do consistent name
1072 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1073 if (NewConsistName
!= NULL
) {
1074 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1075 ASSERT_EFI_ERROR(Status
);
1076 FreePool(NewConsistName
);
1080 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1082 SHELL_FREE_NON_NULL(HandleList
);
1083 SHELL_FREE_NON_NULL(DevicePathList
);
1091 // Find each handle with Block Io
1093 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1094 if (HandleList
!= NULL
) {
1095 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1098 // Get all Device Paths
1100 DevicePathList
= AllocatePool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1101 ASSERT(DevicePathList
!= NULL
);
1103 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1104 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1108 // Sort all DevicePaths
1110 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1113 // Assign new Mappings to all...
1115 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1117 // Get default name first
1119 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1120 ASSERT(NewDefaultName
!= NULL
);
1121 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1122 ASSERT_EFI_ERROR(Status
);
1123 FreePool(NewDefaultName
);
1126 SHELL_FREE_NON_NULL(HandleList
);
1127 SHELL_FREE_NON_NULL(DevicePathList
);
1128 } else if (Count
== (UINTN
)-1) {
1129 return (EFI_NOT_FOUND
);
1132 return (EFI_SUCCESS
);
1137 ShellCommandCleanPath (
1143 for (Path2
= Path
; Path2
!= NULL
&& *Path2
!= CHAR_NULL
; Path2
++) {
1144 if (*Path2
== L
'/') {
1153 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1155 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1157 @return a EFI_FILE_PROTOCOL* representing the same file.
1161 ConvertShellHandleToEfiFileProtocol(
1162 IN CONST SHELL_FILE_HANDLE Handle
1165 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1169 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1171 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1172 @param[in] Path The path to the file for verification.
1174 @return A SHELL_FILE_HANDLE representing the same file.
1175 @retval NULL There was not enough memory.
1179 ConvertEfiFileProtocolToShellHandle(
1180 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1181 IN CONST CHAR16
*Path
1184 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1185 BUFFER_LIST
*NewNode
;
1188 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1189 if (Buffer
== NULL
) {
1192 NewNode
= AllocatePool(sizeof(BUFFER_LIST
));
1193 if (NewNode
== NULL
) {
1196 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1197 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1198 if (Buffer
->Path
== NULL
) {
1201 NewNode
->Buffer
= Buffer
;
1203 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1205 return ((SHELL_FILE_HANDLE
)(Handle
));
1209 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1211 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1213 @return A pointer to the path for the file.
1217 ShellFileHandleGetPath(
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 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1235 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.
1237 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1239 @retval TRUE The item was removed.
1240 @retval FALSE The item was not found.
1244 ShellFileHandleRemove(
1245 IN CONST SHELL_FILE_HANDLE Handle
1250 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1251 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1252 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1254 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1255 RemoveEntryList(&Node
->Link
);
1256 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1257 SHELL_FREE_NON_NULL(Node
->Buffer
);
1258 SHELL_FREE_NON_NULL(Node
);
1266 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1268 This will NOT work on directories.
1270 If Handle is NULL, then ASSERT.
1272 @param[in] Handle the file handle
1274 @retval TRUE the position is at the end of the file
1275 @retval FALSE the position is not at the end of the file
1280 IN SHELL_FILE_HANDLE Handle
1283 EFI_FILE_INFO
*Info
;
1288 // ASSERT if Handle is NULL
1290 ASSERT(Handle
!= NULL
);
1292 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1293 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1294 ASSERT(Info
!= NULL
);
1295 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1301 if (Pos
== Info
->FileSize
) {
1313 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
1314 buffer. The returned buffer must be callee freed.
1316 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1317 maintained and not changed for all operations with the same file.
1319 @param[in] Handle SHELL_FILE_HANDLE to read from.
1320 @param[in,out] Ascii Boolean value for indicating whether the file is
1321 Ascii (TRUE) or UCS2 (FALSE).
1323 @return The line of text from the file.
1325 @sa ShellFileHandleReadLine
1329 ShellFileHandleReturnLine(
1330 IN SHELL_FILE_HANDLE Handle
,
1331 IN OUT BOOLEAN
*Ascii
1341 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
1342 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1343 RetVal
= AllocatePool(Size
);
1344 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
1346 ASSERT_EFI_ERROR(Status
);
1347 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
1355 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
1357 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1358 maintained and not changed for all operations with the same file.
1360 @param[in] Handle SHELL_FILE_HANDLE to read from.
1361 @param[in,out] Buffer The pointer to buffer to read into.
1362 @param[in,out] Size The pointer to number of bytes in Buffer.
1363 @param[in] Truncate If the buffer is large enough, this has no effect.
1364 If the buffer is is too small and Truncate is TRUE,
1365 the line will be truncated.
1366 If the buffer is is too small and Truncate is FALSE,
1367 then no read will occur.
1369 @param[in,out] Ascii Boolean value for indicating whether the file is
1370 Ascii (TRUE) or UCS2 (FALSE).
1372 @retval EFI_SUCCESS The operation was successful. The line is stored in
1374 @retval EFI_INVALID_PARAMETER Handle was NULL.
1375 @retval EFI_INVALID_PARAMETER Size was NULL.
1376 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
1377 Size was updated to the minimum space required.
1378 @sa ShellFileHandleRead
1382 ShellFileHandleReadLine(
1383 IN SHELL_FILE_HANDLE Handle
,
1384 IN OUT CHAR16
*Buffer
,
1386 IN BOOLEAN Truncate
,
1387 IN OUT BOOLEAN
*Ascii
1394 UINT64 OriginalFilePosition
;
1400 return (EFI_INVALID_PARAMETER
);
1402 if (Buffer
== NULL
) {
1405 *Buffer
= CHAR_NULL
;
1407 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
1408 if (OriginalFilePosition
== 0) {
1409 CharSize
= sizeof(CHAR16
);
1410 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
1411 ASSERT_EFI_ERROR(Status
);
1412 if (CharBuffer
== UnicodeFileTag
) {
1416 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
1420 for (CountSoFar
= 0;;CountSoFar
++){
1423 CharSize
= sizeof(CHAR8
);
1425 CharSize
= sizeof(CHAR16
);
1427 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
1428 if ( EFI_ERROR(Status
)
1430 || (CharBuffer
== L
'\n' && !(*Ascii
))
1431 || (CharBuffer
== '\n' && *Ascii
)
1436 // if we have space save it...
1438 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
1439 ASSERT(Buffer
!= NULL
);
1440 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
1441 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
1446 // if we ran out of space tell when...
1448 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
1449 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
1451 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
1453 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
1455 return (EFI_BUFFER_TOO_SMALL
);
1457 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
1458 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
1464 Frees any BUFFER_LIST defined type.
1469 IN BUFFER_LIST
*List
1472 BUFFER_LIST
*BufferListEntry
;
1478 // enumerate through the buffer list and free all memory
1480 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1481 ; !IsListEmpty (&List
->Link
)
1482 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1484 RemoveEntryList(&BufferListEntry
->Link
);
1485 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1486 if (BufferListEntry
->Buffer
!= NULL
) {
1487 FreePool(BufferListEntry
->Buffer
);
1489 FreePool(BufferListEntry
);
1494 Chops off last directory or file entry in a path leaving the trailing slash
1498 @retval FALSE No directory was found to chop off.
1499 @retval TRUE A directory was chopped off.
1504 IN OUT CHAR16
*PathToReturn
1508 CHAR16
*LastSlash
= NULL
;
1510 // get directory name from path... ('chop' off extra)
1512 for ( Walker
= PathToReturn
1513 ; Walker
!= NULL
&& *Walker
!= CHAR_NULL
1516 if (*Walker
== L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
1517 LastSlash
= Walker
+1;
1520 if (LastSlash
!= NULL
) {
1521 *LastSlash
= CHAR_NULL
;