2 Provides interface to shell internal functions for shell commands.
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellCommandLib.h"
17 /// The tag for use in identifying UNICODE files.
18 /// If the file is UNICODE, the first 16 bits of the file will equal this value.
20 gUnicodeFileTag
= 0xFEFF
23 // STATIC local variables
24 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList
;
25 STATIC SCRIPT_FILE_LIST mScriptList
;
26 STATIC ALIAS_LIST mAliasList
;
27 STATIC BOOLEAN mEchoState
;
28 STATIC BOOLEAN mExitRequested
;
29 STATIC BOOLEAN mExitScript
;
30 STATIC CHAR16
*mProfileList
;
31 STATIC UINTN mProfileListSize
;
32 STATIC UINTN mFsMaxCount
= 0;
33 STATIC UINTN mBlkMaxCount
= 0;
34 STATIC BUFFER_LIST mFileHandleList
;
36 // global variables required by library class.
37 EFI_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 gEfiShellProtocol
= NULL
;
211 gEfiShellParametersProtocol
= NULL
;
212 gUnicodeCollation
= NULL
;
213 gDevPathToText
= NULL
;
216 return (RETURN_SUCCESS
);
220 Checks if a command is already on the list.
222 @param[in] CommandString The command string to check for on the list.
226 ShellCommandIsCommandOnList (
227 IN CONST CHAR16
*CommandString
230 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
233 // assert for NULL parameter
235 ASSERT(CommandString
!= NULL
);
238 // check for the command
240 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
241 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
242 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
244 ASSERT(Node
->CommandString
!= NULL
);
245 if (gUnicodeCollation
->StriColl(
247 (CHAR16
*)CommandString
,
248 Node
->CommandString
) == 0
257 Get the help text for a command.
259 @param[in] CommandString The command name.
261 @retval NULL No help text was found.
262 @return String of help text. Caller reuiqred to free.
266 ShellCommandGetCommandHelp (
267 IN CONST CHAR16
*CommandString
270 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
273 // assert for NULL parameter
275 ASSERT(CommandString
!= NULL
);
278 // check for the command
280 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
281 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
282 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
284 ASSERT(Node
->CommandString
!= NULL
);
285 if (gUnicodeCollation
->StriColl(
287 (CHAR16
*)CommandString
,
288 Node
->CommandString
) == 0
290 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
297 Registers handlers of type SHELL_RUN_COMMAND and
298 SHELL_GET_MAN_FILENAME for each shell command.
300 If the ShellSupportLevel is greater than the value of the
301 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
303 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
304 with the command specified by CommandString. If the command named by
305 CommandString has already been registered, then return
306 RETURN_ALREADY_STARTED.
308 If there are not enough resources available to register the handlers then
309 RETURN_OUT_OF_RESOURCES is returned.
311 If CommandString is NULL, then ASSERT().
312 If GetHelpInfoHandler is NULL, then ASSERT().
313 If CommandHandler is NULL, then ASSERT().
314 If ProfileName is NULL, then ASSERT().
316 @param[in] CommandString Pointer to the command name. This is the
317 name to look for on the command line in
319 @param[in] CommandHandler Pointer to a function that runs the
321 @param[in] GetManFileName Pointer to a function that provides man
323 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
325 @param[in] ProfileName profile name to require for support of this
327 @param[in] CanAffectLE indicates whether this command's return value
328 can change the LASTERROR environment variable.
329 @param[in] HiiHandle Handle of this command's HII entry.
330 @param[in] ManFormatHelp HII locator for the help text.
332 @retval RETURN_SUCCESS The handlers were registered.
333 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
334 register the shell command.
335 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
336 currently allowed support level.
337 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
338 is already registered. Only 1 handler set for
339 a given command is allowed.
340 @sa SHELL_GET_MAN_FILENAME
341 @sa SHELL_RUN_COMMAND
345 ShellCommandRegisterCommandName (
346 IN CONST CHAR16
*CommandString
,
347 IN SHELL_RUN_COMMAND CommandHandler
,
348 IN SHELL_GET_MAN_FILENAME GetManFileName
,
349 IN UINT32 ShellMinSupportLevel
,
350 IN CONST CHAR16
*ProfileName
,
351 IN CONST BOOLEAN CanAffectLE
,
352 IN CONST EFI_HANDLE HiiHandle
,
353 IN CONST EFI_STRING_ID ManFormatHelp
356 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
359 // ASSERTs for NULL parameters
361 ASSERT(CommandString
!= NULL
);
362 ASSERT(GetManFileName
!= NULL
);
363 ASSERT(CommandHandler
!= NULL
);
364 ASSERT(ProfileName
!= NULL
);
367 // check for shell support level
369 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
370 return (RETURN_UNSUPPORTED
);
374 // check for already on the list
376 if (ShellCommandIsCommandOnList(CommandString
)) {
377 return (RETURN_ALREADY_STARTED
);
381 // allocate memory for new struct
383 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
384 ASSERT(Node
!= NULL
);
385 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
386 ASSERT(Node
->CommandString
!= NULL
);
389 // populate the new struct
391 StrCpy(Node
->CommandString
, CommandString
);
393 Node
->GetManFileName
= GetManFileName
;
394 Node
->CommandHandler
= CommandHandler
;
395 Node
->LastError
= CanAffectLE
;
396 Node
->HiiHandle
= HiiHandle
;
397 Node
->ManFormatHelp
= ManFormatHelp
;
399 if ( StrLen(ProfileName
)>0
400 && ((mProfileList
!= NULL
401 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
403 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
404 if (mProfileList
== NULL
) {
406 // If this is the first make a leading ';'
408 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
410 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
411 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
415 // add the new struct to the list
417 InsertTailList (&mCommandList
.Link
, &Node
->Link
);
419 return (RETURN_SUCCESS
);
423 Function to get the current Profile string.
425 @retval NULL There are no installed profiles.
426 @return A semi-colon delimited list of profiles.
430 ShellCommandGetProfileList (
434 return (mProfileList
);
438 Checks if a command string has been registered for CommandString and if so it runs
439 the previously registered handler for that command with the command line.
441 If CommandString is NULL, then ASSERT().
443 If Sections is specified, then each section name listed will be compared in a casesensitive
444 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
445 it will be appended to the returned help text. If the section does not exist, no
446 information will be returned. If Sections is NULL, then all help text information
447 available will be returned.
449 @param[in] CommandString Pointer to the command name. This is the name
450 found on the command line in the shell.
451 @param[in,out] RetVal Pointer to the return vaule from the command handler.
453 @param[in,out] CanAffectLE indicates whether this command's return value
454 needs to be placed into LASTERROR environment variable.
456 @retval RETURN_SUCCESS The handler was run.
457 @retval RETURN_NOT_FOUND The CommandString did not match a registered
459 @sa SHELL_RUN_COMMAND
463 ShellCommandRunCommandHandler (
464 IN CONST CHAR16
*CommandString
,
465 IN OUT SHELL_STATUS
*RetVal
,
466 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
469 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
472 // assert for NULL parameters
474 ASSERT(CommandString
!= NULL
);
477 // check for the command
479 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
480 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
481 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
483 ASSERT(Node
->CommandString
!= NULL
);
484 if (gUnicodeCollation
->StriColl(
486 (CHAR16
*)CommandString
,
487 Node
->CommandString
) == 0
489 if (CanAffectLE
!= NULL
) {
490 *CanAffectLE
= Node
->LastError
;
492 if (RetVal
!= NULL
) {
493 *RetVal
= Node
->CommandHandler(NULL
, gST
);
495 Node
->CommandHandler(NULL
, gST
);
497 return (RETURN_SUCCESS
);
500 return (RETURN_NOT_FOUND
);
504 Checks if a command string has been registered for CommandString and if so it
505 returns the MAN filename specified for that command.
507 If CommandString is NULL, then ASSERT().
509 @param[in] CommandString Pointer to the command name. This is the name
510 found on the command line in the shell.\
512 @retval NULL the commandString was not a registered command.
513 @return other the name of the MAN file.
514 @sa SHELL_GET_MAN_FILENAME
518 ShellCommandGetManFileNameHandler (
519 IN CONST CHAR16
*CommandString
522 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
525 // assert for NULL parameters
527 ASSERT(CommandString
!= NULL
);
530 // check for the command
532 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
533 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
534 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
536 ASSERT(Node
->CommandString
!= NULL
);
537 if (gUnicodeCollation
->StriColl(
539 (CHAR16
*)CommandString
,
540 Node
->CommandString
) == 0
542 return (Node
->GetManFileName());
549 Get the list of all available shell internal commands. This is a linked list
550 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
551 list functions. do not modify the values.
553 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
555 @return a Linked list of all available shell commands.
559 ShellCommandGetCommandList (
560 IN CONST BOOLEAN Sort
564 // return ((COMMAND_LIST*)(&mCommandList));
566 return ((COMMAND_LIST
*)(&mCommandList
));
570 Registers aliases to be set as part of the initialization of the shell application.
572 If Command is NULL, then ASSERT().
573 If Alias is NULL, then ASSERT().
575 @param[in] Command Pointer to the Command
576 @param[in] Alias Pointer to Alias
578 @retval RETURN_SUCCESS The handlers were registered.
579 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
580 register the shell command.
584 ShellCommandRegisterAlias (
585 IN CONST CHAR16
*Command
,
586 IN CONST CHAR16
*Alias
594 ASSERT(Command
!= NULL
);
595 ASSERT(Alias
!= NULL
);
598 // allocate memory for new struct
600 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
601 ASSERT(Node
!= NULL
);
602 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
603 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
604 ASSERT(Node
->CommandString
!= NULL
);
605 ASSERT(Node
->Alias
!= NULL
);
608 // populate the new struct
610 StrCpy(Node
->CommandString
, Command
);
611 StrCpy(Node
->Alias
, Alias
);
614 // add the new struct to the list
616 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
618 return (RETURN_SUCCESS
);
622 Get the list of all shell alias commands. This is a linked list
623 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
624 list functions. do not modify the values.
626 @return a Linked list of all requested shell alias'.
630 ShellCommandGetInitAliasList (
634 return (&mAliasList
);
638 Determine if a given alias is on the list of built in alias'.
640 @param[in] Alias The alias to test for
642 @retval TRUE The alias is a built in alias
643 @retval FALSE The alias is not a built in alias
647 ShellCommandIsOnAliasList(
648 IN CONST CHAR16
*Alias
654 // assert for NULL parameter
656 ASSERT(Alias
!= NULL
);
659 // check for the Alias
661 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
662 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
663 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
665 ASSERT(Node
->CommandString
!= NULL
);
666 ASSERT(Node
->Alias
!= NULL
);
667 if (gUnicodeCollation
->StriColl(
670 Node
->CommandString
) == 0
674 if (gUnicodeCollation
->StriColl(
686 Function to determine current state of ECHO. Echo determins if lines from scripts
687 and ECHO commands are enabled.
689 @retval TRUE Echo is currently enabled
690 @retval FALSE Echo is currently disabled
694 ShellCommandGetEchoState(
702 Function to set current state of ECHO. Echo determins if lines from scripts
703 and ECHO commands are enabled.
705 If State is TRUE, Echo will be enabled.
706 If State is FALSE, Echo will be disabled.
708 @param[in] State How to set echo.
712 ShellCommandSetEchoState(
720 Indicate that the current shell or script should exit.
722 @param[in] ScriptOnly TRUE if only exiting a script, FALSE othrwise.
726 ShellCommandRegisterExit (
727 IN BOOLEAN ScriptOnly
730 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
731 if (mExitRequested
) {
732 mExitScript
= ScriptOnly
;
739 Retrieve the Exit indicator.
741 @retval TRUE Exit was indicated.
742 @retval FALSE Exis was not indicated.
746 ShellCommandGetExit (
750 return (mExitRequested
);
754 Retrieve the Exit script indicator.
756 If ShellCommandGetExit returns FALSE than the return from this is undefined.
758 @retval TRUE ScriptOnly was indicated.
759 @retval FALSE ScriptOnly was not indicated.
763 ShellCommandGetScriptExit (
767 return (mExitScript
);
771 Function to cleanup all memory from a SCRIPT_FILE structure.
773 @param[in] Script The pointer to the structure to cleanup.
777 DeleteScriptFileStruct (
778 IN SCRIPT_FILE
*Script
783 if (Script
== NULL
) {
787 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
788 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
790 if (Script
->Argv
!= NULL
) {
791 SHELL_FREE_NON_NULL(Script
->Argv
);
793 Script
->CurrentCommand
= NULL
;
794 while (!IsListEmpty (&Script
->CommandList
)) {
795 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
796 if (Script
->CurrentCommand
!= NULL
) {
797 RemoveEntryList(&Script
->CurrentCommand
->Link
);
798 if (Script
->CurrentCommand
->Cl
!= NULL
) {
799 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
801 if (Script
->CurrentCommand
->Data
!= NULL
) {
802 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
804 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
807 SHELL_FREE_NON_NULL(Script
->ScriptName
);
808 SHELL_FREE_NON_NULL(Script
);
812 Function to return a pointer to the currently running script file object.
814 @retval NULL A script file is not currently running.
815 @return A pointer to the current script file object.
819 ShellCommandGetCurrentScriptFile (
823 SCRIPT_FILE_LIST
*List
;
824 if (IsListEmpty (&mScriptList
.Link
)) {
827 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
832 Function to set a new script as the currently running one.
834 This function will correctly stack and unstack nested scripts.
836 @param[in] Script Pointer to new script information structure. if NULL
837 will remove and de-allocate the top-most Script structure.
839 @return A pointer to the current running script file after this
840 change. NULL if removing the final script.
844 ShellCommandSetNewScript (
845 IN SCRIPT_FILE
*Script OPTIONAL
848 SCRIPT_FILE_LIST
*Node
;
849 if (Script
== NULL
) {
850 if (IsListEmpty (&mScriptList
.Link
)) {
853 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
854 RemoveEntryList(&Node
->Link
);
855 DeleteScriptFileStruct(Node
->Data
);
858 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
863 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
865 return (ShellCommandGetCurrentScriptFile());
869 Function to generate the next default mapping name.
871 If the return value is not NULL then it must be callee freed.
873 @param Type What kind of mapping name to make.
875 @retval NULL a memory allocation failed.
876 @return a new map name string
880 ShellCommandCreateNewMappingName(
881 IN CONST SHELL_MAPPING_TYPE Type
885 ASSERT(Type
< MappingTypeMax
);
889 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
892 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
893 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
894 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
900 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
902 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
903 default paths on the new map name...
905 Path should be FALSE when this function is called from the protocol SetMap function.
907 @param[in] Name The human readable mapped name.
908 @param[in] DevicePath The Device Path for this map.
909 @param[in] Flags The Flags attribute for this map item.
910 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
912 @retval EFI_SUCCESS The addition was sucessful.
913 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
914 @retval EFI_INVALID_PARAMETER A parameter was invalid.
918 ShellCommandAddMapItemAndUpdatePath(
919 IN CONST CHAR16
*Name
,
920 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
921 IN CONST UINT64 Flags
,
922 IN CONST BOOLEAN Path
926 SHELL_MAP_LIST
*MapListNode
;
927 CONST CHAR16
*OriginalPath
;
934 Status
= EFI_SUCCESS
;
936 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
937 if (MapListNode
== NULL
) {
938 Status
= EFI_OUT_OF_RESOURCES
;
940 MapListNode
->Flags
= Flags
;
941 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
942 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
943 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
944 Status
= EFI_OUT_OF_RESOURCES
;
946 StrCpy(MapListNode
->MapName
, Name
);
947 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
950 if (EFI_ERROR(Status
)) {
951 if (MapListNode
!= NULL
) {
952 if (MapListNode
->DevicePath
!= NULL
) {
953 FreePool(MapListNode
->DevicePath
);
955 if (MapListNode
->MapName
!= NULL
) {
956 FreePool(MapListNode
->MapName
);
958 FreePool(MapListNode
);
962 // Since there was no error and Path was TRUE
963 // Now add the correct path for that mapping
965 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
966 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
967 if (OriginalPath
!= NULL
) {
968 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
970 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
972 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
973 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
974 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
975 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
976 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
977 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
978 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
980 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
981 ASSERT_EFI_ERROR(Status
);
988 Creates the default map names for each device path in the system with
989 a protocol depending on the Type.
991 Creates the consistent map names for each device path in the system with
992 a protocol depending on the Type.
994 Note: This will reset all mappings in the system("map -r").
996 Also sets up the default path environment variable if Type is FileSystem.
998 @retval EFI_SUCCESS All map names were created sucessfully.
999 @retval EFI_NOT_FOUND No protocols were found in the system.
1000 @return Error returned from gBS->LocateHandle().
1006 ShellCommandCreateInitialMappingsAndPaths(
1011 EFI_HANDLE
*HandleList
;
1013 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1014 CHAR16
*NewDefaultName
;
1015 CHAR16
*NewConsistName
;
1016 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1017 SHELL_MAP_LIST
*MapListNode
;
1022 // Reset the static members back to zero
1027 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1030 // First empty out the existing list.
1032 if (!IsListEmpty(&gShellMapList
.Link
)) {
1033 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1034 ; !IsListEmpty(&gShellMapList
.Link
)
1035 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1037 RemoveEntryList(&MapListNode
->Link
);
1038 FreePool(MapListNode
);
1043 // Find each handle with Simple File System
1045 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1046 if (HandleList
!= NULL
) {
1048 // Do a count of the handles
1050 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1053 // Get all Device Paths
1055 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1056 ASSERT(DevicePathList
!= NULL
);
1058 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1059 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1063 // Sort all DevicePaths
1065 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1067 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1069 // Assign new Mappings to all...
1071 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1073 // Get default name first
1075 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1076 ASSERT(NewDefaultName
!= NULL
);
1077 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1078 ASSERT_EFI_ERROR(Status
);
1079 FreePool(NewDefaultName
);
1082 // Now do consistent name
1084 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1085 if (NewConsistName
!= NULL
) {
1086 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1087 ASSERT_EFI_ERROR(Status
);
1088 FreePool(NewConsistName
);
1092 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1094 SHELL_FREE_NON_NULL(HandleList
);
1095 SHELL_FREE_NON_NULL(DevicePathList
);
1103 // Find each handle with Block Io
1105 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1106 if (HandleList
!= NULL
) {
1107 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1110 // Get all Device Paths
1112 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1113 ASSERT(DevicePathList
!= NULL
);
1115 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1116 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1120 // Sort all DevicePaths
1122 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1125 // Assign new Mappings to all...
1127 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1129 // Get default name first
1131 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1132 ASSERT(NewDefaultName
!= NULL
);
1133 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1134 ASSERT_EFI_ERROR(Status
);
1135 FreePool(NewDefaultName
);
1138 SHELL_FREE_NON_NULL(HandleList
);
1139 SHELL_FREE_NON_NULL(DevicePathList
);
1140 } else if (Count
== (UINTN
)-1) {
1141 return (EFI_NOT_FOUND
);
1144 return (EFI_SUCCESS
);
1148 Function to make sure all directory delimeters are backslashes.
1150 @param[in,out] Path The path to modify.
1156 ShellCommandCleanPath (
1162 for (Path2
= Path
; Path2
!= NULL
&& *Path2
!= CHAR_NULL
; Path2
++) {
1163 if (*Path2
== L
'/') {
1172 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1174 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1176 @return a EFI_FILE_PROTOCOL* representing the same file.
1180 ConvertShellHandleToEfiFileProtocol(
1181 IN CONST SHELL_FILE_HANDLE Handle
1184 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1188 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1190 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1191 @param[in] Path The path to the file for verification.
1193 @return A SHELL_FILE_HANDLE representing the same file.
1194 @retval NULL There was not enough memory.
1198 ConvertEfiFileProtocolToShellHandle(
1199 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1200 IN CONST CHAR16
*Path
1203 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1204 BUFFER_LIST
*NewNode
;
1207 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1208 if (Buffer
== NULL
) {
1211 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1212 if (NewNode
== NULL
) {
1215 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1216 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1217 if (Buffer
->Path
== NULL
) {
1220 NewNode
->Buffer
= Buffer
;
1222 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1224 return ((SHELL_FILE_HANDLE
)(Handle
));
1228 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1230 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1232 @return A pointer to the path for the file.
1236 ShellFileHandleGetPath(
1237 IN CONST SHELL_FILE_HANDLE Handle
1242 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1243 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1244 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1246 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1247 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1254 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1256 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1258 @retval TRUE The item was removed.
1259 @retval FALSE The item was not found.
1263 ShellFileHandleRemove(
1264 IN CONST SHELL_FILE_HANDLE Handle
1269 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1270 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1271 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1273 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1274 RemoveEntryList(&Node
->Link
);
1275 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1276 SHELL_FREE_NON_NULL(Node
->Buffer
);
1277 SHELL_FREE_NON_NULL(Node
);
1285 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1287 This will NOT work on directories.
1289 If Handle is NULL, then ASSERT.
1291 @param[in] Handle the file handle
1293 @retval TRUE the position is at the end of the file
1294 @retval FALSE the position is not at the end of the file
1299 IN SHELL_FILE_HANDLE Handle
1302 EFI_FILE_INFO
*Info
;
1307 // ASSERT if Handle is NULL
1309 ASSERT(Handle
!= NULL
);
1311 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1312 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1313 ASSERT(Info
!= NULL
);
1314 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1320 if (Pos
== Info
->FileSize
) {
1332 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
1333 buffer. The returned buffer must be callee freed.
1335 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1336 maintained and not changed for all operations with the same file.
1338 @param[in] Handle SHELL_FILE_HANDLE to read from.
1339 @param[in,out] Ascii Boolean value for indicating whether the file is
1340 Ascii (TRUE) or UCS2 (FALSE).
1342 @return The line of text from the file.
1344 @sa ShellFileHandleReadLine
1348 ShellFileHandleReturnLine(
1349 IN SHELL_FILE_HANDLE Handle
,
1350 IN OUT BOOLEAN
*Ascii
1360 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
1361 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1362 RetVal
= AllocateZeroPool(Size
);
1363 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
1365 ASSERT_EFI_ERROR(Status
);
1366 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
1374 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
1376 If the position upon start is 0, then the Ascii Boolean will be set. This should be
1377 maintained and not changed for all operations with the same file.
1379 @param[in] Handle SHELL_FILE_HANDLE to read from.
1380 @param[in,out] Buffer The pointer to buffer to read into.
1381 @param[in,out] Size The pointer to number of bytes in Buffer.
1382 @param[in] Truncate If the buffer is large enough, this has no effect.
1383 If the buffer is is too small and Truncate is TRUE,
1384 the line will be truncated.
1385 If the buffer is is too small and Truncate is FALSE,
1386 then no read will occur.
1388 @param[in,out] Ascii Boolean value for indicating whether the file is
1389 Ascii (TRUE) or UCS2 (FALSE).
1391 @retval EFI_SUCCESS The operation was successful. The line is stored in
1393 @retval EFI_INVALID_PARAMETER Handle was NULL.
1394 @retval EFI_INVALID_PARAMETER Size was NULL.
1395 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
1396 Size was updated to the minimum space required.
1397 @sa ShellFileHandleRead
1401 ShellFileHandleReadLine(
1402 IN SHELL_FILE_HANDLE Handle
,
1403 IN OUT CHAR16
*Buffer
,
1405 IN BOOLEAN Truncate
,
1406 IN OUT BOOLEAN
*Ascii
1413 UINT64 OriginalFilePosition
;
1419 return (EFI_INVALID_PARAMETER
);
1421 if (Buffer
== NULL
) {
1424 *Buffer
= CHAR_NULL
;
1426 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
1427 if (OriginalFilePosition
== 0) {
1428 CharSize
= sizeof(CHAR16
);
1429 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
1430 ASSERT_EFI_ERROR(Status
);
1431 if (CharBuffer
== gUnicodeFileTag
) {
1435 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
1439 for (CountSoFar
= 0;;CountSoFar
++){
1442 CharSize
= sizeof(CHAR8
);
1444 CharSize
= sizeof(CHAR16
);
1446 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
1447 if ( EFI_ERROR(Status
)
1449 || (CharBuffer
== L
'\n' && !(*Ascii
))
1450 || (CharBuffer
== '\n' && *Ascii
)
1455 // if we have space save it...
1457 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
1458 ASSERT(Buffer
!= NULL
);
1459 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
1460 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
1465 // if we ran out of space tell when...
1467 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
1468 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
1470 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
1472 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
1474 return (EFI_BUFFER_TOO_SMALL
);
1476 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
1477 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
1483 Frees any BUFFER_LIST defined type.
1485 @param[in] List The BUFFER_LIST object to free.
1490 IN BUFFER_LIST
*List
1493 BUFFER_LIST
*BufferListEntry
;
1499 // enumerate through the buffer list and free all memory
1501 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1502 ; !IsListEmpty (&List
->Link
)
1503 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1505 RemoveEntryList(&BufferListEntry
->Link
);
1506 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1507 if (BufferListEntry
->Buffer
!= NULL
) {
1508 FreePool(BufferListEntry
->Buffer
);
1510 FreePool(BufferListEntry
);
1515 Chops off last directory or file entry in a path leaving the trailing slash
1517 @param[in,out] PathToReturn The path to modify.
1519 @retval FALSE No directory was found to chop off.
1520 @retval TRUE A directory was chopped off.
1525 IN OUT CHAR16
*PathToReturn
1531 // get directory name from path... ('chop' off extra)
1533 for ( Walker
= PathToReturn
, LastSlash
= NULL
1534 ; Walker
!= NULL
&& *Walker
!= CHAR_NULL
1537 if (*Walker
== L
'\\' && *(Walker
+ 1) != CHAR_NULL
) {
1538 LastSlash
= Walker
+1;
1541 if (LastSlash
!= NULL
) {
1542 *LastSlash
= CHAR_NULL
;