2 Provides interface to shell internal functions for shell commands.
4 This library is for use ONLY by shell commands linked into the shell application.
5 This library will not function if it is used for UEFI Shell 2.0 Applications.
7 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
8 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
9 (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #ifndef _SHELL_COMMAND_LIB_
15 #define _SHELL_COMMAND_LIB_
19 #include <Protocol/Shell.h>
20 #include <Protocol/ShellParameters.h>
21 #include <Protocol/UnicodeCollation.h>
22 #include <Protocol/SimpleFileSystem.h>
24 #include <Library/UefiBootServicesTableLib.h>
27 // The extern global protocol poionters.
29 extern EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
;
30 extern CONST CHAR16
*SupportLevel
[];
33 // The map list objects.
37 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
39 CHAR16
*CurrentDirectoryPath
;
42 /// List of Mappings - DeviceName and Drive Letter(ism).
43 extern SHELL_MAP_LIST gShellMapList
;
44 /// Pointer to node of current directory in the mMapList.
45 extern SHELL_MAP_LIST
*gShellCurMapping
;
48 Returns the help MAN fileName for a given shell command.
50 @retval !NULL The unicode string of the MAN filename.
51 @retval NULL An error ocurred.
56 (EFIAPI
*SHELL_GET_MAN_FILENAME
)(
61 Runs a shell command on a given command line.
63 The specific operation of a given shell command is specified in the UEFI Shell
64 Specification 2.0, or in the source of the given command.
66 Upon completion of the command run the shell protocol and environment variables
67 may have been updated due to the operation.
69 @param[in] ImageHandle The ImageHandle to the app, or NULL if
70 the command built into shell.
71 @param[in] SystemTable The pointer to the system table.
73 @retval RETURN_SUCCESS The shell command was sucessful.
74 @retval RETURN_UNSUPPORTED The command is not supported.
78 (EFIAPI
*SHELL_RUN_COMMAND
)(
79 IN EFI_HANDLE ImageHandle
,
80 IN EFI_SYSTEM_TABLE
*SystemTable
84 Registers the handlers of type SHELL_RUN_COMMAND and
85 SHELL_GET_MAN_FILENAME for each shell command.
87 If the ShellSupportLevel is greater than the value of
88 PcdShellSupportLevel, then return RETURN_UNSUPPORTED.
90 Registers the the handlers specified by GetHelpInfoHandler and CommandHandler
91 with the command specified by CommandString. If the command named by
92 CommandString has already been registered, then return
93 RETURN_ALREADY_STARTED.
95 If there are not enough resources available to register the handlers, then
96 RETURN_OUT_OF_RESOURCES is returned.
98 If CommandString is NULL, then ASSERT().
99 If GetHelpInfoHandler is NULL, then ASSERT().
100 If CommandHandler is NULL, then ASSERT().
101 If ProfileName is NULL, then ASSERT().
103 @param[in] CommandString The pointer to the command name. This is the
104 name to look for on the command line in
106 @param[in] CommandHandler The pointer to a function that runs the
108 @param[in] GetManFileName The pointer to a function that provides man
110 @param[in] ShellMinSupportLevel The minimum Shell Support Level which has this
112 @param[in] ProfileName The profile name to require for support of this
114 @param[in] CanAffectLE Indicates whether this command's return value
115 can change the LASTERROR environment variable.
116 @param[in] HiiHandle The handle of this command's HII entry.
117 @param[in] ManFormatHelp The HII locator for the help text.
119 @retval RETURN_SUCCESS The handlers were registered.
120 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
121 register the shell command.
122 @retval RETURN_UNSUPPORTED The ShellMinSupportLevel was higher than the
123 currently allowed support level.
124 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
125 is already registered. Only one handler set for
126 a given command is allowed.
127 @sa SHELL_GET_MAN_FILENAME
128 @sa SHELL_RUN_COMMAND
132 ShellCommandRegisterCommandName (
133 IN CONST CHAR16
*CommandString
,
134 IN SHELL_RUN_COMMAND CommandHandler
,
135 IN SHELL_GET_MAN_FILENAME GetManFileName
,
136 IN UINT32 ShellMinSupportLevel
,
137 IN CONST CHAR16
*ProfileName
,
138 IN CONST BOOLEAN CanAffectLE
,
139 IN CONST EFI_HII_HANDLE HiiHandle
,
140 IN CONST EFI_STRING_ID ManFormatHelp
144 Checks if a command string has been registered for CommandString, and if so, it runs
145 the previously registered handler for that command with the command line.
147 If CommandString is NULL, then ASSERT().
149 If Sections is specified, then each section name listed will be compared in a case sensitive
150 manner to the section names described in Appendix B UEFI Shell 2.0 Specification. If the section exists,
151 it is appended to the returned help text. If the section does not exist, no
152 information is returned. If Sections is NULL, then all help text information
153 available is returned.
155 @param[in] CommandString The pointer to the command name. This is the name
156 found on the command line in the shell.
157 @param[in, out] RetVal The pointer to the return value from the command handler.
159 @param[in, out] CanAffectLE Indicates whether this command's return value
160 needs to be placed into LASTERROR environment variable.
162 @retval RETURN_SUCCESS The handler was run.
163 @retval RETURN_NOT_FOUND The CommandString did not match a registered
165 @sa SHELL_RUN_COMMAND
169 ShellCommandRunCommandHandler (
170 IN CONST CHAR16
*CommandString
,
171 IN OUT SHELL_STATUS
*RetVal
,
172 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
176 Checks if a command string has been registered for CommandString, and if so, it
177 returns the MAN filename specified for that command.
179 If CommandString is NULL, then ASSERT().
181 @param[in] CommandString The pointer to the command name. This is the name
182 found on the command line in the shell.
184 @retval NULL The CommandString was not a registered command.
185 @retval other The name of the MAN file.
186 @sa SHELL_GET_MAN_FILENAME
190 ShellCommandGetManFileNameHandler (
191 IN CONST CHAR16
*CommandString
196 CHAR16
*CommandString
;
200 Get the list of all available shell internal commands. This is a linked list,
201 via the LIST_ENTRY structure. Enumerate through it using the BaseLib linked
202 list functions. Do not modify the values.
204 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
206 @return A linked list of all available shell commands.
210 ShellCommandGetCommandList (
211 IN CONST BOOLEAN Sort
216 CHAR16
*CommandString
;
221 Registers aliases to be set as part of the initialization of the shell application.
223 If Command is NULL, then ASSERT().
224 If Alias is NULL, then ASSERT().
226 @param[in] Command The pointer to the Command.
227 @param[in] Alias The pointer to Alias.
229 @retval RETURN_SUCCESS The handlers were registered.
230 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
231 register the shell command.
235 ShellCommandRegisterAlias (
236 IN CONST CHAR16
*Command
,
237 IN CONST CHAR16
*Alias
241 Get the list of all shell alias commands. This is a linked list,
242 via LIST_ENTRY structure. Enumerate through it using the BaseLib linked
243 list functions. Do not modify the values.
245 @return A linked list of all requested shell aliases.
249 ShellCommandGetInitAliasList (
254 Determine if a given alias is on the list of built in aliases.
256 @param[in] Alias The alias to test for.
258 @retval TRUE The alias is a built in alias.
259 @retval FALSE The alias is not a built in alias.
263 ShellCommandIsOnAliasList (
264 IN CONST CHAR16
*Alias
268 Checks if a command is already on the list.
270 @param[in] CommandString The command string to check for on the list.
272 @retval TRUE CommandString represents a registered command.
273 @retval FALSE CommandString does not represent a registered command.
277 ShellCommandIsCommandOnList (
278 IN CONST CHAR16
*CommandString
282 Get the help text for a command.
284 @param[in] CommandString The command name.
286 @retval NULL No help text was found.
287 @return The string of the help text. The caller required to free.
291 ShellCommandGetCommandHelp (
292 IN CONST CHAR16
*CommandString
296 Function to make sure that the above pointers are valid.
305 Function to determine current state of ECHO. Echo determines if lines from scripts
306 and ECHO commands are enabled.
308 @retval TRUE Echo is currently enabled.
309 @retval FALSE Echo is currently disabled.
313 ShellCommandGetEchoState (
318 Function to set current state of ECHO. Echo determines if lines from scripts
319 and ECHO commands are enabled.
321 @param[in] State TRUE to enable Echo, FALSE otherwise.
325 ShellCommandSetEchoState (
330 Indicate that the current shell or script should exit.
332 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
333 @param[in] ErrorCode The 64 bit error code to return.
337 ShellCommandRegisterExit (
338 IN BOOLEAN ScriptOnly
,
339 IN CONST UINT64 ErrorCode
343 Retrieve the Exit code.
345 @return the value passed into RegisterExit.
349 ShellCommandGetExitCode (
354 Retrieve the Exit indicator.
356 @retval TRUE Exit was indicated.
357 @retval FALSE Exit was not indicated.
361 ShellCommandGetExit (
366 Retrieve the Exit script indicator.
368 If ShellCommandGetExit returns FALSE, then the return from this is undefined.
370 @retval TRUE ScriptOnly was indicated.
371 @retval FALSE ScriptOnly was not indicated.
375 ShellCommandGetScriptExit (
380 LIST_ENTRY Link
; ///< List enumerator items.
381 UINTN Line
; ///< What line of the script file this was on.
382 CHAR16
*Cl
; ///< The original command line.
383 VOID
*Data
; ///< The data structure format dependant upon Command. (not always used)
384 BOOLEAN Reset
; ///< Reset the command (it must be treated like a initial run (but it may have data already))
385 } SCRIPT_COMMAND_LIST
;
388 CHAR16
*ScriptName
; ///< The filename of this script.
389 CHAR16
**Argv
; ///< The parmameters to the script file.
390 UINTN Argc
; ///< The count of parameters.
391 LIST_ENTRY CommandList
; ///< The script converted to a list of commands (SCRIPT_COMMAND_LIST objects).
392 SCRIPT_COMMAND_LIST
*CurrentCommand
; ///< The command currently being operated. If !=NULL must be a member of CommandList.
393 LIST_ENTRY SubstList
; ///< A list of current script loop alias' (ALIAS_LIST objects) (Used for the for %-based replacement).
397 Function to return a pointer to the currently running script file object.
399 @retval NULL A script file is not currently running.
400 @return A pointer to the current script file object.
404 ShellCommandGetCurrentScriptFile (
409 Function to set a new script as the currently running one.
411 This function will correctly stack and unstack nested scripts.
413 @param[in] Script The pointer to new script information structure. If NULL,
414 it removes and de-allocates the topmost Script structure.
416 @return A pointer to the current running script file after this
417 change. It is NULL if removing the final script.
421 ShellCommandSetNewScript (
422 IN SCRIPT_FILE
*Script OPTIONAL
426 Function to cleanup all memory from a SCRIPT_FILE structure.
428 @param[in] Script The pointer to the structure to cleanup.
432 DeleteScriptFileStruct (
433 IN SCRIPT_FILE
*Script
437 Function to get the current Profile string.
439 This is used to retrieve what profiles were installed.
441 @retval NULL There are no installed profiles.
442 @return A semicolon-delimited list of profiles.
446 ShellCommandGetProfileList (
451 MappingTypeFileSystem
,
454 } SHELL_MAPPING_TYPE
;
457 Function to generate the next default mapping name.
459 If the return value is not NULL then it must be callee freed.
461 @param Type What kind of mapping name to make.
463 @retval NULL a memory allocation failed.
464 @return a new map name string
468 ShellCommandCreateNewMappingName (
469 IN CONST SHELL_MAPPING_TYPE Type
473 Function to initialize the table for creating consistent map names.
475 @param[out] Table The pointer to pointer to pointer to DevicePathProtocol object.
477 @retval EFI_SUCCESS The table was created successfully.
481 ShellCommandConsistMappingInitialize (
482 EFI_DEVICE_PATH_PROTOCOL
***Table
486 Function to uninitialize the table for creating consistent map names.
488 The parameter must have been received from ShellCommandConsistMappingInitialize.
490 @param[out] Table The pointer to pointer to DevicePathProtocol object.
492 @retval EFI_SUCCESS The table was deleted successfully.
496 ShellCommandConsistMappingUnInitialize (
497 EFI_DEVICE_PATH_PROTOCOL
**Table
501 Create a consistent mapped name for the device specified by DevicePath
504 This must be called after ShellCommandConsistMappingInitialize() and
505 before ShellCommandConsistMappingUnInitialize() is called.
507 @param[in] DevicePath The pointer to the dev path for the device.
508 @param[in] Table The Table of mapping information.
510 @retval NULL A consistent mapped name could not be created.
511 @return A pointer to a string allocated from pool with the device name.
515 ShellCommandConsistMappingGenMappingName (
516 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
517 IN EFI_DEVICE_PATH_PROTOCOL
**Table
521 Function to search the list of mappings for the first matching node on the
522 list based on the MapKey.
524 @param[in] MapKey The pointer to the string key to search for in the map.
526 @return the node on the list.
530 ShellCommandFindMapItem (
531 IN CONST CHAR16
*MapKey
535 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
537 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
538 default paths on the new map name...
540 Path should be FALSE when this function is called from the protocol SetMap function.
542 @param[in] Name The human readable mapped name.
543 @param[in] DevicePath The Device Path for this map.
544 @param[in] Flags The Flags attribute for this map item.
545 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
547 @retval EFI_SUCCESS The addition was sucessful.
548 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
549 @retval EFI_INVALID_PARAMETER A parameter was invalid.
553 ShellCommandAddMapItemAndUpdatePath (
554 IN CONST CHAR16
*Name
,
555 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
556 IN CONST UINT64 Flags
,
557 IN CONST BOOLEAN Path
561 Creates the default map names for each device path in the system with
562 a protocol depending on the Type.
564 Also sets up the default path environment variable if Type is FileSystem.
566 @retval EFI_SUCCESS All map names were created sucessfully.
567 @retval EFI_NOT_FOUND No protocols were found in the system.
568 @return Error returned from gBS->LocateHandle().
574 ShellCommandCreateInitialMappingsAndPaths (
579 Add mappings for any devices without one. Do not change any existing maps.
581 @retval EFI_SUCCESS The operation was successful.
585 ShellCommandUpdateMapping (
590 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
592 @param[in] Handle The SHELL_FILE_HANDLE to convert.
594 @return a EFI_FILE_PROTOCOL* representing the same file.
598 ConvertShellHandleToEfiFileProtocol (
599 IN CONST SHELL_FILE_HANDLE Handle
603 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.
605 @param[in] Handle The SHELL_FILE_HANDLE to remove.
607 @retval TRUE The item was removed.
608 @retval FALSE The item was not found.
612 ShellFileHandleRemove (
613 IN CONST SHELL_FILE_HANDLE Handle
617 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
619 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
620 @param[in] Path The path to the file for verification.
622 @return a SHELL_FILE_HANDLE representing the same file.
626 ConvertEfiFileProtocolToShellHandle (
627 IN CONST EFI_FILE_PROTOCOL
*Handle
,
628 IN CONST CHAR16
*Path
632 Find the path that was logged with the specified SHELL_FILE_HANDLE.
634 @param[in] Handle The SHELL_FILE_HANDLE to query on.
636 @return A pointer to the path for the file.
640 ShellFileHandleGetPath (
641 IN CONST SHELL_FILE_HANDLE Handle
645 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
647 This will NOT work on directories.
649 If Handle is NULL, then ASSERT.
651 @param[in] Handle the file handle
653 @retval TRUE the position is at the end of the file
654 @retval FALSE the position is not at the end of the file
659 IN SHELL_FILE_HANDLE Handle
668 Frees any BUFFER_LIST defined type.
670 @param[in] List The pointer to the list head.
679 Function printing hex output to the console.
681 @param[in] Indent Number of spaces to indent.
682 @param[in] Offset Offset to start with.
683 @param[in] DataSize Length of data.
684 @param[in] UserData Pointer to some data.
696 Dump HEX data into buffer.
698 @param[in] Buffer HEX data to be dumped in Buffer.
699 @param[in] Indent How many spaces to indent the output.
700 @param[in] Offset The offset of the printing.
701 @param[in] DataSize The size in bytes of UserData.
702 @param[in] UserData The data to print out.
715 // Determines the ordering operation for ShellSortFileList().
719 // Sort the EFI_SHELL_FILE_INFO objects by the FileName field, in increasing
720 // order, using gUnicodeCollation->StriColl().
722 ShellSortFileListByFileName
,
724 // Sort the EFI_SHELL_FILE_INFO objects by the FullName field, in increasing
725 // order, using gUnicodeCollation->StriColl().
727 ShellSortFileListByFullName
,
729 } SHELL_SORT_FILE_LIST
;
732 Sort an EFI_SHELL_FILE_INFO list, optionally moving duplicates to a separate
735 @param[in,out] FileList The list of EFI_SHELL_FILE_INFO objects to sort.
737 If FileList is NULL on input, then FileList is
738 considered an empty, hence already sorted, list.
740 Otherwise, if (*FileList) is NULL on input, then
741 EFI_INVALID_PARAMETER is returned.
743 Otherwise, the caller is responsible for having
744 initialized (*FileList)->Link with
745 InitializeListHead(). No other fields in the
746 (**FileList) head element are accessed by this
749 On output, (*FileList) is sorted according to Order.
750 If Duplicates is NULL on input, then duplicate
751 elements are preserved, sorted stably, on
752 (*FileList). If Duplicates is not NULL on input,
753 then duplicates are moved (stably sorted) to the
754 new, dynamically allocated (*Duplicates) list.
756 @param[out] Duplicates If Duplicates is NULL on input, (*FileList) will be
757 a monotonically ordered list on output, with
758 duplicates stably sorted.
760 If Duplicates is not NULL on input, (*FileList) will
761 be a strictly monotonically oredered list on output,
762 with duplicates separated (stably sorted) to
763 (*Duplicates). All fields except Link will be
764 zero-initialized in the (**Duplicates) head element.
765 If no duplicates exist, then (*Duplicates) is set to
768 @param[in] Order Determines the comparison operation between
769 EFI_SHELL_FILE_INFO objects.
771 @retval EFI_INVALID_PARAMETER (UINTN)Order is greater than or equal to
772 (UINTN)ShellSortFileListMax. Neither the
773 (*FileList) nor the (*Duplicates) list has
776 @retval EFI_INVALID_PARAMETER (*FileList) was NULL on input. Neither the
777 (*FileList) nor the (*Duplicates) list has
780 @retval EFI_OUT_OF_RESOURCES Memory allocation failed. Neither the
781 (*FileList) nor the (*Duplicates) list has
784 @retval EFI_SUCCESS Sorting successful, including the case when
785 FileList is NULL on input.
790 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
791 OUT EFI_SHELL_FILE_INFO
**Duplicates OPTIONAL
,
792 IN SHELL_SORT_FILE_LIST Order
795 #endif //_SHELL_COMMAND_LIB_