2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2009, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #if !defined(__SHELL_LIB__)
18 #include <Protocol/SimpleFileSystem.h>
19 #include <Protocol/EfiShell.h>
22 This function will retrieve the information about the file for the handle
23 specified and store it in allocated pool memory.
25 This function allocates a buffer to store the file's information. It is the
26 caller's responsibility to free the buffer
28 @param FileHandle The file handle of the file for which information is
31 @retval NULL information could not be retrieved.
33 @return the information about the file
38 IN EFI_FILE_HANDLE FileHandle
42 This function will set the information about the file for the opened handle
45 @param FileHandle The file handle of the file for which information
48 @param FileInfo The infotmation to set.
50 @retval EFI_SUCCESS The information was set.
51 @retval EFI_UNSUPPORTED The InformationType is not known.
52 @retval EFI_NO_MEDIA The device has no medium.
53 @retval EFI_DEVICE_ERROR The device reported an error.
54 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
55 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
56 @retval EFI_ACCESS_DENIED The file was opened read only.
57 @retval EFI_VOLUME_FULL The volume is full.
62 IN EFI_FILE_HANDLE FileHandle
,
63 IN EFI_FILE_INFO
*FileInfo
67 This function will open a file or directory referenced by DevicePath.
69 This function opens a file with the open mode according to the file path. The
70 Attributes is valid only for EFI_FILE_MODE_CREATE.
72 @param FilePath on input the device path to the file. On output
73 the remaining device path.
74 @param DeviceHandle pointer to the system device handle.
75 @param FileHandle pointer to the file handle.
76 @param OpenMode the mode to open the file with.
77 @param Attributes the file's file attributes.
79 @retval EFI_SUCCESS The information was set.
80 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
81 @retval EFI_UNSUPPORTED Could not open the file path.
82 @retval EFI_NOT_FOUND The specified file could not be found on the
83 device or the file system could not be found on
85 @retval EFI_NO_MEDIA The device has no medium.
86 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
87 medium is no longer supported.
88 @retval EFI_DEVICE_ERROR The device reported an error.
89 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
90 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
91 @retval EFI_ACCESS_DENIED The file was opened read only.
92 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
94 @retval EFI_VOLUME_FULL The volume is full.
98 ShellOpenFileByDevicePath(
99 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
100 OUT EFI_HANDLE
*DeviceHandle
,
101 OUT EFI_FILE_HANDLE
*FileHandle
,
107 This function will open a file or directory referenced by filename.
109 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
110 otherwise, the Filehandle is NULL. The Attributes is valid only for
111 EFI_FILE_MODE_CREATE.
113 @param FileName pointer to file name
114 @param FileHandle pointer to the file handle.
115 @param OpenMode the mode to open the file with.
116 @param Attributes the file's file attributes.
118 @retval EFI_SUCCESS The information was set.
119 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
120 @retval EFI_UNSUPPORTED Could not open the file path.
121 @retval EFI_NOT_FOUND The specified file could not be found on the
122 device or the file system could not be found
124 @retval EFI_NO_MEDIA The device has no medium.
125 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
126 medium is no longer supported.
127 @retval EFI_DEVICE_ERROR The device reported an error.
128 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
129 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
130 @retval EFI_ACCESS_DENIED The file was opened read only.
131 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
133 @retval EFI_VOLUME_FULL The volume is full.
139 OUT EFI_FILE_HANDLE
*FileHandle
,
145 This function create a directory
147 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
148 otherwise, the Filehandle is NULL. If the directory already existed, this
149 function opens the existing directory.
151 @param DirectoryName pointer to Directory name
152 @param FileHandle pointer to the file handle.
154 @retval EFI_SUCCESS The information was set.
155 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
156 @retval EFI_UNSUPPORTED Could not open the file path.
157 @retval EFI_NOT_FOUND The specified file could not be found on the
158 device or the file system could not be found
160 @retval EFI_NO_MEDIA The device has no medium.
161 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
162 medium is no longer supported.
163 @retval EFI_DEVICE_ERROR The device reported an error.
164 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
165 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
166 @retval EFI_ACCESS_DENIED The file was opened read only.
167 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
169 @retval EFI_VOLUME_FULL The volume is full.
173 ShellCreateDirectory(
174 IN CHAR16
*DirectoryName
,
175 OUT EFI_FILE_HANDLE
*FileHandle
179 This function reads information from an opened file.
181 If FileHandle is not a directory, the function reads the requested number of
182 bytes from the file at the file's current position and returns them in Buffer.
183 If the read goes beyond the end of the file, the read length is truncated to the
184 end of the file. The file's current position is increased by the number of bytes
185 returned. If FileHandle is a directory, the function reads the directory entry
186 at the file's current position and returns the entry in Buffer. If the Buffer
187 is not large enough to hold the current directory entry, then
188 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
189 BufferSize is set to be the size of the buffer needed to read the entry. On
190 success, the current position is updated to the next directory entry. If there
191 are no more directory entries, the read returns a zero-length buffer.
192 EFI_FILE_INFO is the structure returned as the directory entry.
194 @param FileHandle the opened file handle
196 @param ReadSize on input the size of buffer in bytes. on return
197 the number of bytes written.
199 @param Buffer the buffer to put read data into.
201 @retval EFI_SUCCESS Data was read.
202 @retval EFI_NO_MEDIA The device has no media.
203 @retval EFI_DEVICE_ERROR The device reported an error.
204 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
205 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
212 IN EFI_FILE_HANDLE FileHandle
,
213 IN OUT UINTN
*ReadSize
,
218 Write data to a file.
220 This function writes the specified number of bytes to the file at the current
221 file position. The current file position is advanced the actual number of bytes
222 written, which is returned in BufferSize. Partial writes only occur when there
223 has been a data error during the write attempt (such as "volume space full").
224 The file is automatically grown to hold the data if required. Direct writes to
225 opened directories are not supported.
227 @param FileHandle The opened file for writing
229 @param BufferSize on input the number of bytes in Buffer. On output
230 the number of bytes written.
232 @param Buffer the buffer containing data to write is stored.
234 @retval EFI_SUCCESS Data was written.
235 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
236 @retval EFI_NO_MEDIA The device has no media.
237 @retval EFI_DEVICE_ERROR The device reported an error.
238 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
239 @retval EFI_WRITE_PROTECTED The device is write-protected.
240 @retval EFI_ACCESS_DENIED The file was open for read only.
241 @retval EFI_VOLUME_FULL The volume is full.
246 IN EFI_FILE_HANDLE FileHandle
,
247 IN OUT UINTN
*BufferSize
,
252 Close an open file handle.
254 This function closes a specified file handle. All "dirty" cached file data is
255 flushed to the device, and the file is closed. In all cases the handle is
258 @param FileHandle the file handle to close.
260 @retval EFI_SUCCESS the file handle was closed sucessfully.
261 @retval INVALID_PARAMETER One of the parameters has an invalid value.
266 IN EFI_FILE_HANDLE
*FileHandle
270 Delete a file and close the handle
272 This function closes and deletes a file. In all cases the file handle is closed.
273 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
274 returned, but the handle is still closed.
276 @param FileHandle the file handle to delete
278 @retval EFI_SUCCESS the file was closed sucessfully
279 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
281 @retval INVALID_PARAMETER One of the parameters has an invalid value.
286 IN EFI_FILE_HANDLE
*FileHandle
290 Set the current position in a file.
292 This function sets the current file position for the handle to the position
293 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
294 absolute positioning is supported, and seeking past the end of the file is
295 allowed (a subsequent write would grow the file). Seeking to position
296 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
297 If FileHandle is a directory, the only position that may be set is zero. This
298 has the effect of starting the read process of the directory entries over.
300 @param FileHandle The file handle on which the position is being set
302 @param Position Byte position from begining of file
304 @retval EFI_SUCCESS Operation completed sucessfully.
305 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
307 @retval INVALID_PARAMETER One of the parameters has an invalid value.
311 ShellSetFilePosition (
312 IN EFI_FILE_HANDLE FileHandle
,
317 Gets a file's current position
319 This function retrieves the current file position for the file handle. For
320 directories, the current file position has no meaning outside of the file
321 system driver and as such the operation is not supported. An error is returned
322 if FileHandle is a directory.
324 @param FileHandle The open file handle on which to get the position.
325 @param Position Byte position from begining of file.
327 @retval EFI_SUCCESS the operation completed sucessfully.
328 @retval INVALID_PARAMETER One of the parameters has an invalid value.
329 @retval EFI_UNSUPPORTED the request is not valid on directories.
333 ShellGetFilePosition (
334 IN EFI_FILE_HANDLE FileHandle
,
339 Flushes data on a file
341 This function flushes all modified data associated with a file to a device.
343 @param FileHandle The file handle on which to flush data
345 @retval EFI_SUCCESS The data was flushed.
346 @retval EFI_NO_MEDIA The device has no media.
347 @retval EFI_DEVICE_ERROR The device reported an error.
348 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
349 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
350 @retval EFI_ACCESS_DENIED The file was opened for read only.
355 IN EFI_FILE_HANDLE FileHandle
359 Retrieves the first file from a directory
361 This function takes an open directory handle and gets the first file
362 in the directory's info. Caller can use ShellFindNextFile() to get
365 Caller must use FreePool on *Buffer opon completion of all file searching.
367 @param DirHandle The file handle of the directory to search
368 @param Buffer Pointer to pointer to buffer for file's information
370 @retval EFI_SUCCESS Found the first file.
371 @retval EFI_NOT_FOUND Cannot find the directory.
372 @retval EFI_NO_MEDIA The device has no media.
373 @retval EFI_DEVICE_ERROR The device reported an error.
374 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
375 @return ShellReadFile
380 IN EFI_FILE_HANDLE DirHandle
,
381 OUT EFI_FILE_INFO
**Buffer
385 Retrieves the next file in a directory.
387 To use this function, caller must call the ShellFindFirstFile() to get the
388 first file, and then use this function get other files. This function can be
389 called for several times to get each file's information in the directory. If
390 the call of ShellFindNextFile() got the last file in the directory, the next
391 call of this function has no file to get. *NoFile will be set to TRUE and the
392 data in Buffer is meaningless.
394 @param DirHandle the file handle of the directory
395 @param Buffer pointer to buffer for file's information
396 @param NoFile pointer to boolean when last file is found
398 @retval EFI_SUCCESS Found the next file.
399 @retval EFI_NO_MEDIA The device has no media.
400 @retval EFI_DEVICE_ERROR The device reported an error.
401 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
406 IN EFI_FILE_HANDLE DirHandle
,
407 OUT EFI_FILE_INFO
*Buffer
,
412 Retrieve the size of a file.
414 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
417 @param FileHandle file handle from which size is retrieved
418 @param Size pointer to size
420 @retval EFI_SUCCESS operation was completed sucessfully
421 @retval EFI_DEVICE_ERROR cannot access the file
426 IN EFI_FILE_HANDLE FileHandle
,
431 Retrieves the status of the break execution flag
433 this function is useful to check whether the application is being asked to halt by the shell.
435 @retval TRUE the execution break is enabled
436 @retval FALSE the execution break is not enabled
440 ShellGetExecutionBreakFlag(
445 return the value of an environment variable
447 this function gets the value of the environment variable set by the
448 ShellSetEnvironmentVariable function
450 @param EnvKey The key name of the environment variable.
452 @retval NULL the named environment variable does not exist.
453 @return != NULL pointer to the value of the environment variable
457 ShellGetEnvironmentVariable (
462 set the value of an environment variable
464 This function changes the current value of the specified environment variable. If the
465 environment variable exists and the Value is an empty string, then the environment
466 variable is deleted. If the environment variable exists and the Value is not an empty
467 string, then the value of the environment variable is changed. If the environment
468 variable does not exist and the Value is an empty string, there is no action. If the
469 environment variable does not exist and the Value is a non-empty string, then the
470 environment variable is created and assigned the specified value.
472 This is not supported pre-UEFI Shell 2.0.
474 @param EnvKey The key name of the environment variable.
475 @param EnvVal The Value of the environment variable
476 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
478 @retval EFI_SUCCESS the operation was completed sucessfully
479 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
483 ShellSetEnvironmentVariable (
484 IN CONST CHAR16
*EnvKey
,
485 IN CONST CHAR16
*EnvVal
,
490 cause the shell to parse and execute a command line.
492 This function creates a nested instance of the shell and executes the specified
493 command (CommandLine) with the specified environment (Environment). Upon return,
494 the status code returned by the specified command is placed in StatusCode.
495 If Environment is NULL, then the current environment is used and all changes made
496 by the commands executed will be reflected in the current environment. If the
497 Environment is non-NULL, then the changes made will be discarded.
498 The CommandLine is executed from the current working directory on the current
501 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
502 Output is only supported for pre-UEFI Shell 2.0
504 @param ImageHandle Parent image that is starting the operation
505 @param CommandLine pointer to null terminated command line.
506 @param Output true to display debug output. false to hide it.
507 @param EnvironmentVariables optional pointer to array of environment variables
508 in the form "x=y". if NULL current set is used.
509 @param Status the status of the run command line.
511 @retval EFI_SUCCESS the operation completed sucessfully. Status
512 contains the status code returned.
513 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
514 @retval EFI_OUT_OF_RESOURCES out of resources
515 @retval EFI_UNSUPPORTED the operation is not allowed.
520 IN EFI_HANDLE
*ParentHandle
,
521 IN CHAR16
*CommandLine
,
523 IN CHAR16
**EnvironmentVariables
,
524 OUT EFI_STATUS
*Status
528 Retreives the current directory path
530 If the DeviceName is NULL, it returns the current device's current directory
531 name. If the DeviceName is not NULL, it returns the current directory name
534 @param DeviceName the name of the drive to get directory on
536 @retval NULL the directory does not exist
537 @return != NULL the directory
542 IN CHAR16
*DeviceName OPTIONAL
546 sets (enabled or disabled) the page break mode
548 when page break mode is enabled the screen will stop scrolling
549 and wait for operator input before scrolling a subsequent screen.
551 @param CurrentState TRUE to enable and FALSE to disable
555 ShellSetPageBreakMode (
556 IN BOOLEAN CurrentState
560 Opens a group of files based on a path.
562 This function uses the Arg to open all the matching files. Each matched
563 file has a SHELL_FILE_ARG structure to record the file information. These
564 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
565 structures from ListHead to access each file. This function supports wildcards
566 and will process '?' and '*' as such. the list must be freed with a call to
567 ShellCloseFileMetaArg().
569 This function will fail if called sequentially without freeing the list in the middle.
571 @param Arg pointer to path string
572 @param OpenMode mode to open files with
573 @param ListHead head of linked list of results
575 @retval EFI_SUCCESS the operation was sucessful and the list head
576 contains the list of opened files
577 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
578 *ListHead is set to NULL.
579 @return != EFI_SUCCESS the operation failed
581 @sa InternalShellConvertFileListType
585 ShellOpenFileMetaArg (
588 IN OUT EFI_SHELL_FILE_INFO
**ListHead
592 Free the linked list returned from ShellOpenFileMetaArg
594 @param ListHead the pointer to free
596 @retval EFI_SUCCESS the operation was sucessful
597 @retval EFI_INVALID_PARAMETER A parameter was invalid
601 ShellCloseFileMetaArg (
602 IN OUT EFI_SHELL_FILE_INFO
**ListHead
618 /// Helper structure for no parameters (besides -? and -b)
619 extern SHELL_PARAM_ITEM EmptyParamList
[];
622 Checks the command line arguments passed against the list of valid ones.
623 Optionally removes NULL values first.
625 If no initialization is required, then return RETURN_SUCCESS.
627 @param CheckList pointer to list of parameters to check
628 @param CheckPackage Package of checked values
629 @param ProblemParam optional pointer to pointer to unicode string for
630 the paramater that caused failure.
631 @param AutoPageBreak will automatically set PageBreakEnabled
633 @retval EFI_SUCCESS The operation completed sucessfully.
634 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
635 @retval EFI_INVALID_PARAMETER A parameter was invalid
636 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
637 duplicated. the duplicated command line argument
638 was returned in ProblemParam if provided.
639 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
640 of the command line arguments was returned in
641 ProblemParam if provided.
642 @retval EFI_NOT_FOUND a argument required a value that was missing.
643 the invalid command line argument was returned in
644 ProblemParam if provided.
648 ShellCommandLineParse (
649 IN CONST SHELL_PARAM_ITEM
*CheckList
,
650 OUT LIST_ENTRY
**CheckPackage
,
651 OUT CHAR16
**ProblemParam OPTIONAL
,
652 IN BOOLEAN AutoPageBreak
656 Frees shell variable list that was returned from ShellCommandLineParse.
658 This function will free all the memory that was used for the CheckPackage
659 list of postprocessed shell arguments.
661 this function has no return value.
663 if CheckPackage is NULL, then return
665 @param CheckPackage the list to de-allocate
669 ShellCommandLineFreeVarList (
670 IN LIST_ENTRY
*CheckPackage
674 Checks for presence of a flag parameter
676 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
678 if CheckPackage is NULL then return FALSE.
679 if KeyString is NULL then ASSERT()
681 @param CheckPackage The package of parsed command line arguments
682 @param KeyString the Key of the command line argument to check for
684 @retval TRUE the flag is on the command line
685 @retval FALSE the flag is not on the command line
689 ShellCommandLineGetFlag (
690 IN CONST LIST_ENTRY
*CheckPackage
,
695 returns value from command line argument
697 value parameters are in the form of "-<Key> value" or "/<Key> value"
699 if CheckPackage is NULL, then return NULL;
701 @param CheckPackage The package of parsed command line arguments
702 @param KeyString the Key of the command line argument to check for
704 @retval NULL the flag is not on the command line
705 @return !=NULL pointer to unicode string of the value
709 ShellCommandLineGetValue (
710 IN CONST LIST_ENTRY
*CheckPackage
,
715 returns raw value from command line argument
717 raw value parameters are in the form of "value" in a specific position in the list
719 if CheckPackage is NULL, then return NULL;
721 @param CheckPackage The package of parsed command line arguments
722 @param Position the position of the value
724 @retval NULL the flag is not on the command line
725 @return !=NULL pointer to unicode string of the value
729 ShellCommandLineGetRawValue (
730 IN CONST LIST_ENTRY
*CheckPackage
,
735 This function causes the shell library to initialize itself. If the shell library
736 is already initialized it will de-initialize all the current protocol poitners and
737 re-populate them again.
739 When the library is used with PcdShellLibAutoInitialize set to true this function
740 will return EFI_SUCCESS and perform no actions.
742 This function is intended for internal access for shell commands only.
744 @retval EFI_SUCCESS the initialization was complete sucessfully
752 #endif // __SHELL_LIB__