]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Include/Library/ShellCommandLib.h
5bc03ce256cc20f45526d95fa068f1621add7cb2
[mirror_edk2.git] / ShellPkg / Include / Library / ShellCommandLib.h
1 /** @file
2 Provides interface to shell internal functions for shell commands.
3
4 This library is for use ONLY by shell commands linked into the shell application.
5 This library will not funciton if it is used for UEFI Shell 2.0 Applications.
6
7 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #ifndef _SHELL_COMMAND_LIB_
19 #define _SHELL_COMMAND_LIB_
20
21 #include <Uefi.h>
22 #include <ShellBase.h>
23
24 #include <Protocol/EfiShell.h>
25 #include <Protocol/EfiShellParameters.h>
26 #include <Protocol/UnicodeCollation.h>
27 #include <Protocol/DevicePathToText.h>
28 #include <Protocol/SimpleFileSystem.h>
29
30 #include <Library/UefiBootServicesTableLib.h>
31
32 //
33 // The extern global protocol poionters.
34 //
35 extern EFI_UNICODE_COLLATION_PROTOCOL *gUnicodeCollation;
36 extern EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *gDevPathToText;
37 extern CONST CHAR16* SupportLevel[];
38
39 //
40 // The map list objects.
41 //
42 typedef struct {
43 LIST_ENTRY Link;
44 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
45 CHAR16 *MapName;
46 CHAR16 *CurrentDirectoryPath;
47 UINT64 Flags;
48 } SHELL_MAP_LIST;
49 /// List of Mappings - DeviceName and Drive Letter(ism).
50 extern SHELL_MAP_LIST gShellMapList;
51 /// Pointer to node of current directory in the mMapList.
52 extern SHELL_MAP_LIST *gShellCurDir;
53
54 /**
55 Returns the help MAN fileName for a given shell command.
56
57 @retval !NULL The unicode string of the MAN filename.
58 @retval NULL An error ocurred.
59
60 **/
61 typedef
62 CONST CHAR16 *
63 (EFIAPI *SHELL_GET_MAN_FILENAME)(
64 VOID
65 );
66
67 /**
68 Runs a shell command on a given command line.
69
70 The specific operation of a given shell command is specified in the UEFI Shell
71 Specification 2.0, or in the source of the given command.
72
73 Upon completion of the command run the shell protocol and environment variables
74 may have been updated due to the operation.
75
76 @param[in] ImageHandle The ImageHandle to the app, or NULL if
77 the command built into shell.
78 @param[in] SystemTable The pointer to the system table.
79
80 @retval RETURN_SUCCESS The shell command was sucessful.
81 @retval RETURN_UNSUPPORTED The command is not supported.
82 **/
83 typedef
84 SHELL_STATUS
85 (EFIAPI *SHELL_RUN_COMMAND)(
86 IN EFI_HANDLE ImageHandle,
87 IN EFI_SYSTEM_TABLE *SystemTable
88 );
89
90 /**
91 Registers the handlers of type SHELL_RUN_COMMAND and
92 SHELL_GET_MAN_FILENAME for each shell command.
93
94 If the ShellSupportLevel is greater than the value of
95 PcdShellSupportLevel, then return RETURN_UNSUPPORTED.
96
97 Registers the the handlers specified by GetHelpInfoHandler and CommandHandler
98 with the command specified by CommandString. If the command named by
99 CommandString has already been registered, then return
100 RETURN_ALREADY_STARTED.
101
102 If there are not enough resources available to register the handlers, then
103 RETURN_OUT_OF_RESOURCES is returned.
104
105 If CommandString is NULL, then ASSERT().
106 If GetHelpInfoHandler is NULL, then ASSERT().
107 If CommandHandler is NULL, then ASSERT().
108 If ProfileName is NULL, then ASSERT().
109
110 @param[in] CommandString The pointer to the command name. This is the
111 name to look for on the command line in
112 the shell.
113 @param[in] CommandHandler The pointer to a function that runs the
114 specified command.
115 @param[in] GetManFileName The pointer to a function that provides man
116 filename.
117 @param[in] ShellMinSupportLevel The minimum Shell Support Level which has this
118 function.
119 @param[in] ProfileName The profile name to require for support of this
120 function.
121 @param[in] CanAffectLE Indicates whether this command's return value
122 can change the LASTERROR environment variable.
123 @param[in] HiiHandle The handle of this command's HII entry.
124 @param[in] ManFormatHelp The HII locator for the help text.
125
126 @retval RETURN_SUCCESS The handlers were registered.
127 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
128 register the shell command.
129 @retval RETURN_UNSUPPORTED The ShellMinSupportLevel was higher than the
130 currently allowed support level.
131 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
132 is already registered. Only one handler set for
133 a given command is allowed.
134 @sa SHELL_GET_MAN_FILENAME
135 @sa SHELL_RUN_COMMAND
136 **/
137 RETURN_STATUS
138 EFIAPI
139 ShellCommandRegisterCommandName (
140 IN CONST CHAR16 *CommandString,
141 IN SHELL_RUN_COMMAND CommandHandler,
142 IN SHELL_GET_MAN_FILENAME GetManFileName,
143 IN UINT32 ShellMinSupportLevel,
144 IN CONST CHAR16 *ProfileName,
145 IN CONST BOOLEAN CanAffectLE,
146 IN CONST EFI_HANDLE HiiHandle,
147 IN CONST EFI_STRING_ID ManFormatHelp
148 );
149
150 /**
151 Checks if a command string has been registered for CommandString, and if so, it runs
152 the previously registered handler for that command with the command line.
153
154 If CommandString is NULL, then ASSERT().
155
156 If Sections is specified, then each section name listed will be compared in a case sensitive
157 manner to the section names described in Appendix B UEFI Shell 2.0 Specification. If the section exists,
158 it is appended to the returned help text. If the section does not exist, no
159 information is returned. If Sections is NULL, then all help text information
160 available is returned.
161
162 @param[in] CommandString The pointer to the command name. This is the name
163 found on the command line in the shell.
164 @param[in,out] RetVal The pointer to the return value from the command handler.
165
166 @param[in,out] CanAffectLE Indicates whether this command's return value
167 needs to be placed into LASTERROR environment variable.
168
169 @retval RETURN_SUCCESS The handler was run.
170 @retval RETURN_NOT_FOUND The CommandString did not match a registered
171 command name.
172 @sa SHELL_RUN_COMMAND
173 **/
174 RETURN_STATUS
175 EFIAPI
176 ShellCommandRunCommandHandler (
177 IN CONST CHAR16 *CommandString,
178 IN OUT SHELL_STATUS *RetVal,
179 IN OUT BOOLEAN *CanAffectLE OPTIONAL
180 );
181
182 /**
183 Checks if a command string has been registered for CommandString, and if so, it
184 returns the MAN filename specified for that command.
185
186 If CommandString is NULL, then ASSERT().
187
188 @param[in] CommandString The pointer to the command name. This is the name
189 found on the command line in the shell.
190
191 @retval NULL The CommandString was not a registered command.
192 @retval other The name of the MAN file.
193 @sa SHELL_GET_MAN_FILENAME
194 **/
195 CONST CHAR16*
196 EFIAPI
197 ShellCommandGetManFileNameHandler (
198 IN CONST CHAR16 *CommandString
199 );
200
201
202 typedef struct {
203 LIST_ENTRY Link;
204 CHAR16 *CommandString;
205 } COMMAND_LIST;
206
207 /**
208 Get the list of all available shell internal commands. This is a linked list,
209 via the LIST_ENTRY structure. Enumerate through it using the BaseLib linked
210 list functions. Do not modify the values.
211
212 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
213
214 @return A linked list of all available shell commands.
215 **/
216 CONST COMMAND_LIST*
217 EFIAPI
218 ShellCommandGetCommandList (
219 IN CONST BOOLEAN Sort
220 );
221
222 typedef struct {
223 LIST_ENTRY Link;
224 CHAR16 *CommandString;
225 CHAR16 *Alias;
226 } ALIAS_LIST;
227
228 /**
229 Registers aliases to be set as part of the initialization of the shell application.
230
231 If Command is NULL, then ASSERT().
232 If Alias is NULL, then ASSERT().
233
234 @param[in] Command The pointer to the Command.
235 @param[in] Alias The pointer to Alias.
236
237 @retval RETURN_SUCCESS The handlers were registered.
238 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
239 register the shell command.
240 **/
241 RETURN_STATUS
242 EFIAPI
243 ShellCommandRegisterAlias (
244 IN CONST CHAR16 *Command,
245 IN CONST CHAR16 *Alias
246 );
247
248 /**
249 Get the list of all shell alias commands. This is a linked list,
250 via LIST_ENTRY structure. Enumerate through it using the BaseLib linked
251 list functions. Do not modify the values.
252
253 @return A linked list of all requested shell aliases.
254 **/
255 CONST ALIAS_LIST*
256 EFIAPI
257 ShellCommandGetInitAliasList (
258 VOID
259 );
260
261 /**
262 Determine if a given alias is on the list of built in aliases.
263
264 @param[in] Alias The alias to test for.
265
266 @retval TRUE The alias is a built in alias.
267 @retval FALSE The alias is not a built in alias.
268 **/
269 BOOLEAN
270 EFIAPI
271 ShellCommandIsOnAliasList (
272 IN CONST CHAR16 *Alias
273 );
274
275 /**
276 Checks if a command is already on the list.
277
278 @param[in] CommandString The command string to check for on the list.
279
280 @retval TRUE CommandString represents a registered command.
281 @retval FALSE CommandString does not represent a registered command.
282 **/
283 BOOLEAN
284 EFIAPI
285 ShellCommandIsCommandOnList (
286 IN CONST CHAR16 *CommandString
287 );
288
289 /**
290 Get the help text for a command.
291
292 @param[in] CommandString The command name.
293
294 @retval NULL No help text was found.
295 @return The string of the help text. The caller required to free.
296 **/
297 CHAR16*
298 EFIAPI
299 ShellCommandGetCommandHelp (
300 IN CONST CHAR16 *CommandString
301 );
302
303 /**
304 Function to make sure that the above pointers are valid.
305 **/
306 EFI_STATUS
307 EFIAPI
308 CommandInit (
309 VOID
310 );
311
312 /**
313 Function to determine current state of ECHO. Echo determines if lines from scripts
314 and ECHO commands are enabled.
315
316 @retval TRUE Echo is currently enabled.
317 @retval FALSE Echo is currently disabled.
318 **/
319 BOOLEAN
320 EFIAPI
321 ShellCommandGetEchoState (
322 VOID
323 );
324
325 /**
326 Function to set current state of ECHO. Echo determines if lines from scripts
327 and ECHO commands are enabled.
328
329 @param[in] State TRUE to enable Echo, FALSE otherwise.
330 **/
331 VOID
332 EFIAPI
333 ShellCommandSetEchoState (
334 IN BOOLEAN State
335 );
336
337
338
339 /**
340 Indicate that the current shell or script should exit.
341
342 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
343 **/
344 VOID
345 EFIAPI
346 ShellCommandRegisterExit (
347 IN BOOLEAN ScriptOnly
348 );
349
350 /**
351 Retrieve the Exit indicator.
352
353 @retval TRUE Exit was indicated.
354 @retval FALSE Exit was not indicated.
355 **/
356 BOOLEAN
357 EFIAPI
358 ShellCommandGetExit (
359 VOID
360 );
361
362 /**
363 Retrieve the Exit script indicator.
364
365 If ShellCommandGetExit returns FALSE, then the return from this is undefined.
366
367 @retval TRUE ScriptOnly was indicated.
368 @retval FALSE ScriptOnly was not indicated.
369 **/
370 BOOLEAN
371 EFIAPI
372 ShellCommandGetScriptExit (
373 VOID
374 );
375
376 typedef struct {
377 LIST_ENTRY Link; ///< List enumerator items.
378 UINTN Line; ///< What line of the script file this was on.
379 CHAR16 *Cl; ///< The original command line.
380 VOID *Data; ///< The data structure format dependant upon Command. (not always used)
381 BOOLEAN Reset; ///< Reset the command (it must be treated like a initial run (but it may have data already))
382 } SCRIPT_COMMAND_LIST;
383
384 typedef struct {
385 CHAR16 *ScriptName; ///< The filename of this script.
386 CHAR16 **Argv; ///< The parmameters to the script file.
387 UINTN Argc; ///< The count of parameters.
388 LIST_ENTRY CommandList; ///< The script converted to a list of commands (SCRIPT_COMMAND_LIST objects).
389 SCRIPT_COMMAND_LIST *CurrentCommand; ///< The command currently being operated. If !=NULL must be a member of CommandList.
390 LIST_ENTRY SubstList; ///< A list of current script loop alias' (ALIAS_LIST objects) (Used for the for %-based replacement).
391 } SCRIPT_FILE;
392
393 /**
394 Function to return a pointer to the currently running script file object.
395
396 @retval NULL A script file is not currently running.
397 @return A pointer to the current script file object.
398 **/
399 SCRIPT_FILE*
400 EFIAPI
401 ShellCommandGetCurrentScriptFile (
402 VOID
403 );
404
405 /**
406 Function to set a new script as the currently running one.
407
408 This function will correctly stack and unstack nested scripts.
409
410 @param[in] Script The pointer to new script information structure. If NULL,
411 it removes and de-allocates the topmost Script structure.
412
413 @return A pointer to the current running script file after this
414 change. It is NULL if removing the final script.
415 **/
416 SCRIPT_FILE*
417 EFIAPI
418 ShellCommandSetNewScript (
419 IN SCRIPT_FILE *Script OPTIONAL
420 );
421
422 /**
423 Function to cleanup all memory from a SCRIPT_FILE structure.
424
425 @param[in] Script The pointer to the structure to cleanup.
426 **/
427 VOID
428 EFIAPI
429 DeleteScriptFileStruct (
430 IN SCRIPT_FILE *Script
431 );
432
433 /**
434 Function to get the current Profile string.
435
436 This is used to retrieve what profiles were installed.
437
438 @retval NULL There are no installed profiles.
439 @return A semicolon-delimited list of profiles.
440 **/
441 CONST CHAR16 *
442 EFIAPI
443 ShellCommandGetProfileList (
444 VOID
445 );
446
447 typedef enum {
448 MappingTypeFileSystem,
449 MappingTypeBlockIo,
450 MappingTypeMax
451 } SHELL_MAPPING_TYPE;
452
453 /**
454 Function to generate the next default mapping name.
455
456 If the return value is not NULL then it must be callee freed.
457
458 @param Type What kind of mapping name to make.
459
460 @retval NULL a memory allocation failed.
461 @return a new map name string
462 **/
463 CHAR16*
464 EFIAPI
465 ShellCommandCreateNewMappingName(
466 IN CONST SHELL_MAPPING_TYPE Type
467 );
468
469 /**
470 Function to initialize the table for creating consistent map names.
471
472 @param[out] Table The pointer to pointer to pointer to DevicePathProtocol object.
473
474 @retval EFI_SUCCESS The table was created successfully.
475 **/
476 EFI_STATUS
477 EFIAPI
478 ShellCommandConsistMappingInitialize (
479 EFI_DEVICE_PATH_PROTOCOL ***Table
480 );
481
482 /**
483 Function to uninitialize the table for creating consistent map names.
484
485 The parameter must have been received from ShellCommandConsistMappingInitialize.
486
487 @param[out] Table The pointer to pointer to DevicePathProtocol object.
488
489 @retval EFI_SUCCESS The table was deleted successfully.
490 **/
491 EFI_STATUS
492 EFIAPI
493 ShellCommandConsistMappingUnInitialize (
494 EFI_DEVICE_PATH_PROTOCOL **Table
495 );
496
497 /**
498 Create a consistent mapped name for the device specified by DevicePath
499 based on the Table.
500
501 This must be called after ShellCommandConsistMappingInitialize() and
502 before ShellCommandConsistMappingUnInitialize() is called.
503
504 @param[in] DevicePath The pointer to the dev path for the device.
505 @param[in] Table The Table of mapping information.
506
507 @retval NULL A consistent mapped name could not be created.
508 @return A pointer to a string allocated from pool with the device name.
509 **/
510 CHAR16*
511 EFIAPI
512 ShellCommandConsistMappingGenMappingName (
513 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
514 IN EFI_DEVICE_PATH_PROTOCOL **Table
515 );
516
517 /**
518 Function to search the list of mappings for the first matching node on the
519 list based on the MapKey.
520
521 @param[in] MapKey The pointer to the string key to search for in the map.
522
523 @return the node on the list.
524 **/
525 SHELL_MAP_LIST*
526 EFIAPI
527 ShellCommandFindMapItem (
528 IN CONST CHAR16 *MapKey
529 );
530
531 /**
532 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
533
534 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
535 default paths on the new map name...
536
537 Path should be FALSE when this function is called from the protocol SetMap function.
538
539 @param[in] Name The human readable mapped name.
540 @param[in] DevicePath The Device Path for this map.
541 @param[in] Flags The Flags attribute for this map item.
542 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
543
544 @retval EFI_SUCCESS The addition was sucessful.
545 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
546 @retval EFI_INVALID_PARAMETER A parameter was invalid.
547 **/
548 EFI_STATUS
549 EFIAPI
550 ShellCommandAddMapItemAndUpdatePath(
551 IN CONST CHAR16 *Name,
552 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
553 IN CONST UINT64 Flags,
554 IN CONST BOOLEAN Path
555 );
556
557 /**
558 Creates the default map names for each device path in the system with
559 a protocol depending on the Type.
560
561 Also sets up the default path environment variable if Type is FileSystem.
562
563 @retval EFI_SUCCESS All map names were created sucessfully.
564 @retval EFI_NOT_FOUND No protocols were found in the system.
565 @return Error returned from gBS->LocateHandle().
566
567 @sa LocateHandle
568 **/
569 EFI_STATUS
570 EFIAPI
571 ShellCommandCreateInitialMappingsAndPaths(
572 VOID
573 );
574
575 /**
576 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
577
578 @param[in] Handle The SHELL_FILE_HANDLE to convert.
579
580 @return a EFI_FILE_PROTOCOL* representing the same file.
581 **/
582 EFI_FILE_PROTOCOL*
583 EFIAPI
584 ConvertShellHandleToEfiFileProtocol(
585 IN CONST SHELL_FILE_HANDLE Handle
586 );
587
588 /**
589 Remove a SHELL_FILE_HANDLE frmo the list of SHELL_FILE_HANDLES.
590
591 @param[in] Handle The SHELL_FILE_HANDLE to remove.
592
593 @retval TRUE The item was removed.
594 @retval FALSE The item was not found.
595 **/
596 BOOLEAN
597 EFIAPI
598 ShellFileHandleRemove(
599 IN CONST SHELL_FILE_HANDLE Handle
600 );
601
602 /**
603 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
604
605 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
606 @param[in] Path The path to the file for verification.
607
608 @return a SHELL_FILE_HANDLE representing the same file.
609 **/
610 SHELL_FILE_HANDLE
611 EFIAPI
612 ConvertEfiFileProtocolToShellHandle(
613 IN CONST EFI_FILE_PROTOCOL *Handle,
614 IN CONST CHAR16 *Path
615 );
616
617 /**
618 Find the path that was logged with the specified SHELL_FILE_HANDLE.
619
620 @param[in] Handle The SHELL_FILE_HANDLE to query on.
621
622 @return A pointer to the path for the file.
623 **/
624 CONST CHAR16*
625 EFIAPI
626 ShellFileHandleGetPath(
627 IN CONST SHELL_FILE_HANDLE Handle
628 );
629
630
631 /**
632 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
633
634 This will NOT work on directories.
635
636 If Handle is NULL, then ASSERT.
637
638 @param[in] Handle the file handle
639
640 @retval TRUE the position is at the end of the file
641 @retval FALSE the position is not at the end of the file
642 **/
643 BOOLEAN
644 EFIAPI
645 ShellFileHandleEof(
646 IN SHELL_FILE_HANDLE Handle
647 );
648
649 typedef struct {
650 LIST_ENTRY Link;
651 void *Buffer;
652 } BUFFER_LIST;
653
654 /**
655 Frees any BUFFER_LIST defined type.
656
657 @param[in] List The pointer to the list head.
658 **/
659 VOID
660 EFIAPI
661 FreeBufferList (
662 IN BUFFER_LIST *List
663 );
664
665 #endif //_SHELL_COMMAND_LIB_