2 Provides interface to shell internal functions for shell commands.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "UefiShellCommandLib.h"
14 // STATIC local variables
15 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList
;
16 STATIC SCRIPT_FILE_LIST mScriptList
;
17 STATIC ALIAS_LIST mAliasList
;
18 STATIC BOOLEAN mEchoState
;
19 STATIC BOOLEAN mExitRequested
;
20 STATIC UINT64 mExitCode
;
21 STATIC BOOLEAN mExitScript
;
22 STATIC CHAR16
*mProfileList
;
23 STATIC UINTN mProfileListSize
;
24 STATIC UINTN mFsMaxCount
= 0;
25 STATIC UINTN mBlkMaxCount
= 0;
26 STATIC BUFFER_LIST mFileHandleList
;
28 STATIC CONST CHAR8 Hex
[] = {
47 // global variables required by library class.
48 EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
= NULL
;
49 SHELL_MAP_LIST gShellMapList
;
50 SHELL_MAP_LIST
*gShellCurMapping
= NULL
;
52 CONST CHAR16
* SupportLevel
[] = {
60 Function to make sure that the global protocol pointers are valid.
61 must be called after constructor before accessing the pointers.
71 EFI_UNICODE_COLLATION_PROTOCOL
*Uc
;
77 if (gUnicodeCollation
== NULL
) {
79 GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME
, (VOID
**)&PlatformLang
, NULL
);
81 Status
= gBS
->LocateHandleBuffer (
83 &gEfiUnicodeCollation2ProtocolGuid
,
88 if (EFI_ERROR (Status
)) {
92 for (Index
= 0; Index
< NumHandles
; Index
++) {
94 // Open Unicode Collation Protocol
96 Status
= gBS
->OpenProtocol (
98 &gEfiUnicodeCollation2ProtocolGuid
,
102 EFI_OPEN_PROTOCOL_GET_PROTOCOL
104 if (EFI_ERROR (Status
)) {
109 // Without clue provided use the first Unicode Collation2 protocol.
110 // This may happen when PlatformLang is NULL or when no installed Unicode
111 // Collation2 protocol instance supports PlatformLang.
113 if (gUnicodeCollation
== NULL
) {
114 gUnicodeCollation
= Uc
;
116 if (PlatformLang
== NULL
) {
121 // Find the best matching matching language from the supported languages
122 // of Unicode Collation2 protocol.
124 BestLanguage
= GetBestLanguage (
125 Uc
->SupportedLanguages
,
130 if (BestLanguage
!= NULL
) {
131 FreePool (BestLanguage
);
132 gUnicodeCollation
= Uc
;
136 if (Handles
!= NULL
) {
139 if (PlatformLang
!= NULL
) {
140 FreePool (PlatformLang
);
144 return (gUnicodeCollation
== NULL
) ? EFI_UNSUPPORTED
: EFI_SUCCESS
;
148 Constructor for the Shell Command library.
150 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
152 @param ImageHandle the image handle of the process
153 @param SystemTable the EFI System Table pointer
155 @retval EFI_SUCCESS the initialization was complete sucessfully
159 ShellCommandLibConstructor (
160 IN EFI_HANDLE ImageHandle
,
161 IN EFI_SYSTEM_TABLE
*SystemTable
165 InitializeListHead(&gShellMapList
.Link
);
166 InitializeListHead(&mCommandList
.Link
);
167 InitializeListHead(&mAliasList
.Link
);
168 InitializeListHead(&mScriptList
.Link
);
169 InitializeListHead(&mFileHandleList
.Link
);
172 mExitRequested
= FALSE
;
174 mProfileListSize
= 0;
177 Status
= CommandInit ();
178 if (EFI_ERROR (Status
)) {
179 return EFI_DEVICE_ERROR
;
182 return (RETURN_SUCCESS
);
186 Frees list of file handles.
188 @param[in] List The list to free.
195 BUFFER_LIST
*BufferListEntry
;
201 // enumerate through the buffer list and free all memory
203 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
204 ; !IsListEmpty (&List
->Link
)
205 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
207 RemoveEntryList(&BufferListEntry
->Link
);
208 ASSERT(BufferListEntry
->Buffer
!= NULL
);
209 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)(BufferListEntry
->Buffer
))->Path
);
210 SHELL_FREE_NON_NULL(BufferListEntry
->Buffer
);
211 SHELL_FREE_NON_NULL(BufferListEntry
);
216 Destructor for the library. free any resources.
218 @param ImageHandle the image handle of the process
219 @param SystemTable the EFI System Table pointer
221 @retval RETURN_SUCCESS this function always returns success
225 ShellCommandLibDestructor (
226 IN EFI_HANDLE ImageHandle
,
227 IN EFI_SYSTEM_TABLE
*SystemTable
230 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
232 SCRIPT_FILE_LIST
*Node3
;
233 SHELL_MAP_LIST
*MapNode
;
235 // enumerate throught the list and free all the memory
237 while (!IsListEmpty (&mCommandList
.Link
)) {
238 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
239 RemoveEntryList(&Node
->Link
);
240 SHELL_FREE_NON_NULL(Node
->CommandString
);
242 DEBUG_CODE(Node
= NULL
;);
246 // enumerate through the alias list and free all memory
248 while (!IsListEmpty (&mAliasList
.Link
)) {
249 Node2
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
);
250 RemoveEntryList(&Node2
->Link
);
251 SHELL_FREE_NON_NULL(Node2
->CommandString
);
252 SHELL_FREE_NON_NULL(Node2
->Alias
);
253 SHELL_FREE_NON_NULL(Node2
);
254 DEBUG_CODE(Node2
= NULL
;);
258 // enumerate throught the list and free all the memory
260 while (!IsListEmpty (&mScriptList
.Link
)) {
261 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
262 RemoveEntryList(&Node3
->Link
);
263 DeleteScriptFileStruct(Node3
->Data
);
268 // enumerate throught the mappings list and free all the memory
270 if (!IsListEmpty(&gShellMapList
.Link
)) {
271 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
272 ; !IsListEmpty (&gShellMapList
.Link
)
273 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
275 ASSERT(MapNode
!= NULL
);
276 RemoveEntryList(&MapNode
->Link
);
277 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
278 SHELL_FREE_NON_NULL(MapNode
->MapName
);
279 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
283 if (!IsListEmpty(&mFileHandleList
.Link
)){
284 FreeFileHandleList(&mFileHandleList
);
287 if (mProfileList
!= NULL
) {
288 FreePool(mProfileList
);
291 gUnicodeCollation
= NULL
;
292 gShellCurMapping
= NULL
;
294 return (RETURN_SUCCESS
);
298 Find a dynamic command protocol instance given a command name string.
300 @param CommandString the command name string
302 @return instance the command protocol instance, if dynamic command instance found
303 @retval NULL no dynamic command protocol instance found for name
305 CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*
306 ShellCommandFindDynamicCommand (
307 IN CONST CHAR16
*CommandString
311 EFI_HANDLE
*CommandHandleList
;
312 EFI_HANDLE
*NextCommand
;
313 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
315 CommandHandleList
= GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid
);
316 if (CommandHandleList
== NULL
) {
318 // not found or out of resources
323 for (NextCommand
= CommandHandleList
; *NextCommand
!= NULL
; NextCommand
++) {
324 Status
= gBS
->HandleProtocol(
326 &gEfiShellDynamicCommandProtocolGuid
,
327 (VOID
**)&DynamicCommand
330 if (EFI_ERROR(Status
)) {
334 if (gUnicodeCollation
->StriColl(
336 (CHAR16
*)CommandString
,
337 (CHAR16
*)DynamicCommand
->CommandName
) == 0
339 FreePool(CommandHandleList
);
340 return (DynamicCommand
);
344 FreePool(CommandHandleList
);
349 Checks if a command exists as a dynamic command protocol instance
351 @param[in] CommandString The command string to check for on the list.
354 ShellCommandDynamicCommandExists (
355 IN CONST CHAR16
*CommandString
358 return (BOOLEAN
) ((ShellCommandFindDynamicCommand(CommandString
) != NULL
));
362 Checks if a command is already on the internal command list.
364 @param[in] CommandString The command string to check for on the list.
367 ShellCommandIsCommandOnInternalList(
368 IN CONST CHAR16
*CommandString
371 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
374 // assert for NULL parameter
376 ASSERT(CommandString
!= NULL
);
379 // check for the command
381 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
382 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
383 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
385 ASSERT(Node
->CommandString
!= NULL
);
386 if (gUnicodeCollation
->StriColl(
388 (CHAR16
*)CommandString
,
389 Node
->CommandString
) == 0
398 Checks if a command exists, either internally or through the dynamic command protocol.
400 @param[in] CommandString The command string to check for on the list.
404 ShellCommandIsCommandOnList(
405 IN CONST CHAR16
*CommandString
408 if (ShellCommandIsCommandOnInternalList(CommandString
)) {
412 return ShellCommandDynamicCommandExists(CommandString
);
416 Get the help text for a dynamic command.
418 @param[in] CommandString The command name.
420 @retval NULL No help text was found.
421 @return String of help text. Caller required to free.
424 ShellCommandGetDynamicCommandHelp(
425 IN CONST CHAR16
*CommandString
428 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
430 DynamicCommand
= (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*)ShellCommandFindDynamicCommand(CommandString
);
431 if (DynamicCommand
== NULL
) {
436 // TODO: how to get proper language?
438 return DynamicCommand
->GetHelp(DynamicCommand
, "en");
442 Get the help text for an internal command.
444 @param[in] CommandString The command name.
446 @retval NULL No help text was found.
447 @return String of help text. Caller reuiqred to free.
450 ShellCommandGetInternalCommandHelp(
451 IN CONST CHAR16
*CommandString
454 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
457 // assert for NULL parameter
459 ASSERT(CommandString
!= NULL
);
462 // check for the command
464 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
465 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
466 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
468 ASSERT(Node
->CommandString
!= NULL
);
469 if (gUnicodeCollation
->StriColl(
471 (CHAR16
*)CommandString
,
472 Node
->CommandString
) == 0
474 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
481 Get the help text for a command.
483 @param[in] CommandString The command name.
485 @retval NULL No help text was found.
486 @return String of help text.Caller reuiqred to free.
490 ShellCommandGetCommandHelp (
491 IN CONST CHAR16
*CommandString
495 HelpStr
= ShellCommandGetInternalCommandHelp(CommandString
);
497 if (HelpStr
== NULL
) {
498 HelpStr
= ShellCommandGetDynamicCommandHelp(CommandString
);
506 Registers handlers of type SHELL_RUN_COMMAND and
507 SHELL_GET_MAN_FILENAME for each shell command.
509 If the ShellSupportLevel is greater than the value of the
510 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
512 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
513 with the command specified by CommandString. If the command named by
514 CommandString has already been registered, then return
515 RETURN_ALREADY_STARTED.
517 If there are not enough resources available to register the handlers then
518 RETURN_OUT_OF_RESOURCES is returned.
520 If CommandString is NULL, then ASSERT().
521 If GetHelpInfoHandler is NULL, then ASSERT().
522 If CommandHandler is NULL, then ASSERT().
523 If ProfileName is NULL, then ASSERT().
525 @param[in] CommandString Pointer to the command name. This is the
526 name to look for on the command line in
528 @param[in] CommandHandler Pointer to a function that runs the
530 @param[in] GetManFileName Pointer to a function that provides man
532 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
534 @param[in] ProfileName profile name to require for support of this
536 @param[in] CanAffectLE indicates whether this command's return value
537 can change the LASTERROR environment variable.
538 @param[in] HiiHandle Handle of this command's HII entry.
539 @param[in] ManFormatHelp HII locator for the help text.
541 @retval RETURN_SUCCESS The handlers were registered.
542 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
543 register the shell command.
544 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
545 currently allowed support level.
546 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
547 is already registered. Only 1 handler set for
548 a given command is allowed.
549 @sa SHELL_GET_MAN_FILENAME
550 @sa SHELL_RUN_COMMAND
554 ShellCommandRegisterCommandName (
555 IN CONST CHAR16
*CommandString
,
556 IN SHELL_RUN_COMMAND CommandHandler
,
557 IN SHELL_GET_MAN_FILENAME GetManFileName
,
558 IN UINT32 ShellMinSupportLevel
,
559 IN CONST CHAR16
*ProfileName
,
560 IN CONST BOOLEAN CanAffectLE
,
561 IN CONST EFI_HII_HANDLE HiiHandle
,
562 IN CONST EFI_STRING_ID ManFormatHelp
565 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
566 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Command
;
567 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*PrevCommand
;
568 INTN LexicalMatchValue
;
571 // Initialize local variables.
575 LexicalMatchValue
= 0;
578 // ASSERTs for NULL parameters
580 ASSERT(CommandString
!= NULL
);
581 ASSERT(GetManFileName
!= NULL
);
582 ASSERT(CommandHandler
!= NULL
);
583 ASSERT(ProfileName
!= NULL
);
586 // check for shell support level
588 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
589 return (RETURN_UNSUPPORTED
);
593 // check for already on the list
595 if (ShellCommandIsCommandOnList(CommandString
)) {
596 return (RETURN_ALREADY_STARTED
);
600 // allocate memory for new struct
602 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
604 return RETURN_OUT_OF_RESOURCES
;
606 Node
->CommandString
= AllocateCopyPool(StrSize(CommandString
), CommandString
);
607 if (Node
->CommandString
== NULL
) {
609 return RETURN_OUT_OF_RESOURCES
;
612 Node
->GetManFileName
= GetManFileName
;
613 Node
->CommandHandler
= CommandHandler
;
614 Node
->LastError
= CanAffectLE
;
615 Node
->HiiHandle
= HiiHandle
;
616 Node
->ManFormatHelp
= ManFormatHelp
;
618 if ( StrLen(ProfileName
)>0
619 && ((mProfileList
!= NULL
620 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
622 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
623 if (mProfileList
== NULL
) {
625 // If this is the first make a leading ';'
627 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
629 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
630 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
634 // Insert a new entry on top of the list
636 InsertHeadList (&mCommandList
.Link
, &Node
->Link
);
639 // Move a new registered command to its sorted ordered location in the list
641 for (Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
),
642 PrevCommand
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
)
643 ; !IsNull (&mCommandList
.Link
, &Command
->Link
)
644 ; Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode (&mCommandList
.Link
, &Command
->Link
)) {
647 // Get Lexical Comparison Value between PrevCommand and Command list entry
649 LexicalMatchValue
= gUnicodeCollation
->StriColl (
651 PrevCommand
->CommandString
,
652 Command
->CommandString
656 // Swap PrevCommand and Command list entry if PrevCommand list entry
657 // is alphabetically greater than Command list entry
659 if (LexicalMatchValue
> 0){
660 Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*) SwapListEntries (&PrevCommand
->Link
, &Command
->Link
);
661 } else if (LexicalMatchValue
< 0) {
663 // PrevCommand entry is lexically lower than Command entry
669 return (RETURN_SUCCESS
);
673 Function to get the current Profile string.
675 @retval NULL There are no installed profiles.
676 @return A semi-colon delimited list of profiles.
680 ShellCommandGetProfileList (
684 return (mProfileList
);
688 Checks if a command string has been registered for CommandString and if so it runs
689 the previously registered handler for that command with the command line.
691 If CommandString is NULL, then ASSERT().
693 If Sections is specified, then each section name listed will be compared in a casesensitive
694 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
695 it will be appended to the returned help text. If the section does not exist, no
696 information will be returned. If Sections is NULL, then all help text information
697 available will be returned.
699 @param[in] CommandString Pointer to the command name. This is the name
700 found on the command line in the shell.
701 @param[in, out] RetVal Pointer to the return vaule from the command handler.
703 @param[in, out] CanAffectLE indicates whether this command's return value
704 needs to be placed into LASTERROR environment variable.
706 @retval RETURN_SUCCESS The handler was run.
707 @retval RETURN_NOT_FOUND The CommandString did not match a registered
709 @sa SHELL_RUN_COMMAND
713 ShellCommandRunCommandHandler (
714 IN CONST CHAR16
*CommandString
,
715 IN OUT SHELL_STATUS
*RetVal
,
716 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
719 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
720 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
723 // assert for NULL parameters
725 ASSERT(CommandString
!= NULL
);
728 // check for the command
730 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
731 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
732 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
734 ASSERT(Node
->CommandString
!= NULL
);
735 if (gUnicodeCollation
->StriColl(
737 (CHAR16
*)CommandString
,
738 Node
->CommandString
) == 0
740 if (CanAffectLE
!= NULL
) {
741 *CanAffectLE
= Node
->LastError
;
743 if (RetVal
!= NULL
) {
744 *RetVal
= Node
->CommandHandler(NULL
, gST
);
746 Node
->CommandHandler(NULL
, gST
);
748 return (RETURN_SUCCESS
);
753 // An internal command was not found, try to find a dynamic command
755 DynamicCommand
= (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*)ShellCommandFindDynamicCommand(CommandString
);
756 if (DynamicCommand
!= NULL
) {
757 if (RetVal
!= NULL
) {
758 *RetVal
= DynamicCommand
->Handler(DynamicCommand
, gST
, gEfiShellParametersProtocol
, gEfiShellProtocol
);
760 DynamicCommand
->Handler(DynamicCommand
, gST
, gEfiShellParametersProtocol
, gEfiShellProtocol
);
762 return (RETURN_SUCCESS
);
765 return (RETURN_NOT_FOUND
);
769 Checks if a command string has been registered for CommandString and if so it
770 returns the MAN filename specified for that command.
772 If CommandString is NULL, then ASSERT().
774 @param[in] CommandString Pointer to the command name. This is the name
775 found on the command line in the shell.\
777 @retval NULL the commandString was not a registered command.
778 @return other the name of the MAN file.
779 @sa SHELL_GET_MAN_FILENAME
783 ShellCommandGetManFileNameHandler (
784 IN CONST CHAR16
*CommandString
787 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
790 // assert for NULL parameters
792 ASSERT(CommandString
!= NULL
);
795 // check for the command
797 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
798 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
799 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
801 ASSERT(Node
->CommandString
!= NULL
);
802 if (gUnicodeCollation
->StriColl(
804 (CHAR16
*)CommandString
,
805 Node
->CommandString
) == 0
807 return (Node
->GetManFileName());
814 Get the list of all available shell internal commands. This is a linked list
815 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
816 list functions. do not modify the values.
818 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
820 @return a Linked list of all available shell commands.
824 ShellCommandGetCommandList (
825 IN CONST BOOLEAN Sort
829 // return ((COMMAND_LIST*)(&mCommandList));
831 return ((COMMAND_LIST
*)(&mCommandList
));
835 Registers aliases to be set as part of the initialization of the shell application.
837 If Command is NULL, then ASSERT().
838 If Alias is NULL, then ASSERT().
840 @param[in] Command Pointer to the Command
841 @param[in] Alias Pointer to Alias
843 @retval RETURN_SUCCESS The handlers were registered.
844 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
845 register the shell command.
849 ShellCommandRegisterAlias (
850 IN CONST CHAR16
*Command
,
851 IN CONST CHAR16
*Alias
855 ALIAS_LIST
*CommandAlias
;
856 ALIAS_LIST
*PrevCommandAlias
;
857 INTN LexicalMatchValue
;
862 ASSERT(Command
!= NULL
);
863 ASSERT(Alias
!= NULL
);
866 // allocate memory for new struct
868 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
870 return RETURN_OUT_OF_RESOURCES
;
872 Node
->CommandString
= AllocateCopyPool(StrSize(Command
), Command
);
873 if (Node
->CommandString
== NULL
) {
875 return RETURN_OUT_OF_RESOURCES
;
877 Node
->Alias
= AllocateCopyPool(StrSize(Alias
), Alias
);
878 if (Node
->Alias
== NULL
) {
879 FreePool (Node
->CommandString
);
881 return RETURN_OUT_OF_RESOURCES
;
884 InsertHeadList (&mAliasList
.Link
, &Node
->Link
);
887 // Move a new pre-defined registered alias to its sorted ordered location in the list
889 for ( CommandAlias
= (ALIAS_LIST
*)GetFirstNode (&mAliasList
.Link
),
890 PrevCommandAlias
= (ALIAS_LIST
*)GetFirstNode (&mAliasList
.Link
)
891 ; !IsNull (&mAliasList
.Link
, &CommandAlias
->Link
)
892 ; CommandAlias
= (ALIAS_LIST
*) GetNextNode (&mAliasList
.Link
, &CommandAlias
->Link
) ) {
894 // Get Lexical comparison value between PrevCommandAlias and CommandAlias List Entry
896 LexicalMatchValue
= gUnicodeCollation
->StriColl (
898 PrevCommandAlias
->Alias
,
903 // Swap PrevCommandAlias and CommandAlias list entry if PrevCommandAlias list entry
904 // is alphabetically greater than CommandAlias list entry
906 if (LexicalMatchValue
> 0) {
907 CommandAlias
= (ALIAS_LIST
*) SwapListEntries (&PrevCommandAlias
->Link
, &CommandAlias
->Link
);
908 } else if (LexicalMatchValue
< 0) {
910 // PrevCommandAlias entry is lexically lower than CommandAlias entry
916 return (RETURN_SUCCESS
);
920 Get the list of all shell alias commands. This is a linked list
921 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
922 list functions. do not modify the values.
924 @return a Linked list of all requested shell alias'.
928 ShellCommandGetInitAliasList (
932 return (&mAliasList
);
936 Determine if a given alias is on the list of built in alias'.
938 @param[in] Alias The alias to test for
940 @retval TRUE The alias is a built in alias
941 @retval FALSE The alias is not a built in alias
945 ShellCommandIsOnAliasList(
946 IN CONST CHAR16
*Alias
952 // assert for NULL parameter
954 ASSERT(Alias
!= NULL
);
957 // check for the Alias
959 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
960 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
961 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
963 ASSERT(Node
->CommandString
!= NULL
);
964 ASSERT(Node
->Alias
!= NULL
);
965 if (gUnicodeCollation
->StriColl(
968 Node
->CommandString
) == 0
972 if (gUnicodeCollation
->StriColl(
984 Function to determine current state of ECHO. Echo determines if lines from scripts
985 and ECHO commands are enabled.
987 @retval TRUE Echo is currently enabled
988 @retval FALSE Echo is currently disabled
992 ShellCommandGetEchoState(
1000 Function to set current state of ECHO. Echo determines if lines from scripts
1001 and ECHO commands are enabled.
1003 If State is TRUE, Echo will be enabled.
1004 If State is FALSE, Echo will be disabled.
1006 @param[in] State How to set echo.
1010 ShellCommandSetEchoState(
1018 Indicate that the current shell or script should exit.
1020 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
1021 @param[in] ErrorCode The 64 bit error code to return.
1025 ShellCommandRegisterExit (
1026 IN BOOLEAN ScriptOnly
,
1027 IN CONST UINT64 ErrorCode
1030 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
1031 if (mExitRequested
) {
1032 mExitScript
= ScriptOnly
;
1034 mExitScript
= FALSE
;
1036 mExitCode
= ErrorCode
;
1040 Retrieve the Exit indicator.
1042 @retval TRUE Exit was indicated.
1043 @retval FALSE Exis was not indicated.
1047 ShellCommandGetExit (
1051 return (mExitRequested
);
1055 Retrieve the Exit code.
1057 If ShellCommandGetExit returns FALSE than the return from this is undefined.
1059 @return the value passed into RegisterExit.
1063 ShellCommandGetExitCode (
1070 Retrieve the Exit script indicator.
1072 If ShellCommandGetExit returns FALSE than the return from this is undefined.
1074 @retval TRUE ScriptOnly was indicated.
1075 @retval FALSE ScriptOnly was not indicated.
1079 ShellCommandGetScriptExit (
1083 return (mExitScript
);
1087 Function to cleanup all memory from a SCRIPT_FILE structure.
1089 @param[in] Script The pointer to the structure to cleanup.
1093 DeleteScriptFileStruct (
1094 IN SCRIPT_FILE
*Script
1099 if (Script
== NULL
) {
1103 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
1104 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
1106 if (Script
->Argv
!= NULL
) {
1107 SHELL_FREE_NON_NULL(Script
->Argv
);
1109 Script
->CurrentCommand
= NULL
;
1110 while (!IsListEmpty (&Script
->CommandList
)) {
1111 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
1112 if (Script
->CurrentCommand
!= NULL
) {
1113 RemoveEntryList(&Script
->CurrentCommand
->Link
);
1114 if (Script
->CurrentCommand
->Cl
!= NULL
) {
1115 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
1117 if (Script
->CurrentCommand
->Data
!= NULL
) {
1118 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
1120 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
1123 SHELL_FREE_NON_NULL(Script
->ScriptName
);
1124 SHELL_FREE_NON_NULL(Script
);
1128 Function to return a pointer to the currently running script file object.
1130 @retval NULL A script file is not currently running.
1131 @return A pointer to the current script file object.
1135 ShellCommandGetCurrentScriptFile (
1139 SCRIPT_FILE_LIST
*List
;
1140 if (IsListEmpty (&mScriptList
.Link
)) {
1143 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
1144 return (List
->Data
);
1148 Function to set a new script as the currently running one.
1150 This function will correctly stack and unstack nested scripts.
1152 @param[in] Script Pointer to new script information structure. if NULL
1153 will remove and de-allocate the top-most Script structure.
1155 @return A pointer to the current running script file after this
1156 change. NULL if removing the final script.
1160 ShellCommandSetNewScript (
1161 IN SCRIPT_FILE
*Script OPTIONAL
1164 SCRIPT_FILE_LIST
*Node
;
1165 if (Script
== NULL
) {
1166 if (IsListEmpty (&mScriptList
.Link
)) {
1169 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
1170 RemoveEntryList(&Node
->Link
);
1171 DeleteScriptFileStruct(Node
->Data
);
1174 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
1178 Node
->Data
= Script
;
1179 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
1181 return (ShellCommandGetCurrentScriptFile());
1185 Function to generate the next default mapping name.
1187 If the return value is not NULL then it must be callee freed.
1189 @param Type What kind of mapping name to make.
1191 @retval NULL a memory allocation failed.
1192 @return a new map name string
1196 ShellCommandCreateNewMappingName(
1197 IN CONST SHELL_MAPPING_TYPE Type
1201 ASSERT(Type
< MappingTypeMax
);
1205 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
1208 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
1209 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
1210 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
1216 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
1218 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
1219 default paths on the new map name...
1221 Path should be FALSE when this function is called from the protocol SetMap function.
1223 @param[in] Name The human readable mapped name.
1224 @param[in] DevicePath The Device Path for this map.
1225 @param[in] Flags The Flags attribute for this map item.
1226 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
1228 @retval EFI_SUCCESS The addition was sucessful.
1229 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1230 @retval EFI_INVALID_PARAMETER A parameter was invalid.
1234 ShellCommandAddMapItemAndUpdatePath(
1235 IN CONST CHAR16
*Name
,
1236 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1237 IN CONST UINT64 Flags
,
1238 IN CONST BOOLEAN Path
1242 SHELL_MAP_LIST
*MapListNode
;
1243 CONST CHAR16
*OriginalPath
;
1249 OriginalPath
= NULL
;
1250 Status
= EFI_SUCCESS
;
1252 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
1253 if (MapListNode
== NULL
) {
1254 Status
= EFI_OUT_OF_RESOURCES
;
1256 MapListNode
->Flags
= Flags
;
1257 MapListNode
->MapName
= AllocateCopyPool(StrSize(Name
), Name
);
1258 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
1259 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
1260 Status
= EFI_OUT_OF_RESOURCES
;
1262 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
1265 if (EFI_ERROR(Status
)) {
1266 if (MapListNode
!= NULL
) {
1267 if (MapListNode
->DevicePath
!= NULL
) {
1268 FreePool(MapListNode
->DevicePath
);
1270 if (MapListNode
->MapName
!= NULL
) {
1271 FreePool(MapListNode
->MapName
);
1273 FreePool(MapListNode
);
1277 // Since there was no error and Path was TRUE
1278 // Now add the correct path for that mapping
1280 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
1281 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
1282 if (OriginalPath
!= NULL
) {
1283 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
1284 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
1286 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1287 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
1288 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1289 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
1290 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1291 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
1293 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
1294 ASSERT_EFI_ERROR(Status
);
1301 Creates the default map names for each device path in the system with
1302 a protocol depending on the Type.
1304 Creates the consistent map names for each device path in the system with
1305 a protocol depending on the Type.
1307 Note: This will reset all mappings in the system("map -r").
1309 Also sets up the default path environment variable if Type is FileSystem.
1311 @retval EFI_SUCCESS All map names were created sucessfully.
1312 @retval EFI_NOT_FOUND No protocols were found in the system.
1313 @return Error returned from gBS->LocateHandle().
1319 ShellCommandCreateInitialMappingsAndPaths(
1324 EFI_HANDLE
*HandleList
;
1326 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1327 CHAR16
*NewDefaultName
;
1328 CHAR16
*NewConsistName
;
1329 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1330 SHELL_MAP_LIST
*MapListNode
;
1331 CONST CHAR16
*CurDir
;
1332 CHAR16
*SplitCurDir
;
1334 SHELL_MAP_LIST
*MapListItem
;
1342 // Reset the static members back to zero
1347 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1350 // First empty out the existing list.
1352 if (!IsListEmpty(&gShellMapList
.Link
)) {
1353 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1354 ; !IsListEmpty(&gShellMapList
.Link
)
1355 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1357 RemoveEntryList(&MapListNode
->Link
);
1358 SHELL_FREE_NON_NULL(MapListNode
->DevicePath
);
1359 SHELL_FREE_NON_NULL(MapListNode
->MapName
);
1360 SHELL_FREE_NON_NULL(MapListNode
->CurrentDirectoryPath
);
1361 FreePool(MapListNode
);
1366 // Find each handle with Simple File System
1368 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1369 if (HandleList
!= NULL
) {
1371 // Do a count of the handles
1373 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1376 // Get all Device Paths
1378 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1379 if (DevicePathList
== NULL
) {
1380 SHELL_FREE_NON_NULL (HandleList
);
1381 return EFI_OUT_OF_RESOURCES
;
1384 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1385 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1389 // Sort all DevicePaths
1391 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1393 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1395 // Assign new Mappings to all...
1397 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1399 // Get default name first
1401 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1402 ASSERT(NewDefaultName
!= NULL
);
1403 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1404 ASSERT_EFI_ERROR(Status
);
1405 FreePool(NewDefaultName
);
1408 // Now do consistent name
1410 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1411 if (NewConsistName
!= NULL
) {
1412 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1413 ASSERT_EFI_ERROR(Status
);
1414 FreePool(NewConsistName
);
1418 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1420 SHELL_FREE_NON_NULL(HandleList
);
1421 SHELL_FREE_NON_NULL(DevicePathList
);
1426 //gShellCurMapping point to node of current file system in the gShellMapList. When reset all mappings,
1427 //all nodes in the gShellMapList will be free. Then gShellCurMapping will be a dangling pointer, So,
1428 //after created new mappings, we should reset the gShellCurMapping pointer back to node of current file system.
1430 if (gShellCurMapping
!= NULL
) {
1431 gShellCurMapping
= NULL
;
1432 CurDir
= gEfiShellProtocol
->GetEnv(L
"cwd");
1433 if (CurDir
!= NULL
) {
1434 MapName
= AllocateCopyPool (StrSize(CurDir
), CurDir
);
1435 if (MapName
== NULL
) {
1436 return EFI_OUT_OF_RESOURCES
;
1438 SplitCurDir
= StrStr (MapName
, L
":");
1439 if (SplitCurDir
== NULL
) {
1440 SHELL_FREE_NON_NULL (MapName
);
1441 return EFI_UNSUPPORTED
;
1443 *(SplitCurDir
+ 1) = CHAR_NULL
;
1444 MapListItem
= ShellCommandFindMapItem (MapName
);
1445 if (MapListItem
!= NULL
) {
1446 gShellCurMapping
= MapListItem
;
1448 SHELL_FREE_NON_NULL (MapName
);
1456 // Find each handle with Block Io
1458 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1459 if (HandleList
!= NULL
) {
1460 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1463 // Get all Device Paths
1465 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1466 if (DevicePathList
== NULL
) {
1467 SHELL_FREE_NON_NULL (HandleList
);
1468 return EFI_OUT_OF_RESOURCES
;
1471 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1472 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1476 // Sort all DevicePaths
1478 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1481 // Assign new Mappings to all...
1483 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1485 // Get default name first
1487 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1488 ASSERT(NewDefaultName
!= NULL
);
1489 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1490 ASSERT_EFI_ERROR(Status
);
1491 FreePool(NewDefaultName
);
1494 SHELL_FREE_NON_NULL(HandleList
);
1495 SHELL_FREE_NON_NULL(DevicePathList
);
1496 } else if (Count
== (UINTN
)-1) {
1497 return (EFI_NOT_FOUND
);
1500 return (EFI_SUCCESS
);
1504 Add mappings for any devices without one. Do not change any existing maps.
1506 @retval EFI_SUCCESS The operation was successful.
1510 ShellCommandUpdateMapping (
1515 EFI_HANDLE
*HandleList
;
1517 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1518 CHAR16
*NewDefaultName
;
1519 CHAR16
*NewConsistName
;
1520 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1523 Status
= EFI_SUCCESS
;
1526 // remove mappings that represent removed devices.
1530 // Find each handle with Simple File System
1532 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1533 if (HandleList
!= NULL
) {
1535 // Do a count of the handles
1537 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1540 // Get all Device Paths
1542 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1543 if (DevicePathList
== NULL
) {
1544 return (EFI_OUT_OF_RESOURCES
);
1547 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1548 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1552 // Sort all DevicePaths
1554 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1556 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1559 // Assign new Mappings to remainders
1561 for (Count
= 0 ; !EFI_ERROR(Status
) && HandleList
[Count
] != NULL
&& !EFI_ERROR(Status
); Count
++) {
1563 // Skip ones that already have
1565 if (gEfiShellProtocol
->GetMapFromDevicePath(&DevicePathList
[Count
]) != NULL
) {
1571 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1572 if (NewDefaultName
== NULL
) {
1573 Status
= EFI_OUT_OF_RESOURCES
;
1578 // Call shell protocol SetMap function now...
1580 Status
= gEfiShellProtocol
->SetMap(DevicePathList
[Count
], NewDefaultName
);
1582 if (!EFI_ERROR(Status
)) {
1584 // Now do consistent name
1586 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1587 if (NewConsistName
!= NULL
) {
1588 Status
= gEfiShellProtocol
->SetMap(DevicePathList
[Count
], NewConsistName
);
1589 FreePool(NewConsistName
);
1593 FreePool(NewDefaultName
);
1595 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1596 SHELL_FREE_NON_NULL(HandleList
);
1597 SHELL_FREE_NON_NULL(DevicePathList
);
1604 // Do it all over again for gEfiBlockIoProtocolGuid
1611 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1613 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1615 @return a EFI_FILE_PROTOCOL* representing the same file.
1619 ConvertShellHandleToEfiFileProtocol(
1620 IN CONST SHELL_FILE_HANDLE Handle
1623 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1627 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1629 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1630 @param[in] Path The path to the file for verification.
1632 @return A SHELL_FILE_HANDLE representing the same file.
1633 @retval NULL There was not enough memory.
1637 ConvertEfiFileProtocolToShellHandle(
1638 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1639 IN CONST CHAR16
*Path
1642 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1643 BUFFER_LIST
*NewNode
;
1646 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1647 if (Buffer
== NULL
) {
1650 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1651 if (NewNode
== NULL
) {
1652 SHELL_FREE_NON_NULL(Buffer
);
1655 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1656 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1657 if (Buffer
->Path
== NULL
) {
1658 SHELL_FREE_NON_NULL(NewNode
);
1659 SHELL_FREE_NON_NULL(Buffer
);
1662 NewNode
->Buffer
= Buffer
;
1664 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1666 return ((SHELL_FILE_HANDLE
)(Handle
));
1670 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1672 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1674 @return A pointer to the path for the file.
1678 ShellFileHandleGetPath(
1679 IN CONST SHELL_FILE_HANDLE Handle
1684 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1685 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1686 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1688 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1689 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1696 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1698 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1700 @retval TRUE The item was removed.
1701 @retval FALSE The item was not found.
1705 ShellFileHandleRemove(
1706 IN CONST SHELL_FILE_HANDLE Handle
1711 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1712 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1713 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1715 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1716 RemoveEntryList(&Node
->Link
);
1717 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1718 SHELL_FREE_NON_NULL(Node
->Buffer
);
1719 SHELL_FREE_NON_NULL(Node
);
1727 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1729 This will NOT work on directories.
1731 If Handle is NULL, then ASSERT.
1733 @param[in] Handle the file handle
1735 @retval TRUE the position is at the end of the file
1736 @retval FALSE the position is not at the end of the file
1741 IN SHELL_FILE_HANDLE Handle
1744 EFI_FILE_INFO
*Info
;
1749 // ASSERT if Handle is NULL
1751 ASSERT(Handle
!= NULL
);
1753 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1754 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1755 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1761 if (Pos
== Info
->FileSize
) {
1773 Frees any BUFFER_LIST defined type.
1775 @param[in] List The BUFFER_LIST object to free.
1780 IN BUFFER_LIST
*List
1783 BUFFER_LIST
*BufferListEntry
;
1789 // enumerate through the buffer list and free all memory
1791 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1792 ; !IsListEmpty (&List
->Link
)
1793 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1795 RemoveEntryList(&BufferListEntry
->Link
);
1796 if (BufferListEntry
->Buffer
!= NULL
) {
1797 FreePool(BufferListEntry
->Buffer
);
1799 FreePool(BufferListEntry
);
1804 Dump some hexadecimal data to the screen.
1806 @param[in] Indent How many spaces to indent the output.
1807 @param[in] Offset The offset of the printing.
1808 @param[in] DataSize The size in bytes of UserData.
1809 @param[in] UserData The data to print out.
1831 while (DataSize
!= 0) {
1833 if (Size
> DataSize
) {
1837 for (Index
= 0; Index
< Size
; Index
+= 1) {
1838 TempByte
= Data
[Index
];
1839 Val
[Index
* 3 + 0] = Hex
[TempByte
>> 4];
1840 Val
[Index
* 3 + 1] = Hex
[TempByte
& 0xF];
1841 Val
[Index
* 3 + 2] = (CHAR8
) ((Index
== 7) ? '-' : ' ');
1842 Str
[Index
] = (CHAR8
) ((TempByte
< ' ' || TempByte
> '~') ? '.' : TempByte
);
1847 ShellPrintEx(-1, -1, L
"%*a%08X: %-48a *%a*\r\n", Indent
, "", Offset
, Val
, Str
);
1856 Dump HEX data into buffer.
1858 @param[in] Buffer HEX data to be dumped in Buffer.
1859 @param[in] Indent How many spaces to indent the output.
1860 @param[in] Offset The offset of the printing.
1861 @param[in] DataSize The size in bytes of UserData.
1862 @param[in] UserData The data to print out.
1885 while (DataSize
!= 0) {
1887 if (Size
> DataSize
) {
1891 for (Index
= 0; Index
< Size
; Index
+= 1) {
1892 TempByte
= Data
[Index
];
1893 Val
[Index
* 3 + 0] = Hex
[TempByte
>> 4];
1894 Val
[Index
* 3 + 1] = Hex
[TempByte
& 0xF];
1895 Val
[Index
* 3 + 2] = (CHAR8
) ((Index
== 7) ? '-' : ' ');
1896 Str
[Index
] = (CHAR8
) ((TempByte
< ' ' || TempByte
> 'z') ? '.' : TempByte
);
1901 TempRetVal
= CatSPrint (RetVal
, L
"%*a%08X: %-48a *%a*\r\n", Indent
, "", Offset
, Val
, Str
);
1902 SHELL_FREE_NON_NULL (RetVal
);
1903 RetVal
= TempRetVal
;
1914 ORDERED_COLLECTION_USER_COMPARE function for SHELL_SORT_UNIQUE_NAME objects.
1916 @param[in] Unique1AsVoid The first SHELL_SORT_UNIQUE_NAME object (Unique1),
1917 passed in as a pointer-to-VOID.
1919 @param[in] Unique2AsVoid The second SHELL_SORT_UNIQUE_NAME object (Unique2),
1920 passed in as a pointer-to-VOID.
1922 @retval <0 If Unique1 compares less than Unique2.
1924 @retval 0 If Unique1 compares equal to Unique2.
1926 @retval >0 If Unique1 compares greater than Unique2.
1932 IN CONST VOID
*Unique1AsVoid
,
1933 IN CONST VOID
*Unique2AsVoid
1936 CONST SHELL_SORT_UNIQUE_NAME
*Unique1
;
1937 CONST SHELL_SORT_UNIQUE_NAME
*Unique2
;
1939 Unique1
= Unique1AsVoid
;
1940 Unique2
= Unique2AsVoid
;
1943 // We need to cast away CONST for EFI_UNICODE_COLLATION_STRICOLL.
1945 return gUnicodeCollation
->StriColl (
1947 (CHAR16
*)Unique1
->Alias
,
1948 (CHAR16
*)Unique2
->Alias
1953 ORDERED_COLLECTION_KEY_COMPARE function for SHELL_SORT_UNIQUE_NAME objects.
1955 @param[in] UniqueAliasAsVoid The CHAR16 string UniqueAlias, passed in as a
1958 @param[in] UniqueAsVoid The SHELL_SORT_UNIQUE_NAME object (Unique),
1959 passed in as a pointer-to-VOID.
1961 @retval <0 If UniqueAlias compares less than Unique->Alias.
1963 @retval 0 If UniqueAlias compares equal to Unique->Alias.
1965 @retval >0 If UniqueAlias compares greater than Unique->Alias.
1970 UniqueNameAliasCompare (
1971 IN CONST VOID
*UniqueAliasAsVoid
,
1972 IN CONST VOID
*UniqueAsVoid
1975 CONST CHAR16
*UniqueAlias
;
1976 CONST SHELL_SORT_UNIQUE_NAME
*Unique
;
1978 UniqueAlias
= UniqueAliasAsVoid
;
1979 Unique
= UniqueAsVoid
;
1982 // We need to cast away CONST for EFI_UNICODE_COLLATION_STRICOLL.
1984 return gUnicodeCollation
->StriColl (
1986 (CHAR16
*)UniqueAlias
,
1987 (CHAR16
*)Unique
->Alias
1992 Sort an EFI_SHELL_FILE_INFO list, optionally moving duplicates to a separate
1995 @param[in,out] FileList The list of EFI_SHELL_FILE_INFO objects to sort.
1997 If FileList is NULL on input, then FileList is
1998 considered an empty, hence already sorted, list.
2000 Otherwise, if (*FileList) is NULL on input, then
2001 EFI_INVALID_PARAMETER is returned.
2003 Otherwise, the caller is responsible for having
2004 initialized (*FileList)->Link with
2005 InitializeListHead(). No other fields in the
2006 (**FileList) head element are accessed by this
2009 On output, (*FileList) is sorted according to Order.
2010 If Duplicates is NULL on input, then duplicate
2011 elements are preserved, sorted stably, on
2012 (*FileList). If Duplicates is not NULL on input,
2013 then duplicates are moved (stably sorted) to the
2014 new, dynamically allocated (*Duplicates) list.
2016 @param[out] Duplicates If Duplicates is NULL on input, (*FileList) will be
2017 a monotonically ordered list on output, with
2018 duplicates stably sorted.
2020 If Duplicates is not NULL on input, (*FileList) will
2021 be a strictly monotonically oredered list on output,
2022 with duplicates separated (stably sorted) to
2023 (*Duplicates). All fields except Link will be
2024 zero-initialized in the (**Duplicates) head element.
2025 If no duplicates exist, then (*Duplicates) is set to
2028 @param[in] Order Determines the comparison operation between
2029 EFI_SHELL_FILE_INFO objects.
2031 @retval EFI_INVALID_PARAMETER (UINTN)Order is greater than or equal to
2032 (UINTN)ShellSortFileListMax. Neither the
2033 (*FileList) nor the (*Duplicates) list has
2036 @retval EFI_INVALID_PARAMETER (*FileList) was NULL on input. Neither the
2037 (*FileList) nor the (*Duplicates) list has
2040 @retval EFI_OUT_OF_RESOURCES Memory allocation failed. Neither the
2041 (*FileList) nor the (*Duplicates) list has
2044 @retval EFI_SUCCESS Sorting successful, including the case when
2045 FileList is NULL on input.
2050 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
2051 OUT EFI_SHELL_FILE_INFO
**Duplicates OPTIONAL
,
2052 IN SHELL_SORT_FILE_LIST Order
2055 LIST_ENTRY
*FilesHead
;
2056 ORDERED_COLLECTION
*Sort
;
2057 LIST_ENTRY
*FileEntry
;
2058 EFI_SHELL_FILE_INFO
*FileInfo
;
2059 SHELL_SORT_UNIQUE_NAME
*Unique
;
2061 EFI_SHELL_FILE_INFO
*Dupes
;
2062 LIST_ENTRY
*NextFileEntry
;
2063 CONST CHAR16
*Alias
;
2064 ORDERED_COLLECTION_ENTRY
*SortEntry
;
2065 LIST_ENTRY
*TargetFileList
;
2066 ORDERED_COLLECTION_ENTRY
*NextSortEntry
;
2069 if ((UINTN
)Order
>= (UINTN
)ShellSortFileListMax
) {
2070 return EFI_INVALID_PARAMETER
;
2073 if (FileList
== NULL
) {
2075 // FileList is considered empty, hence already sorted, with no duplicates.
2077 if (Duplicates
!= NULL
) {
2083 if (*FileList
== NULL
) {
2084 return EFI_INVALID_PARAMETER
;
2086 FilesHead
= &(*FileList
)->Link
;
2089 // Collect all the unique names.
2091 Sort
= OrderedCollectionInit (UniqueNameCompare
, UniqueNameAliasCompare
);
2093 return EFI_OUT_OF_RESOURCES
;
2096 BASE_LIST_FOR_EACH (FileEntry
, FilesHead
) {
2097 FileInfo
= (EFI_SHELL_FILE_INFO
*)FileEntry
;
2100 // Try to record the name of this file as a unique name.
2102 Unique
= AllocatePool (sizeof (*Unique
));
2103 if (Unique
== NULL
) {
2104 Status
= EFI_OUT_OF_RESOURCES
;
2107 Unique
->Alias
= ((Order
== ShellSortFileListByFileName
) ?
2108 FileInfo
->FileName
:
2109 FileInfo
->FullName
);
2110 InitializeListHead (&Unique
->SameNameList
);
2112 Status
= OrderedCollectionInsert (Sort
, NULL
, Unique
);
2113 if (EFI_ERROR (Status
)) {
2115 // Only two errors are possible: memory allocation failed, or this name
2116 // has been encountered before. In either case, the
2117 // SHELL_SORT_UNIQUE_NAME object being constructed has to be released.
2121 // Memory allocation failure is fatal, while having seen the same name
2122 // before is normal.
2124 if (Status
== EFI_OUT_OF_RESOURCES
) {
2127 ASSERT (Status
== EFI_ALREADY_STARTED
);
2132 // If separation of duplicates has been requested, allocate the list for
2135 if (Duplicates
!= NULL
) {
2136 Dupes
= AllocateZeroPool (sizeof (*Dupes
));
2137 if (Dupes
== NULL
) {
2138 Status
= EFI_OUT_OF_RESOURCES
;
2141 InitializeListHead (&Dupes
->Link
);
2145 // No memory allocation beyond this point; thus, no chance to fail. We can
2146 // now migrate the EFI_SHELL_FILE_INFO objects from (*FileList) to Sort.
2148 BASE_LIST_FOR_EACH_SAFE (FileEntry
, NextFileEntry
, FilesHead
) {
2149 FileInfo
= (EFI_SHELL_FILE_INFO
*)FileEntry
;
2151 // Look up the SHELL_SORT_UNIQUE_NAME that matches FileInfo's name.
2153 Alias
= ((Order
== ShellSortFileListByFileName
) ?
2154 FileInfo
->FileName
:
2155 FileInfo
->FullName
);
2156 SortEntry
= OrderedCollectionFind (Sort
, Alias
);
2157 ASSERT (SortEntry
!= NULL
);
2158 Unique
= OrderedCollectionUserStruct (SortEntry
);
2160 // Move FileInfo from (*FileList) to the end of the list of files whose
2161 // names all compare identical to FileInfo's name.
2163 RemoveEntryList (&FileInfo
->Link
);
2164 InsertTailList (&Unique
->SameNameList
, &FileInfo
->Link
);
2168 // All EFI_SHELL_FILE_INFO objects originally in (*FileList) have been
2169 // distributed to Sort. Now migrate them back to (*FileList), advancing in
2170 // unique name order.
2172 for (SortEntry
= OrderedCollectionMin (Sort
);
2174 SortEntry
= OrderedCollectionNext (SortEntry
)) {
2175 Unique
= OrderedCollectionUserStruct (SortEntry
);
2177 // The first FileInfo encountered for each unique name goes back on
2178 // (*FileList) unconditionally. Further FileInfo instances for the same
2179 // unique name -- that is, duplicates -- are either returned to (*FileList)
2180 // or separated, dependent on the caller's request.
2182 TargetFileList
= FilesHead
;
2183 BASE_LIST_FOR_EACH_SAFE (FileEntry
, NextFileEntry
, &Unique
->SameNameList
) {
2184 RemoveEntryList (FileEntry
);
2185 InsertTailList (TargetFileList
, FileEntry
);
2186 if (Duplicates
!= NULL
) {
2187 TargetFileList
= &Dupes
->Link
;
2193 // We're done. If separation of duplicates has been requested, output the
2194 // list of duplicates -- and free that list at once, if it's empty (i.e., if
2195 // no duplicates have been found).
2197 if (Duplicates
!= NULL
) {
2198 if (IsListEmpty (&Dupes
->Link
)) {
2202 *Duplicates
= Dupes
;
2205 Status
= EFI_SUCCESS
;
2211 for (SortEntry
= OrderedCollectionMin (Sort
);
2213 SortEntry
= NextSortEntry
) {
2214 NextSortEntry
= OrderedCollectionNext (SortEntry
);
2215 OrderedCollectionDelete (Sort
, SortEntry
, &UniqueAsVoid
);
2216 Unique
= UniqueAsVoid
;
2217 ASSERT (IsListEmpty (&Unique
->SameNameList
));
2220 OrderedCollectionUninit (Sort
);