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
197 CHAR16
*CommandString
;
201 Get the list of all available shell internal commands. This is a linked list,
202 via the LIST_ENTRY structure. Enumerate through it using the BaseLib linked
203 list functions. Do not modify the values.
205 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
207 @return A linked list of all available shell commands.
211 ShellCommandGetCommandList (
212 IN CONST BOOLEAN Sort
217 CHAR16
*CommandString
;
222 Registers aliases to be set as part of the initialization of the shell application.
224 If Command is NULL, then ASSERT().
225 If Alias is NULL, then ASSERT().
227 @param[in] Command The pointer to the Command.
228 @param[in] Alias The pointer to Alias.
230 @retval RETURN_SUCCESS The handlers were registered.
231 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
232 register the shell command.
236 ShellCommandRegisterAlias (
237 IN CONST CHAR16
*Command
,
238 IN CONST CHAR16
*Alias
242 Get the list of all shell alias commands. This is a linked list,
243 via LIST_ENTRY structure. Enumerate through it using the BaseLib linked
244 list functions. Do not modify the values.
246 @return A linked list of all requested shell aliases.
250 ShellCommandGetInitAliasList (
255 Determine if a given alias is on the list of built in aliases.
257 @param[in] Alias The alias to test for.
259 @retval TRUE The alias is a built in alias.
260 @retval FALSE The alias is not a built in alias.
264 ShellCommandIsOnAliasList (
265 IN CONST CHAR16
*Alias
269 Checks if a command is already on the list.
271 @param[in] CommandString The command string to check for on the list.
273 @retval TRUE CommandString represents a registered command.
274 @retval FALSE CommandString does not represent a registered command.
278 ShellCommandIsCommandOnList (
279 IN CONST CHAR16
*CommandString
283 Get the help text for a command.
285 @param[in] CommandString The command name.
287 @retval NULL No help text was found.
288 @return The string of the help text. The caller required to free.
292 ShellCommandGetCommandHelp (
293 IN CONST CHAR16
*CommandString
297 Function to make sure that the above pointers are valid.
306 Function to determine current state of ECHO. Echo determines if lines from scripts
307 and ECHO commands are enabled.
309 @retval TRUE Echo is currently enabled.
310 @retval FALSE Echo is currently disabled.
314 ShellCommandGetEchoState (
319 Function to set current state of ECHO. Echo determines if lines from scripts
320 and ECHO commands are enabled.
322 @param[in] State TRUE to enable Echo, FALSE otherwise.
326 ShellCommandSetEchoState (
333 Indicate that the current shell or script should exit.
335 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
336 @param[in] ErrorCode The 64 bit error code to return.
340 ShellCommandRegisterExit (
341 IN BOOLEAN ScriptOnly
,
342 IN CONST UINT64 ErrorCode
346 Retrieve the Exit code.
348 @return the value passed into RegisterExit.
352 ShellCommandGetExitCode (
357 Retrieve the Exit indicator.
359 @retval TRUE Exit was indicated.
360 @retval FALSE Exit was not indicated.
364 ShellCommandGetExit (
369 Retrieve the Exit script indicator.
371 If ShellCommandGetExit returns FALSE, then the return from this is undefined.
373 @retval TRUE ScriptOnly was indicated.
374 @retval FALSE ScriptOnly was not indicated.
378 ShellCommandGetScriptExit (
383 LIST_ENTRY Link
; ///< List enumerator items.
384 UINTN Line
; ///< What line of the script file this was on.
385 CHAR16
*Cl
; ///< The original command line.
386 VOID
*Data
; ///< The data structure format dependant upon Command. (not always used)
387 BOOLEAN Reset
; ///< Reset the command (it must be treated like a initial run (but it may have data already))
388 } SCRIPT_COMMAND_LIST
;
391 CHAR16
*ScriptName
; ///< The filename of this script.
392 CHAR16
**Argv
; ///< The parmameters to the script file.
393 UINTN Argc
; ///< The count of parameters.
394 LIST_ENTRY CommandList
; ///< The script converted to a list of commands (SCRIPT_COMMAND_LIST objects).
395 SCRIPT_COMMAND_LIST
*CurrentCommand
; ///< The command currently being operated. If !=NULL must be a member of CommandList.
396 LIST_ENTRY SubstList
; ///< A list of current script loop alias' (ALIAS_LIST objects) (Used for the for %-based replacement).
400 Function to return a pointer to the currently running script file object.
402 @retval NULL A script file is not currently running.
403 @return A pointer to the current script file object.
407 ShellCommandGetCurrentScriptFile (
412 Function to set a new script as the currently running one.
414 This function will correctly stack and unstack nested scripts.
416 @param[in] Script The pointer to new script information structure. If NULL,
417 it removes and de-allocates the topmost Script structure.
419 @return A pointer to the current running script file after this
420 change. It is NULL if removing the final script.
424 ShellCommandSetNewScript (
425 IN SCRIPT_FILE
*Script OPTIONAL
429 Function to cleanup all memory from a SCRIPT_FILE structure.
431 @param[in] Script The pointer to the structure to cleanup.
435 DeleteScriptFileStruct (
436 IN SCRIPT_FILE
*Script
440 Function to get the current Profile string.
442 This is used to retrieve what profiles were installed.
444 @retval NULL There are no installed profiles.
445 @return A semicolon-delimited list of profiles.
449 ShellCommandGetProfileList (
454 MappingTypeFileSystem
,
457 } SHELL_MAPPING_TYPE
;
460 Function to generate the next default mapping name.
462 If the return value is not NULL then it must be callee freed.
464 @param Type What kind of mapping name to make.
466 @retval NULL a memory allocation failed.
467 @return a new map name string
471 ShellCommandCreateNewMappingName(
472 IN CONST SHELL_MAPPING_TYPE Type
476 Function to initialize the table for creating consistent map names.
478 @param[out] Table The pointer to pointer to pointer to DevicePathProtocol object.
480 @retval EFI_SUCCESS The table was created successfully.
484 ShellCommandConsistMappingInitialize (
485 EFI_DEVICE_PATH_PROTOCOL
***Table
489 Function to uninitialize the table for creating consistent map names.
491 The parameter must have been received from ShellCommandConsistMappingInitialize.
493 @param[out] Table The pointer to pointer to DevicePathProtocol object.
495 @retval EFI_SUCCESS The table was deleted successfully.
499 ShellCommandConsistMappingUnInitialize (
500 EFI_DEVICE_PATH_PROTOCOL
**Table
504 Create a consistent mapped name for the device specified by DevicePath
507 This must be called after ShellCommandConsistMappingInitialize() and
508 before ShellCommandConsistMappingUnInitialize() is called.
510 @param[in] DevicePath The pointer to the dev path for the device.
511 @param[in] Table The Table of mapping information.
513 @retval NULL A consistent mapped name could not be created.
514 @return A pointer to a string allocated from pool with the device name.
518 ShellCommandConsistMappingGenMappingName (
519 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
520 IN EFI_DEVICE_PATH_PROTOCOL
**Table
524 Function to search the list of mappings for the first matching node on the
525 list based on the MapKey.
527 @param[in] MapKey The pointer to the string key to search for in the map.
529 @return the node on the list.
533 ShellCommandFindMapItem (
534 IN CONST CHAR16
*MapKey
538 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
540 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
541 default paths on the new map name...
543 Path should be FALSE when this function is called from the protocol SetMap function.
545 @param[in] Name The human readable mapped name.
546 @param[in] DevicePath The Device Path for this map.
547 @param[in] Flags The Flags attribute for this map item.
548 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
550 @retval EFI_SUCCESS The addition was sucessful.
551 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
552 @retval EFI_INVALID_PARAMETER A parameter was invalid.
556 ShellCommandAddMapItemAndUpdatePath(
557 IN CONST CHAR16
*Name
,
558 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
559 IN CONST UINT64 Flags
,
560 IN CONST BOOLEAN Path
564 Creates the default map names for each device path in the system with
565 a protocol depending on the Type.
567 Also sets up the default path environment variable if Type is FileSystem.
569 @retval EFI_SUCCESS All map names were created sucessfully.
570 @retval EFI_NOT_FOUND No protocols were found in the system.
571 @return Error returned from gBS->LocateHandle().
577 ShellCommandCreateInitialMappingsAndPaths(
582 Add mappings for any devices without one. Do not change any existing maps.
584 @retval EFI_SUCCESS The operation was successful.
588 ShellCommandUpdateMapping (
593 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
595 @param[in] Handle The SHELL_FILE_HANDLE to convert.
597 @return a EFI_FILE_PROTOCOL* representing the same file.
601 ConvertShellHandleToEfiFileProtocol(
602 IN CONST SHELL_FILE_HANDLE Handle
606 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.
608 @param[in] Handle The SHELL_FILE_HANDLE to remove.
610 @retval TRUE The item was removed.
611 @retval FALSE The item was not found.
615 ShellFileHandleRemove(
616 IN CONST SHELL_FILE_HANDLE Handle
620 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
622 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
623 @param[in] Path The path to the file for verification.
625 @return a SHELL_FILE_HANDLE representing the same file.
629 ConvertEfiFileProtocolToShellHandle(
630 IN CONST EFI_FILE_PROTOCOL
*Handle
,
631 IN CONST CHAR16
*Path
635 Find the path that was logged with the specified SHELL_FILE_HANDLE.
637 @param[in] Handle The SHELL_FILE_HANDLE to query on.
639 @return A pointer to the path for the file.
643 ShellFileHandleGetPath(
644 IN CONST SHELL_FILE_HANDLE Handle
649 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
651 This will NOT work on directories.
653 If Handle is NULL, then ASSERT.
655 @param[in] Handle the file handle
657 @retval TRUE the position is at the end of the file
658 @retval FALSE the position is not at the end of the file
663 IN SHELL_FILE_HANDLE Handle
672 Frees any BUFFER_LIST defined type.
674 @param[in] List The pointer to the list head.
683 Function printing hex output to the console.
685 @param[in] Indent Number of spaces to indent.
686 @param[in] Offset Offset to start with.
687 @param[in] DataSize Length of data.
688 @param[in] UserData Pointer to some data.
700 Dump HEX data into buffer.
702 @param[in] Buffer HEX data to be dumped in Buffer.
703 @param[in] Indent How many spaces to indent the output.
704 @param[in] Offset The offset of the printing.
705 @param[in] DataSize The size in bytes of UserData.
706 @param[in] UserData The data to print out.
717 #endif //_SHELL_COMMAND_LIB_