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 <Guid/FileInfo.h>
20 #include <Protocol/EfiShell.h>
23 This function will retrieve the information about the file for the handle
24 specified and store it in allocated pool memory.
26 This function allocates a buffer to store the file\92s information. It is the
27 caller\92s responsibility to free the buffer
29 @param FileHandle The file handle of the file for which information is
32 @retval NULL information could not be retrieved.
34 @return the information about the file
39 IN EFI_FILE_HANDLE FileHandle
43 This function will set the information about the file for the opened handle
46 @param FileHandle The file handle of the file for which information
49 @param FileInfo The infotmation to set.
51 @retval EFI_SUCCESS The information was set.
52 @retval EFI_UNSUPPORTED The InformationType is not known.
53 @retval EFI_NO_MEDIA The device has no medium.
54 @retval EFI_DEVICE_ERROR The device reported an error.
55 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
56 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
57 @retval EFI_ACCESS_DENIED The file was opened read only.
58 @retval EFI_VOLUME_FULL The volume is full.
63 IN EFI_FILE_HANDLE FileHandle
,
64 IN EFI_FILE_INFO
*FileInfo
68 This function will open a file or directory referenced by DevicePath.
70 This function opens a file with the open mode according to the file path. The
71 Attributes is valid only for EFI_FILE_MODE_CREATE.
73 @param FilePath on input the device path to the file. On output
74 the remaining device path.
75 @param DeviceHandle pointer to the system device handle.
76 @param FileHandle pointer to the file handle.
77 @param OpenMode the mode to open the file with.
78 @param Attributes the file's file attributes.
80 @retval EFI_SUCCESS The information was set.
81 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
82 @retval EFI_UNSUPPORTED Could not open the file path.
83 @retval EFI_NOT_FOUND The specified file could not be found on the
84 device or the file system could not be found on
86 @retval EFI_NO_MEDIA The device has no medium.
87 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
88 medium is no longer supported.
89 @retval EFI_DEVICE_ERROR The device reported an error.
90 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
91 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
92 @retval EFI_ACCESS_DENIED The file was opened read only.
93 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
95 @retval EFI_VOLUME_FULL The volume is full.
99 ShellOpenFileByDevicePath(
100 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
101 OUT EFI_HANDLE
*DeviceHandle
,
102 OUT EFI_FILE_HANDLE
*FileHandle
,
108 This function will open a file or directory referenced by filename.
110 If return is EFI_SUCCESS, the Filehandle is the opened file\92s handle;
111 otherwise, the Filehandle is NULL. The Attributes is valid only for
112 EFI_FILE_MODE_CREATE.
114 @param FileName pointer to file name
115 @param FileHandle pointer to the file handle.
116 @param OpenMode the mode to open the file with.
117 @param Attributes the file's file attributes.
119 @retval EFI_SUCCESS The information was set.
120 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
121 @retval EFI_UNSUPPORTED Could not open the file path.
122 @retval EFI_NOT_FOUND The specified file could not be found on the
123 device or the file system could not be found
125 @retval EFI_NO_MEDIA The device has no medium.
126 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
127 medium is no longer supported.
128 @retval EFI_DEVICE_ERROR The device reported an error.
129 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
130 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
131 @retval EFI_ACCESS_DENIED The file was opened read only.
132 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
134 @retval EFI_VOLUME_FULL The volume is full.
140 OUT EFI_FILE_HANDLE
*FileHandle
,
146 This function create a directory
148 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
149 otherwise, the Filehandle is NULL. If the directory already existed, this
150 function opens the existing directory.
152 @param DirectoryName pointer to Directory name
153 @param FileHandle pointer to the file handle.
155 @retval EFI_SUCCESS The information was set.
156 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
157 @retval EFI_UNSUPPORTED Could not open the file path.
158 @retval EFI_NOT_FOUND The specified file could not be found on the
159 device or the file system could not be found
161 @retval EFI_NO_MEDIA The device has no medium.
162 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
163 medium is no longer supported.
164 @retval EFI_DEVICE_ERROR The device reported an error.
165 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
166 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
167 @retval EFI_ACCESS_DENIED The file was opened read only.
168 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
170 @retval EFI_VOLUME_FULL The volume is full.
174 ShellCreateDirectory(
175 IN CHAR16
*DirectoryName
,
176 OUT EFI_FILE_HANDLE
*FileHandle
180 This function reads information from an opened file.
182 If FileHandle is not a directory, the function reads the requested number of
183 bytes from the file at the file\92s current position and returns them in Buffer.
184 If the read goes beyond the end of the file, the read length is truncated to the
185 end of the file. The file\92s current position is increased by the number of bytes
186 returned. If FileHandle is a directory, the function reads the directory entry
187 at the file\92s current position and returns the entry in Buffer. If the Buffer
188 is not large enough to hold the current directory entry, then
189 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
190 BufferSize is set to be the size of the buffer needed to read the entry. On
191 success, the current position is updated to the next directory entry. If there
192 are no more directory entries, the read returns a zero-length buffer.
193 EFI_FILE_INFO is the structure returned as the directory entry.
195 @param FileHandle the opened file handle
197 @param ReadSize on input the size of buffer in bytes. on return
198 the number of bytes written.
200 @param Buffer the buffer to put read data into.
202 @retval EFI_SUCCESS Data was read.
203 @retval EFI_NO_MEDIA The device has no media.
204 @retval EFI_DEVICE_ERROR The device reported an error.
205 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
206 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
213 IN EFI_FILE_HANDLE FileHandle
,
214 IN OUT UINTN
*ReadSize
,
219 Write data to a file.
221 This function writes the specified number of bytes to the file at the current
222 file position. The current file position is advanced the actual number of bytes
223 written, which is returned in BufferSize. Partial writes only occur when there
224 has been a data error during the write attempt (such as \93volume space full\94).
225 The file is automatically grown to hold the data if required. Direct writes to
226 opened directories are not supported.
228 @param FileHandle The opened file for writing
230 @param BufferSize on input the number of bytes in Buffer. On output
231 the number of bytes written.
233 @param Buffer the buffer containing data to write is stored.
235 @retval EFI_SUCCESS Data was written.
236 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
237 @retval EFI_NO_MEDIA The device has no media.
238 @retval EFI_DEVICE_ERROR The device reported an error.
239 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
240 @retval EFI_WRITE_PROTECTED The device is write-protected.
241 @retval EFI_ACCESS_DENIED The file was open for read only.
242 @retval EFI_VOLUME_FULL The volume is full.
247 IN EFI_FILE_HANDLE FileHandle
,
248 IN OUT UINTN
*BufferSize
,
249 IN CONST VOID
*Buffer
253 Close an open file handle.
255 This function closes a specified file handle. All \93dirty\94 cached file data is
256 flushed to the device, and the file is closed. In all cases the handle is
259 @param FileHandle the file handle to close.
261 @retval EFI_SUCCESS the file handle was closed sucessfully.
262 @retval INVALID_PARAMETER One of the parameters has an invalid value.
267 IN EFI_FILE_HANDLE
*FileHandle
271 Delete a file and close the handle
273 This function closes and deletes a file. In all cases the file handle is closed.
274 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
275 returned, but the handle is still closed.
277 @param FileHandle the file handle to delete
279 @retval EFI_SUCCESS the file was closed sucessfully
280 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
282 @retval INVALID_PARAMETER One of the parameters has an invalid value.
287 IN EFI_FILE_HANDLE
*FileHandle
291 Set the current position in a file.
293 This function sets the current file position for the handle to the position
294 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
295 absolute positioning is supported, and seeking past the end of the file is
296 allowed (a subsequent write would grow the file). Seeking to position
297 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
298 If FileHandle is a directory, the only position that may be set is zero. This
299 has the effect of starting the read process of the directory entries over.
301 @param FileHandle The file handle on which the position is being set
303 @param Position Byte position from begining of file
305 @retval EFI_SUCCESS Operation completed sucessfully.
306 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
308 @retval INVALID_PARAMETER One of the parameters has an invalid value.
312 ShellSetFilePosition (
313 IN EFI_FILE_HANDLE FileHandle
,
318 Gets a file's current position
320 This function retrieves the current file position for the file handle. For
321 directories, the current file position has no meaning outside of the file
322 system driver and as such the operation is not supported. An error is returned
323 if FileHandle is a directory.
325 @param FileHandle The open file handle on which to get the position.
326 @param Position Byte position from begining of file.
328 @retval EFI_SUCCESS the operation completed sucessfully.
329 @retval INVALID_PARAMETER One of the parameters has an invalid value.
330 @retval EFI_UNSUPPORTED the request is not valid on directories.
334 ShellGetFilePosition (
335 IN EFI_FILE_HANDLE FileHandle
,
340 Flushes data on a file
342 This function flushes all modified data associated with a file to a device.
344 @param FileHandle The file handle on which to flush data
346 @retval EFI_SUCCESS The data was flushed.
347 @retval EFI_NO_MEDIA The device has no media.
348 @retval EFI_DEVICE_ERROR The device reported an error.
349 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
350 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
351 @retval EFI_ACCESS_DENIED The file was opened for read only.
356 IN EFI_FILE_HANDLE FileHandle
360 Retrieves the first file from a directory
362 This function takes an open directory handle and gets the first file
363 in the directory's info. Caller can use ShellFindNextFile() to get
366 @param DirHandle The file handle of the directory to search
367 @param Buffer Pointer to buffer for file's information
369 @retval EFI_SUCCESS Found the first file.
370 @retval EFI_NOT_FOUND Cannot find the directory.
371 @retval EFI_NO_MEDIA The device has no media.
372 @retval EFI_DEVICE_ERROR The device reported an error.
373 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
374 @return ShellReadFile
379 IN EFI_FILE_HANDLE DirHandle
,
380 OUT EFI_FILE_INFO
*Buffer
384 Retrieves the next file in a directory.
386 To use this function, caller must call the ShellFindFirstFile() to get the
387 first file, and then use this function get other files. This function can be
388 called for several times to get each file's information in the directory. If
389 the call of ShellFindNextFile() got the last file in the directory, the next
390 call of this function has no file to get. *NoFile will be set to TRUE and the
391 data in Buffer is meaningless.
393 @param DirHandle the file handle of the directory
394 @param Buffer pointer to buffer for file's information
395 @param NoFile pointer to boolean when last file is found
397 @retval EFI_SUCCESS Found the next file.
398 @retval EFI_NO_MEDIA The device has no media.
399 @retval EFI_DEVICE_ERROR The device reported an error.
400 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
405 IN EFI_FILE_HANDLE DirHandle
,
406 OUT EFI_FILE_INFO
*Buffer
,
411 Retrieve the size of a file.
413 This function extracts the file size info from the FileHandle\92s EFI_FILE_INFO
416 @param FileHandle file handle from which size is retrieved
417 @param Size pointer to size
419 @retval EFI_SUCCESS operation was completed sucessfully
420 @retval EFI_DEVICE_ERROR cannot access the file
425 IN EFI_FILE_HANDLE FileHandle
,
430 Retrieves the status of the break execution flag
432 this function is useful to check whether the application is being asked to halt by the shell.
434 @retval TRUE the execution break is enabled
435 @retval FALSE the execution break is not enabled
439 ShellGetExecutionBreakFlag(
444 return the value of an environment variable
446 this function gets the value of the environment variable set by the
447 ShellSetEnvironmentVariable function
449 @param EnvKey The key name of the environment variable.
451 @retval NULL the named environment variable does not exist.
452 @return != NULL pointer to the value of the environment variable
456 ShellGetEnvironmentVariable (
461 set the value of an environment variable
463 This function changes the current value of the specified environment variable. If the
464 environment variable exists and the Value is an empty string, then the environment
465 variable is deleted. If the environment variable exists and the Value is not an empty
466 string, then the value of the environment variable is changed. If the environment
467 variable does not exist and the Value is an empty string, there is no action. If the
468 environment variable does not exist and the Value is a non-empty string, then the
469 environment variable is created and assigned the specified value.
471 This is not supported pre-UEFI Shell 2.0.
473 @param EnvKey The key name of the environment variable.
474 @param EnvVal The Value of the environment variable
475 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
477 @retval EFI_SUCCESS the operation was completed sucessfully
478 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
482 ShellSetEnvironmentVariable (
483 IN CONST CHAR16
*EnvKey
,
484 IN CONST CHAR16
*EnvVal
,
489 cause the shell to parse and execute a command line.
491 This function creates a nested instance of the shell and executes the specified
492 command (CommandLine) with the specified environment (Environment). Upon return,
493 the status code returned by the specified command is placed in StatusCode.
494 If Environment is NULL, then the current environment is used and all changes made
495 by the commands executed will be reflected in the current environment. If the
496 Environment is non-NULL, then the changes made will be discarded.
497 The CommandLine is executed from the current working directory on the current
500 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
501 Output is only supported for pre-UEFI Shell 2.0
503 @param ImageHandle Parent image that is starting the operation
504 @param CommandLine pointer to null terminated command line.
505 @param Output true to display debug output. false to hide it.
506 @param EnvironmentVariables optional pointer to array of environment variables
507 in the form "x=y". if NULL current set is used.
508 @param Status the status of the run command line.
510 @retval EFI_SUCCESS the operation completed sucessfully. Status
511 contains the status code returned.
512 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
513 @retval EFI_OUT_OF_RESOURCES out of resources
514 @retval EFI_UNSUPPORTED the operation is not allowed.
519 IN EFI_HANDLE
*ParentHandle
,
520 IN CHAR16
*CommandLine
,
522 IN CHAR16
**EnvironmentVariables
,
523 OUT EFI_STATUS
*Status
527 Retreives the current directory path
529 If the DeviceName is NULL, it returns the current device\92s current directory
530 name. If the DeviceName is not NULL, it returns the current directory name
533 @param DeviceName the name of the drive to get directory on
535 @retval NULL the directory does not exist
536 @return != NULL the directory
541 IN CHAR16
*DeviceName OPTIONAL
545 sets (enabled or disabled) the page break mode
547 when page break mode is enabled the screen will stop scrolling
548 and wait for operator input before scrolling a subsequent screen.
550 @param CurrentState TRUE to enable and FALSE to disable
554 ShellSetPageBreakMode (
555 IN BOOLEAN CurrentState
559 Opens a group of files based on a path.
561 This function uses the Arg to open all the matching files. Each matched
562 file has a SHELL_FILE_ARG structure to record the file information. These
563 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
564 structures from ListHead to access each file. This function supports wildcards
565 and will process '?' and '*' as such. the list must be freed with a call to
566 ShellCloseFileMetaArg().
568 This function will fail if called sequentially without freeing the list in the middle.
570 @param Arg pointer to path string
571 @param OpenMode mode to open files with
572 @param ListHead head of linked list of results
574 @retval EFI_SUCCESS the operation was sucessful and the list head
575 contains the list of opened files
576 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
577 *ListHead is set to NULL.
578 @return != EFI_SUCCESS the operation failed
580 @sa InternalShellConvertFileListType
584 ShellOpenFileMetaArg (
587 IN OUT EFI_SHELL_FILE_INFO
**ListHead
591 Free the linked list returned from ShellOpenFileMetaArg
593 @param ListHead the pointer to free
595 @retval EFI_SUCCESS the operation was sucessful
596 @retval EFI_INVALID_PARAMETER A parameter was invalid
600 ShellCloseFileMetaArg (
601 IN OUT EFI_SHELL_FILE_INFO
**ListHead
617 Checks the command line arguments passed against the list of valid ones.
618 Optionally removes NULL values first.
620 If no initialization is required, then return RETURN_SUCCESS.
622 @param CheckList pointer to list of parameters to check
623 @param CheckPackage Package of checked values
624 @param ProblemParam optional pointer to pointer to unicode string for
625 the paramater that caused failure.
626 @param AutoPageBreak will automatically set PageBreakEnabled
628 @retval EFI_SUCCESS The operation completed sucessfully.
629 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
630 @retval EFI_INVALID_PARAMETER A parameter was invalid
631 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
632 duplicated. the duplicated command line argument
633 was returned in ProblemParam if provided.
634 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
635 of the command line arguments was returned in
636 ProblemParam if provided.
637 @retval EFI_NOT_FOUND a argument required a value that was missing.
638 the invalid command line argument was returned in
639 ProblemParam if provided.
643 ShellCommandLineParse (
644 IN CONST SHELL_PARAM_ITEM
*CheckList
,
645 OUT LIST_ENTRY
**CheckPackage
,
646 OUT CHAR16
**ProblemParam OPTIONAL
,
647 IN BOOLEAN AutoPageBreak
651 Frees shell variable list that was returned from ShellCommandLineParse.
653 This function will free all the memory that was used for the CheckPackage
654 list of postprocessed shell arguments.
656 this function has no return value.
658 if CheckPackage is NULL, then return
660 @param CheckPackage the list to de-allocate
664 ShellCommandLineFreeVarList (
665 IN LIST_ENTRY
*CheckPackage
669 Checks for presence of a flag parameter
671 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
673 if CheckPackage is NULL then return FALSE.
674 if KeyString is NULL then ASSERT()
676 @param CheckPackage The package of parsed command line arguments
677 @param KeyString the Key of the command line argument to check for
679 @retval TRUE the flag is on the command line
680 @retval FALSE the flag is not on the command line
684 ShellCommandLineGetFlag (
685 IN CONST LIST_ENTRY
*CheckPackage
,
690 returns value from command line argument
692 value parameters are in the form of "-<Key> value" or "/<Key> value"
694 if CheckPackage is NULL, then return NULL;
696 @param CheckPackage The package of parsed command line arguments
697 @param KeyString the Key of the command line argument to check for
699 @retval NULL the flag is not on the command line
700 @return !=NULL pointer to unicode string of the value
704 ShellCommandLineGetValue (
705 IN CONST LIST_ENTRY
*CheckPackage
,
710 returns raw value from command line argument
712 raw value parameters are in the form of "value" in a specific position in the list
714 if CheckPackage is NULL, then return NULL;
716 @param CheckPackage The package of parsed command line arguments
717 @param Position the position of the value
719 @retval NULL the flag is not on the command line
720 @return !=NULL pointer to unicode string of the value
724 ShellCommandLineGetRawValue (
725 IN CONST LIST_ENTRY
*CheckPackage
,
729 #endif // __SHELL_LIB__