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.
16 #include <Library/ShellLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/DevicePathLib.h>
23 #include <Protocol/EfiShellEnvironment2.h>
24 #include <Protocol/EfiShellInterface.h>
25 #include <Protocol/EfiShell.h>
26 #include <Protocol/EfiShellParameters.h>
27 #include <Protocol/SimpleFileSystem.h>
29 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
30 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
32 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
33 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
34 EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
35 EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
36 EFI_HANDLE mEfiShellEnvironment2Handle
;
37 EFI_LIST_ENTRY
*mOldStyleFileList
;
40 helper function to find ShellEnvironment2 for constructor
45 IN EFI_HANDLE ImageHandle
55 Status
= gBS
->OpenProtocol(ImageHandle
,
56 &gEfiShellEnvironment2Guid
,
57 (VOID
**)&mEfiShellEnvironment2
,
60 EFI_OPEN_PROTOCOL_GET_PROTOCOL
63 // look for the mEfiShellEnvironment2 protocol at a higher level
65 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
&&
66 (mEfiShellEnvironment2
->MajorVersion
> EFI_SHELL_MAJOR_VER
||
67 (mEfiShellEnvironment2
->MajorVersion
== EFI_SHELL_MAJOR_VER
&& mEfiShellEnvironment2
->MinorVersion
>= EFI_SHELL_MINOR_VER
)))) {
69 // figure out how big of a buffer we need.
71 Status
= gBS
->LocateHandle (ByProtocol
,
72 &gEfiShellEnvironment2Guid
,
73 NULL
, // ignored for ByProtocol
77 ASSERT(Status
== EFI_BUFFER_TOO_SMALL
);
78 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
79 ASSERT(Buffer
!= NULL
);
80 Status
= gBS
->LocateHandle (ByProtocol
,
81 &gEfiShellEnvironment2Guid
,
82 NULL
, // ignored for ByProtocol
86 if (!EFI_ERROR (Status
)) {
88 // now parse the list of returned handles
90 Status
= EFI_NOT_FOUND
;
91 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
92 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
93 &gEfiShellEnvironment2Guid
,
94 (VOID
**)&mEfiShellEnvironment2
,
97 EFI_OPEN_PROTOCOL_GET_PROTOCOL
99 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
&&
100 (mEfiShellEnvironment2
->MajorVersion
> EFI_SHELL_MAJOR_VER
||
101 (mEfiShellEnvironment2
->MajorVersion
== EFI_SHELL_MAJOR_VER
&& mEfiShellEnvironment2
->MinorVersion
>= EFI_SHELL_MINOR_VER
))) {
102 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
103 Status
= EFI_SUCCESS
;
109 if (Buffer
!= NULL
) {
116 Constructor for the Shell library.
118 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
120 @param ImageHandle the image handle of the process
121 @param SystemTable the EFI System Table pointer
123 @retval EFI_SUCCESS the initialization was complete sucessfully
124 @return others an error ocurred during initialization
128 ShellLibConstructor (
129 IN EFI_HANDLE ImageHandle
,
130 IN EFI_SYSTEM_TABLE
*SystemTable
135 ASSERT(SystemTable
!= NULL
);
138 mEfiShellEnvironment2
= NULL
;
139 mEfiShellProtocol
= NULL
;
140 mEfiShellParametersProtocol
= NULL
;
141 mEfiShellInterface
= NULL
;
142 mEfiShellEnvironment2Handle
= NULL
;
143 mOldStyleFileList
= NULL
;
146 // UEFI 2.0 shell interfaces (used preferentially)
148 Status
= gBS
->OpenProtocol(ImageHandle
,
149 &gEfiShellProtocolGuid
,
150 (VOID
**)&mEfiShellProtocol
,
153 EFI_OPEN_PROTOCOL_GET_PROTOCOL
155 if (EFI_ERROR(Status
)) {
156 mEfiShellProtocol
= NULL
;
158 Status
= gBS
->OpenProtocol(ImageHandle
,
159 &gEfiShellParametersProtocolGuid
,
160 (VOID
**)&mEfiShellParametersProtocol
,
163 EFI_OPEN_PROTOCOL_GET_PROTOCOL
165 if (EFI_ERROR(Status
)) {
166 mEfiShellParametersProtocol
= NULL
;
169 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
171 // Moved to seperate function due to complexity
173 Status
= ShellFindSE2(ImageHandle
);
175 if (EFI_ERROR(Status
)) {
176 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
177 mEfiShellEnvironment2
= NULL
;
179 Status
= gBS
->OpenProtocol(ImageHandle
,
180 &gEfiShellInterfaceGuid
,
181 (VOID
**)&mEfiShellInterface
,
184 EFI_OPEN_PROTOCOL_GET_PROTOCOL
186 if (EFI_ERROR(Status
)) {
187 mEfiShellInterface
= NULL
;
191 // only success getting 2 of either the old or new, but no 1/2 and 1/2
193 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
194 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
195 return (EFI_SUCCESS
);
197 return (EFI_NOT_FOUND
);
201 Destructory for the library. free any resources.
206 IN EFI_HANDLE ImageHandle
,
207 IN EFI_SYSTEM_TABLE
*SystemTable
210 if (mEfiShellEnvironment2
!= NULL
) {
211 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
212 &gEfiShellEnvironment2Guid
,
216 if (mEfiShellInterface
!= NULL
) {
217 gBS
->CloseProtocol(ImageHandle
,
218 &gEfiShellInterfaceGuid
,
222 if (mEfiShellProtocol
!= NULL
) {
223 gBS
->CloseProtocol(ImageHandle
,
224 &gEfiShellProtocolGuid
,
228 if (mEfiShellParametersProtocol
!= NULL
) {
229 gBS
->CloseProtocol(ImageHandle
,
230 &gEfiShellParametersProtocolGuid
,
234 return (EFI_SUCCESS
);
237 This function will retrieve the information about the file for the handle
238 specified and store it in allocated pool memory.
240 This function allocates a buffer to store the file\92s information. It is the
241 caller\92s responsibility to free the buffer
243 @param FileHandle The file handle of the file for which information is
246 @retval NULL information could not be retrieved.
248 @return the information about the file
253 IN EFI_FILE_HANDLE FileHandle
256 EFI_GUID FileInfoGuid
;
257 EFI_FILE_INFO
*pFileInfo
;
262 // ASSERT if FileHandle is NULL
264 ASSERT (FileHandle
!= NULL
);
267 // Get the required size to allocate
269 FileInfoGuid
= gEfiFileInfoGuid
;
272 Status
= FileHandle
->GetInfo(FileHandle
,
277 // error is expected. getting size to allocate
279 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
280 pFileInfo
= AllocateZeroPool(FileInfoSize
);
281 ASSERT (pFileInfo
!= NULL
);
283 // now get the information
285 Status
= FileHandle
->GetInfo(FileHandle
,
290 // if we got an error free the memory and return NULL
292 if (EFI_ERROR(Status
)) {
300 This function will set the information about the file for the opened handle
303 @param FileHandle The file handle of the file for which information
306 @param FileInfo The infotmation to set.
308 @retval EFI_SUCCESS The information was set.
309 @retval EFI_UNSUPPORTED The InformationType is not known.
310 @retval EFI_NO_MEDIA The device has no medium.
311 @retval EFI_DEVICE_ERROR The device reported an error.
312 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
313 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
314 @retval EFI_ACCESS_DENIED The file was opened read only.
315 @retval EFI_VOLUME_FULL The volume is full.
320 IN EFI_FILE_HANDLE FileHandle
,
321 IN EFI_FILE_INFO
*FileInfo
324 EFI_GUID FileInfoGuid
;
327 // ASSERT if the FileHandle or FileInfo is NULL
329 ASSERT (FileHandle
!= NULL
);
330 ASSERT (FileInfo
!= NULL
);
332 FileInfoGuid
= gEfiFileInfoGuid
;
336 return (FileHandle
->SetInfo(FileHandle
,
338 (UINTN
)FileInfo
->Size
,
343 This function will open a file or directory referenced by DevicePath.
345 This function opens a file with the open mode according to the file path. The
346 Attributes is valid only for EFI_FILE_MODE_CREATE.
348 @param FilePath on input the device path to the file. On output
349 the remaining device path.
350 @param DeviceHandle pointer to the system device handle.
351 @param FileHandle pointer to the file handle.
352 @param OpenMode the mode to open the file with.
353 @param Attributes the file's file attributes.
355 @retval EFI_SUCCESS The information was set.
356 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
357 @retval EFI_UNSUPPORTED Could not open the file path.
358 @retval EFI_NOT_FOUND The specified file could not be found on the
359 device or the file system could not be found on
361 @retval EFI_NO_MEDIA The device has no medium.
362 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
363 medium is no longer supported.
364 @retval EFI_DEVICE_ERROR The device reported an error.
365 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
366 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
367 @retval EFI_ACCESS_DENIED The file was opened read only.
368 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
370 @retval EFI_VOLUME_FULL The volume is full.
374 ShellOpenFileByDevicePath(
375 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
376 OUT EFI_HANDLE
*DeviceHandle
,
377 OUT EFI_FILE_HANDLE
*FileHandle
,
384 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
385 EFI_FILE_HANDLE LastHandle
;
388 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
390 ASSERT(FilePath
!= NULL
);
391 ASSERT(FileHandle
!= NULL
);
392 ASSERT(DeviceHandle
!= NULL
);
394 // which shell interface should we use
396 if (mEfiShellProtocol
!= NULL
) {
398 // use UEFI Shell 2.0 method.
400 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
401 if (FileName
== NULL
) {
402 return (EFI_INVALID_PARAMETER
);
404 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
409 // use old shell method.
411 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
414 if (EFI_ERROR (Status
)) {
417 Status
= gBS
->OpenProtocol(*DeviceHandle
,
418 &gEfiSimpleFileSystemProtocolGuid
,
419 &EfiSimpleFileSystemProtocol
,
422 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
423 if (EFI_ERROR (Status
)) {
426 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
427 if (EFI_ERROR (Status
)) {
433 // go down directories one node at a time.
435 while (!IsDevicePathEnd (*FilePath
)) {
437 // For file system access each node should be a file path component
439 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
440 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
443 return (EFI_INVALID_PARAMETER
);
446 // Open this file path node
448 LastHandle
= *FileHandle
;
452 // Try to test opening an existing file
454 Status
= LastHandle
->Open (
457 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
458 OpenMode
&~EFI_FILE_MODE_CREATE
,
463 // see if the error was that it needs to be created
465 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
466 Status
= LastHandle
->Open (
469 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
475 // Close the last node
477 LastHandle
->Close (LastHandle
);
479 if (EFI_ERROR(Status
)) {
486 *FilePath
= NextDevicePathNode (*FilePath
);
488 return (EFI_SUCCESS
);
493 This function will open a file or directory referenced by filename.
495 If return is EFI_SUCCESS, the Filehandle is the opened file\92s handle;
496 otherwise, the Filehandle is NULL. The Attributes is valid only for
497 EFI_FILE_MODE_CREATE.
499 if FileNAme is NULL then ASSERT()
501 @param FileName pointer to file name
502 @param FileHandle pointer to the file handle.
503 @param OpenMode the mode to open the file with.
504 @param Attributes the file's file attributes.
506 @retval EFI_SUCCESS The information was set.
507 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
508 @retval EFI_UNSUPPORTED Could not open the file path.
509 @retval EFI_NOT_FOUND The specified file could not be found on the
510 device or the file system could not be found
512 @retval EFI_NO_MEDIA The device has no medium.
513 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
514 medium is no longer supported.
515 @retval EFI_DEVICE_ERROR The device reported an error.
516 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
517 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
518 @retval EFI_ACCESS_DENIED The file was opened read only.
519 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
521 @retval EFI_VOLUME_FULL The volume is full.
527 OUT EFI_FILE_HANDLE
*FileHandle
,
532 EFI_HANDLE DeviceHandle
;
533 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
536 // ASSERT if FileName is NULL
538 ASSERT(FileName
!= NULL
);
540 if (mEfiShellProtocol
!= NULL
) {
542 // Use UEFI Shell 2.0 method
544 return (mEfiShellProtocol
->OpenFileByName(FileName
,
549 // Using EFI Shell version
550 // this means convert name to path and call that function
551 // since this will use EFI method again that will open it.
553 ASSERT(mEfiShellEnvironment2
!= NULL
);
554 FilePath
= mEfiShellEnvironment2
->NameToPath (FileName
);
555 if (FileDevicePath
!= NULL
) {
556 return (ShellOpenFileByDevicePath(&FilePath
,
562 return (EFI_DEVICE_ERROR
);
565 This function create a directory
567 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
568 otherwise, the Filehandle is NULL. If the directory already existed, this
569 function opens the existing directory.
571 @param DirectoryName pointer to directory name
572 @param FileHandle pointer to the file handle.
574 @retval EFI_SUCCESS The information was set.
575 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
576 @retval EFI_UNSUPPORTED Could not open the file path.
577 @retval EFI_NOT_FOUND The specified file could not be found on the
578 device or the file system could not be found
580 @retval EFI_NO_MEDIA The device has no medium.
581 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
582 medium is no longer supported.
583 @retval EFI_DEVICE_ERROR The device reported an error.
584 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
585 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
586 @retval EFI_ACCESS_DENIED The file was opened read only.
587 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
589 @retval EFI_VOLUME_FULL The volume is full.
590 @sa ShellOpenFileByName
594 ShellCreateDirectory(
595 IN CHAR16
*DirectoryName
,
596 OUT EFI_FILE_HANDLE
*FileHandle
600 // this is a pass thru to the open file function with sepcific open mode and attributes
602 return (ShellOpenFileByName(DirectoryName
,
604 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
610 This function reads information from an opened file.
612 If FileHandle is not a directory, the function reads the requested number of
613 bytes from the file at the file\92s current position and returns them in Buffer.
614 If the read goes beyond the end of the file, the read length is truncated to the
615 end of the file. The file\92s current position is increased by the number of bytes
616 returned. If FileHandle is a directory, the function reads the directory entry
617 at the file\92s current position and returns the entry in Buffer. If the Buffer
618 is not large enough to hold the current directory entry, then
619 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
620 BufferSize is set to be the size of the buffer needed to read the entry. On
621 success, the current position is updated to the next directory entry. If there
622 are no more directory entries, the read returns a zero-length buffer.
623 EFI_FILE_INFO is the structure returned as the directory entry.
625 @param FileHandle the opened file handle
626 @param BufferSize on input the size of buffer in bytes. on return
627 the number of bytes written.
628 @param Buffer the buffer to put read data into.
630 @retval EFI_SUCCESS Data was read.
631 @retval EFI_NO_MEDIA The device has no media.
632 @retval EFI_DEVICE_ERROR The device reported an error.
633 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
634 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
641 IN EFI_FILE_HANDLE FileHandle
,
642 IN OUT UINTN
*BufferSize
,
647 // ASSERT if FileHandle is NULL
649 ASSERT (FileHandle
!= NULL
);
652 // Perform the read based on EFI_FILE_PROTOCOL
654 return (FileHandle
->Read(FileHandle
, BufferSize
, Buffer
));
659 Write data to a file.
661 This function writes the specified number of bytes to the file at the current
662 file position. The current file position is advanced the actual number of bytes
663 written, which is returned in BufferSize. Partial writes only occur when there
664 has been a data error during the write attempt (such as \93volume space full\94).
665 The file is automatically grown to hold the data if required. Direct writes to
666 opened directories are not supported.
668 @param FileHandle The opened file for writing
669 @param BufferSize on input the number of bytes in Buffer. On output
670 the number of bytes written.
671 @param Buffer the buffer containing data to write is stored.
673 @retval EFI_SUCCESS Data was written.
674 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
675 @retval EFI_NO_MEDIA The device has no media.
676 @retval EFI_DEVICE_ERROR The device reported an error.
677 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
678 @retval EFI_WRITE_PROTECTED The device is write-protected.
679 @retval EFI_ACCESS_DENIED The file was open for read only.
680 @retval EFI_VOLUME_FULL The volume is full.
685 IN EFI_FILE_HANDLE FileHandle
,
686 IN OUT UINTN
*BufferSize
,
691 // ASSERT if FileHandle is NULL
693 ASSERT (FileHandle
!= NULL
);
695 // Perform the write based on EFI_FILE_PROTOCOL
697 return (FileHandle
->Write(FileHandle
, BufferSize
, Buffer
));
701 Close an open file handle.
703 This function closes a specified file handle. All \93dirty\94 cached file data is
704 flushed to the device, and the file is closed. In all cases the handle is
707 @param FileHandle the file handle to close.
709 @retval EFI_SUCCESS the file handle was closed sucessfully.
714 IN EFI_FILE_HANDLE
*FileHandle
719 // ASSERT if FileHandle is NULL
721 ASSERT (FileHandle
!= NULL
);
722 ASSERT (*FileHandle
!= NULL
);
724 // Perform the Close based on EFI_FILE_PROTOCOL
726 Status
= (*FileHandle
)->Close(*FileHandle
);
732 Delete a file and close the handle
734 This function closes and deletes a file. In all cases the file handle is closed.
735 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
736 returned, but the handle is still closed.
738 @param FileHandle the file handle to delete
740 @retval EFI_SUCCESS the file was closed sucessfully
741 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
743 @retval INVALID_PARAMETER One of the parameters has an invalid value.
748 IN EFI_FILE_HANDLE
*FileHandle
753 // ASSERT if FileHandle is NULL
755 ASSERT (FileHandle
!= NULL
);
756 ASSERT (*FileHandle
!= NULL
);
758 // Perform the Delete based on EFI_FILE_PROTOCOL
760 Status
= (*FileHandle
)->Delete(*FileHandle
);
766 Set the current position in a file.
768 This function sets the current file position for the handle to the position
769 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
770 absolute positioning is supported, and seeking past the end of the file is
771 allowed (a subsequent write would grow the file). Seeking to position
772 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
773 If FileHandle is a directory, the only position that may be set is zero. This
774 has the effect of starting the read process of the directory entries over.
776 @param FileHandle The file handle on which the position is being set
777 @param Position Byte position from begining of file
779 @retval EFI_SUCCESS Operation completed sucessfully.
780 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
782 @retval INVALID_PARAMETER One of the parameters has an invalid value.
786 ShellSetFilePosition (
787 IN EFI_FILE_HANDLE FileHandle
,
792 // ASSERT if FileHandle is NULL
794 ASSERT (FileHandle
!= NULL
);
796 // Perform the SetPosition based on EFI_FILE_PROTOCOL
798 return (FileHandle
->SetPosition(FileHandle
, Position
));
802 Gets a file's current position
804 This function retrieves the current file position for the file handle. For
805 directories, the current file position has no meaning outside of the file
806 system driver and as such the operation is not supported. An error is returned
807 if FileHandle is a directory.
809 @param FileHandle The open file handle on which to get the position.
810 @param Position Byte position from begining of file.
812 @retval EFI_SUCCESS the operation completed sucessfully.
813 @retval INVALID_PARAMETER One of the parameters has an invalid value.
814 @retval EFI_UNSUPPORTED the request is not valid on directories.
818 ShellGetFilePosition (
819 IN EFI_FILE_HANDLE FileHandle
,
824 // ASSERT if FileHandle is NULL
826 ASSERT (FileHandle
!= NULL
);
828 // Perform the GetPosition based on EFI_FILE_PROTOCOL
830 return (FileHandle
->GetPosition(FileHandle
, Position
));
833 Flushes data on a file
835 This function flushes all modified data associated with a file to a device.
837 @param FileHandle The file handle on which to flush data
839 @retval EFI_SUCCESS The data was flushed.
840 @retval EFI_NO_MEDIA The device has no media.
841 @retval EFI_DEVICE_ERROR The device reported an error.
842 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
843 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
844 @retval EFI_ACCESS_DENIED The file was opened for read only.
849 IN EFI_FILE_HANDLE FileHandle
853 // ASSERT if FileHandle is NULL
855 ASSERT (FileHandle
!= NULL
);
857 // Perform the Flush based on EFI_FILE_PROTOCOL
859 return (FileHandle
->Flush(FileHandle
));
863 function to determine if a given handle is a directory handle
865 if DirHandle is NULL then ASSERT()
867 open the file information on the DirHandle and verify that the Attribute
868 includes EFI_FILE_DIRECTORY bit set.
870 @param DirHandle Handle to open file
872 @retval EFI_SUCCESS DirHandle is a directory
873 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
874 @retval EFI_NOT_FOUND DirHandle is not a directory
878 InternalShellIsDirectory (
879 IN EFI_FILE_HANDLE DirHandle
882 EFI_FILE_INFO
*DirInfo
;
885 // ASSERT if DirHandle is NULL
887 ASSERT(DirHandle
!= NULL
);
890 // get the file information for DirHandle
892 DirInfo
= ShellGetFileInfo (DirHandle
);
897 if (DirInfo
== NULL
) {
901 return (EFI_INVALID_PARAMETER
);
903 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
905 // Attributes say this is not a directory
908 return (EFI_NOT_FOUND
);
914 return (EFI_SUCCESS
);
918 Retrieves the first file from a directory
920 This function opens a directory and gets the first file\92s info in the
921 directory. Caller can use ShellFindNextFile() to get other files. When
922 complete the caller is responsible for calling FreePool() on Buffer.
924 @param DirHandle The file handle of the directory to search
925 @param Buffer Pointer to buffer for file's information
927 @retval EFI_SUCCESS Found the first file.
928 @retval EFI_NOT_FOUND Cannot find the directory.
929 @retval EFI_NO_MEDIA The device has no media.
930 @retval EFI_DEVICE_ERROR The device reported an error.
931 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
932 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
938 IN EFI_FILE_HANDLE DirHandle
,
939 OUT EFI_FILE_INFO
*Buffer
946 // ASSERT if DirHandle is NULL
948 ASSERT (DirHandle
!= NULL
);
951 // verify that DirHandle is a directory
953 Status
= InternalShellIsDirectory(DirHandle
);
954 if (EFI_ERROR(Status
)) {
959 // reset to the begining of the directory
961 Status
= ShellSetFilePosition (DirHandle
, 0);
962 if (EFI_ERROR(Status
)) {
967 // Allocate a buffer sized to struct size + enough for the string at the end
969 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
970 Buffer
= AllocateZeroPool(BufferSize
);
971 ASSERT (Buffer
!= NULL
);
974 // read in the info about the first file
976 Status
= ShellReadFile (DirHandle
, &BufferSize
, Buffer
);
977 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
978 if (EFI_ERROR(Status
)) {
981 return (EFI_SUCCESS
);
984 Retrieves the next file in a directory.
986 To use this function, caller must call the LibFindFirstFile() to get the
987 first file, and then use this function get other files. This function can be
988 called for several times to get each file's information in the directory. If
989 the call of ShellFindNextFile() got the last file in the directory, the next
990 call of this function has no file to get. *NoFile will be set to TRUE and the
991 Buffer memory will be automatically freed.
993 @param DirHandle the file handle of the directory
994 @param Buffer pointer to buffer for file's information
995 @param NoFile pointer to boolean when last file is found
997 @retval EFI_SUCCESS Found the next file, or reached last file
998 @retval EFI_NO_MEDIA The device has no media.
999 @retval EFI_DEVICE_ERROR The device reported an error.
1000 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1005 IN EFI_FILE_HANDLE DirHandle
,
1006 OUT EFI_FILE_INFO
*Buffer
,
1014 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
1016 ASSERT (DirHandle
!= NULL
);
1017 ASSERT (Buffer
!= NULL
);
1018 ASSERT (NoFile
!= NULL
);
1021 // verify that DirHandle is a directory
1023 Status
= InternalShellIsDirectory(DirHandle
);
1024 if (EFI_ERROR(Status
)) {
1029 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
1031 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
1034 // read in the info about the next file
1036 Status
= ShellReadFile (DirHandle
, &BufferSize
, Buffer
);
1037 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
1038 if (EFI_ERROR(Status
)) {
1043 // If we read 0 bytes (but did not have erros) we already read in the last file.
1045 if (BufferSize
== 0) {
1050 return (EFI_SUCCESS
);
1053 Retrieve the size of a file.
1055 if FileHandle is NULL then ASSERT()
1056 if Size is NULL then ASSERT()
1058 This function extracts the file size info from the FileHandle\92s EFI_FILE_INFO
1061 @param FileHandle file handle from which size is retrieved
1062 @param Size pointer to size
1064 @retval EFI_SUCCESS operation was completed sucessfully
1065 @retval EFI_DEVICE_ERROR cannot access the file
1070 IN EFI_FILE_HANDLE FileHandle
,
1074 EFI_FILE_INFO
*FileInfo
;
1077 // ASSERT for FileHandle or Size being NULL
1079 ASSERT (FileHandle
!= NULL
);
1080 ASSERT (Size
!= NULL
);
1083 // get the FileInfo structure
1085 FileInfo
= ShellGetFileInfo(FileHandle
);
1086 if (FileInfo
== NULL
) {
1087 return (EFI_DEVICE_ERROR
);
1091 // Assign the Size pointer to the correct value
1093 *Size
= FileInfo
->FileSize
;
1096 // free the FileInfo memory
1100 return (EFI_SUCCESS
);
1103 Retrieves the status of the break execution flag
1105 this function is useful to check whether the application is being asked to halt by the shell.
1107 @retval TRUE the execution break is enabled
1108 @retval FALSE the execution break is not enabled
1112 ShellGetExecutionBreakFlag(
1117 // Check for UEFI Shell 2.0 protocols
1119 if (mEfiShellProtocol
!= NULL
) {
1122 // We are using UEFI Shell 2.0; see if the event has been triggered
1124 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1131 // using EFI Shell; call the function to check
1133 ASSERT(mEfiShellEnvironment2
!= NULL
);
1134 return (mEfiShellEnvironment2
->GetExecutionBreak());
1137 return the value of an environment variable
1139 this function gets the value of the environment variable set by the
1140 ShellSetEnvironmentVariable function
1142 @param EnvKey The key name of the environment variable.
1144 @retval NULL the named environment variable does not exist.
1145 @return != NULL pointer to the value of the environment variable
1149 ShellGetEnvironmentVariable (
1154 // Check for UEFI Shell 2.0 protocols
1156 if (mEfiShellProtocol
!= NULL
) {
1157 return (mEfiShellProtocol
->GetEnv(EnvKey
));
1161 // ASSERT that we must have EFI shell
1163 ASSERT(mEfiShellEnvironment2
!= NULL
);
1168 return (mEfiShellEnvironment2
->GetEnv(EnvKey
));
1171 set the value of an environment variable
1173 This function changes the current value of the specified environment variable. If the
1174 environment variable exists and the Value is an empty string, then the environment
1175 variable is deleted. If the environment variable exists and the Value is not an empty
1176 string, then the value of the environment variable is changed. If the environment
1177 variable does not exist and the Value is an empty string, there is no action. If the
1178 environment variable does not exist and the Value is a non-empty string, then the
1179 environment variable is created and assigned the specified value.
1181 This is not supported pre-UEFI Shell 2.0.
1183 @param EnvKey The key name of the environment variable.
1184 @param EnvVal The Value of the environment variable
1185 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1187 @retval EFI_SUCCESS the operation was completed sucessfully
1188 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1192 ShellSetEnvironmentVariable (
1193 IN CONST CHAR16
*EnvKey
,
1194 IN CONST CHAR16
*EnvVal
,
1199 // Check for UEFI Shell 2.0 protocols
1201 if (mEfiShellProtocol
!= NULL
) {
1202 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1206 // This feature does not exist under EFI shell
1208 return (EFI_UNSUPPORTED
);
1211 cause the shell to parse and execute a command line.
1213 This function creates a nested instance of the shell and executes the specified
1214 command (CommandLine) with the specified environment (Environment). Upon return,
1215 the status code returned by the specified command is placed in StatusCode.
1216 If Environment is NULL, then the current environment is used and all changes made
1217 by the commands executed will be reflected in the current environment. If the
1218 Environment is non-NULL, then the changes made will be discarded.
1219 The CommandLine is executed from the current working directory on the current
1222 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1223 Output is only supported for pre-UEFI Shell 2.0
1225 @param ImageHandle Parent image that is starting the operation
1226 @param CommandLine pointer to null terminated command line.
1227 @param Output true to display debug output. false to hide it.
1228 @param EnvironmentVariables optional pointer to array of environment variables
1229 in the form "x=y". if NULL current set is used.
1230 @param Status the status of the run command line.
1232 @retval EFI_SUCCESS the operation completed sucessfully. Status
1233 contains the status code returned.
1234 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1235 @retval EFI_OUT_OF_RESOURCES out of resources
1236 @retval EFI_UNSUPPORTED the operation is not allowed.
1241 IN EFI_HANDLE
*ParentHandle
,
1242 IN CHAR16
*CommandLine OPTIONAL
,
1243 IN BOOLEAN Output OPTIONAL
,
1244 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1245 OUT EFI_STATUS
*Status OPTIONAL
1249 // Check for UEFI Shell 2.0 protocols
1251 if (mEfiShellProtocol
!= NULL
) {
1253 // Call UEFI Shell 2.0 version (not using Output parameter)
1255 return (mEfiShellProtocol
->Execute(ParentHandle
,
1257 EnvironmentVariables
,
1261 // ASSERT that we must have EFI shell
1263 ASSERT(mEfiShellEnvironment2
!= NULL
);
1265 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1266 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1268 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1273 Retreives the current directory path
1275 If the DeviceName is NULL, it returns the current device\92s current directory
1276 name. If the DeviceName is not NULL, it returns the current directory name
1279 @param DeviceName the name of the drive to get directory on
1281 @retval NULL the directory does not exist
1282 @return != NULL the directory
1286 ShellGetCurrentDir (
1287 IN CHAR16
*DeviceName OPTIONAL
1291 // Check for UEFI Shell 2.0 protocols
1293 if (mEfiShellProtocol
!= NULL
) {
1294 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1297 // ASSERT that we must have EFI shell
1299 ASSERT(mEfiShellEnvironment2
!= NULL
);
1300 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1303 sets (enabled or disabled) the page break mode
1305 when page break mode is enabled the screen will stop scrolling
1306 and wait for operator input before scrolling a subsequent screen.
1308 @param CurrentState TRUE to enable and FALSE to disable
1312 ShellSetPageBreakMode (
1313 IN BOOLEAN CurrentState
1317 // check for enabling
1319 if (CurrentState
!= 0x00) {
1321 // check for UEFI Shell 2.0
1323 if (mEfiShellProtocol
!= NULL
) {
1325 // Enable with UEFI 2.0 Shell
1327 mEfiShellProtocol
->EnablePageBreak();
1331 // ASSERT that must have EFI Shell
1333 ASSERT(mEfiShellEnvironment2
!= NULL
);
1335 // Enable with EFI Shell
1337 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1342 // check for UEFI Shell 2.0
1344 if (mEfiShellProtocol
!= NULL
) {
1346 // Disable with UEFI 2.0 Shell
1348 mEfiShellProtocol
->DisablePageBreak();
1352 // ASSERT that must have EFI Shell
1354 ASSERT(mEfiShellEnvironment2
!= NULL
);
1356 // Disable with EFI Shell
1358 mEfiShellEnvironment2
->DisablePageBreak ();
1365 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1366 /// This allows for the struct to be populated.
1369 EFI_LIST_ENTRY Link
;
1373 EFI_FILE_HANDLE Handle
;
1374 EFI_FILE_INFO
*Info
;
1375 } EFI_SHELL_FILE_INFO_NO_CONST
;
1378 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1380 if OldStyleFileList is NULL then ASSERT()
1382 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1383 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1384 the ShellCloseFileMetaArg function.
1386 @param FileList the EFI shell list type
1388 @retval the resultant head of the double linked new format list;
1392 InternalShellConvertFileListType (
1393 EFI_LIST_ENTRY
*FileList
1396 LIST_ENTRY
*ListHead
;
1397 SHELL_FILE_ARG
*OldInfo
;
1398 EFI_LIST_ENTRY
*Link
;
1399 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1402 // ASSERT that FileList is not NULL
1404 ASSERT(FileList
!= NULL
);
1407 // Allocate our list head and initialize the list
1409 ListHead
= AllocateZeroPool(sizeof(EFI_LIST_ENTRY
));
1410 ASSERT (ListHead
!= NULL
);
1411 ListHead
= InitializeListHead (ListHead
);
1414 // enumerate through each member of the old list and copy
1416 for (Link
= FileList
->Flink
; Link
!= FileList
; Link
= Link
->Flink
) {
1417 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1420 // make sure the old list was valid
1422 ASSERT(OldInfo
!= NULL
);
1423 ASSERT(OldInfo
->Info
!= NULL
);
1424 ASSERT(OldInfo
->FullName
!= NULL
);
1425 ASSERT(OldInfo
->FileName
!= NULL
);
1428 // allocate a new EFI_SHELL_FILE_INFO object
1430 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1433 // copy the simple items
1435 NewInfo
->Handle
= OldInfo
->Handle
;
1436 NewInfo
->Status
= OldInfo
->Status
;
1439 // allocate new space to copy strings and structure
1441 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1442 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1443 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1446 // make sure all the memory allocations were sucessful
1448 ASSERT(NewInfo
->FullName
!= NULL
);
1449 ASSERT(NewInfo
->FileName
!= NULL
);
1450 ASSERT(NewInfo
->Info
!= NULL
);
1453 // Copt the strings and structure
1455 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1456 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1457 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1460 // add that to the list
1462 InsertTailList(ListHead
, (LIST_ENTRY
*)NewInfo
);
1467 Opens a group of files based on a path.
1469 This function uses the Arg to open all the matching files. Each matched
1470 file has a SHELL_FILE_ARG structure to record the file information. These
1471 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1472 structures from ListHead to access each file. This function supports wildcards
1473 and will process '?' and '*' as such. the list must be freed with a call to
1474 ShellCloseFileMetaArg().
1476 This function will fail if called sequentially without freeing the list in the middle.
1478 @param Arg pointer to path string
1479 @param OpenMode mode to open files with
1480 @param ListHead head of linked list of results
1482 @retval EFI_SUCCESS the operation was sucessful and the list head
1483 contains the list of opened files
1484 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1485 *ListHead is set to NULL.
1486 @return != EFI_SUCCESS the operation failed
1488 @sa InternalShellConvertFileListType
1492 ShellOpenFileMetaArg (
1495 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1499 LIST_ENTRY
*EmptyNode
;
1502 // make sure we have no outstanding list
1504 if (mOldStyleFileList
!= NULL
) {
1506 return (EFI_UNSUPPORTED
);
1510 // ASSERT that Arg and ListHead are not NULL
1512 ASSERT(Arg
!= NULL
);
1513 ASSERT(ListHead
!= NULL
);
1516 // Check for UEFI Shell 2.0 protocols
1518 if (mEfiShellProtocol
!= NULL
) {
1519 return (mEfiShellProtocol
->OpenFileList(Arg
,
1525 // ASSERT that we must have EFI shell
1527 ASSERT(mEfiShellEnvironment2
!= NULL
);
1530 // allocate memory for old list head
1532 mOldStyleFileList
= (EFI_LIST_ENTRY
*)AllocatePool(sizeof(EFI_LIST_ENTRY
));
1533 ASSERT(mOldStyleFileList
!= NULL
);
1536 // make sure the list head is initialized
1538 InitializeListHead((LIST_ENTRY
*)mOldStyleFileList
);
1541 // Get the EFI Shell list of files
1543 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, mOldStyleFileList
);
1544 if (EFI_ERROR(Status
)) {
1550 // Convert that to equivalent of UEFI Shell 2.0 structure
1552 EmptyNode
= InternalShellConvertFileListType(mOldStyleFileList
);
1555 // remove the empty head of the list
1557 *ListHead
= (EFI_SHELL_FILE_INFO
*)RemoveEntryList(EmptyNode
);
1558 FreePool(EmptyNode
);
1563 Free the linked list returned from ShellOpenFileMetaArg
1565 if ListHead is NULL then ASSERT()
1567 @param ListHead the pointer to free
1569 @retval EFI_SUCCESS the operation was sucessful
1573 ShellCloseFileMetaArg (
1574 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1580 // ASSERT that ListHead is not NULL
1582 ASSERT(ListHead
!= NULL
);
1585 // Check for UEFI Shell 2.0 protocols
1587 if (mEfiShellProtocol
!= NULL
) {
1588 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1591 // Free the EFI Shell version that was converted.
1593 ASSERT_EFI_ERROR(mEfiShellEnvironment2
->FreeFileList(mOldStyleFileList
));
1594 FreePool(mOldStyleFileList
);
1595 mOldStyleFileList
= NULL
;
1598 // Since this is EFI Shell version we need to free our internally made copy
1601 for (Node
= GetFirstNode((LIST_ENTRY
*)*ListHead
) ; IsListEmpty((LIST_ENTRY
*)*ListHead
) == FALSE
; Node
= GetFirstNode((LIST_ENTRY
*)*ListHead
)) {
1602 RemoveEntryList(Node
);
1603 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1604 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1605 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1606 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1613 EFI_LIST_ENTRY List
;
1617 UINTN OriginalPosition
;
1618 } SHELL_PARAM_PACKAGE
;
1621 Checks the list of valid arguments and returns TRUE if the item was found. If the
1622 return value is TRUE then the type parameter is set also.
1624 if CheckList is NULL then ASSERT();
1625 if Name is NULL then ASSERT();
1626 if Type is NULL then ASSERT();
1628 @param Type pointer to type of parameter if it was found
1629 @param Name pointer to Name of parameter found
1630 @param CheckList List to check against
1632 @retval TRUE the Parameter was found. Type is valid.
1633 @retval FALSE the Parameter was not found. Type is not valid.
1638 IN CONST CHAR16
*Name
,
1639 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1643 SHELL_PARAM_ITEM
*TempListItem
;
1646 // ASSERT that all 3 pointer parameters aren't NULL
1648 ASSERT(CheckList
!= NULL
);
1649 ASSERT(Type
!= NULL
);
1650 ASSERT(Name
!= NULL
);
1653 // Enumerate through the list
1655 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1657 // If the Name matches set the type and return TRUE
1659 if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1660 *Type
= TempListItem
->Type
;
1667 Checks the string for indicators of "flag" status. this is a leading '/' or '-'
1669 @param Name pointer to Name of parameter found
1671 @retval TRUE the Parameter is a flag.
1672 @retval FALSE the Parameter not a flag
1677 IN CONST CHAR16
*Name
1681 // ASSERT that Name isn't NULL
1683 ASSERT(Name
!= NULL
);
1686 // If the Name has a / or - as the first character return TRUE
1688 if ((Name
[0] == L
'/') || (Name
[0] == L
'-') ) {
1695 Checks the command line arguments passed against the list of valid ones.
1697 If no initialization is required, then return RETURN_SUCCESS.
1699 @param CheckList pointer to list of parameters to check
1700 @param CheckPackage pointer to pointer to list checked values
1701 @param ProblemParam optional pointer to pointer to unicode string for
1702 the paramater that caused failure.
1703 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1704 @param Argc Count of parameters in Argv
1705 @param Argv pointer to array of parameters
1707 @retval EFI_SUCCESS The operation completed sucessfully.
1708 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1709 @retval EFI_INVALID_PARAMETER A parameter was invalid
1710 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1711 duplicated. the duplicated command line argument
1712 was returned in ProblemParam if provided.
1713 @retval EFI_NOT_FOUND a argument required a value that was missing.
1714 the invalid command line argument was returned in
1715 ProblemParam if provided.
1719 InternalCommandLineParse (
1720 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1721 OUT LIST_ENTRY
**CheckPackage
,
1722 OUT CHAR16
**ProblemParam OPTIONAL
,
1723 IN BOOLEAN AutoPageBreak
,
1724 IN CONST CHAR16
**Argv
,
1730 ParamType CurrentItemType
;
1731 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1732 BOOLEAN GetItemValue
;
1734 CurrentItemPackage
= NULL
;
1739 ASSERT(CheckList
!= NULL
);
1740 ASSERT(Argv
!= NULL
);
1743 GetItemValue
= FALSE
;
1746 // If there is only 1 item we dont need to do anything
1749 *CheckPackage
= NULL
;
1750 return (EFI_SUCCESS
);
1754 // initialize the linked list
1756 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1757 InitializeListHead(*CheckPackage
);
1760 // loop through each of the arguments
1762 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1763 if (Argv
[LoopCounter
] == NULL
) {
1765 // do nothing for NULL argv
1767 } else if (GetItemValue
== TRUE
) {
1768 ASSERT(CurrentItemPackage
!= NULL
);
1770 // get the item VALUE for the previous flag
1772 GetItemValue
= FALSE
;
1773 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1774 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1775 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1776 InsertTailList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1777 } else if (IsFlag(Argv
[LoopCounter
]) == FALSE
) {
1779 // add this one as a non-flag
1781 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1782 ASSERT(CurrentItemPackage
!= NULL
);
1783 CurrentItemPackage
->Name
= NULL
;
1784 CurrentItemPackage
->Type
= TypePosition
;
1785 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1786 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1787 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1788 CurrentItemPackage
->OriginalPosition
= Count
++;
1789 InsertTailList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1790 } else if (IsCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) == TRUE
) {
1794 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1795 ASSERT(CurrentItemPackage
!= NULL
);
1796 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1797 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1798 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1799 CurrentItemPackage
->Type
= CurrentItemType
;
1800 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1803 // Does this flag require a value
1805 if (CurrentItemPackage
->Type
== TypeValue
) {
1807 // trigger the next loop to populate the value of this item
1809 GetItemValue
= TRUE
;
1812 // this item has no value expected; we are done
1814 CurrentItemPackage
->Value
= NULL
;
1815 InsertTailList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1817 } else if (ProblemParam
) {
1819 // this was a non-recognised flag... error!
1821 *ProblemParam
= (CHAR16
*)Argv
[LoopCounter
];
1822 ShellCommandLineFreeVarList(*CheckPackage
);
1823 *CheckPackage
= NULL
;
1824 return (EFI_VOLUME_CORRUPTED
);
1826 ShellCommandLineFreeVarList(*CheckPackage
);
1827 *CheckPackage
= NULL
;
1828 return (EFI_VOLUME_CORRUPTED
);
1832 // support for AutoPageBreak
1834 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1835 ShellSetPageBreakMode(TRUE
);
1837 return (EFI_SUCCESS
);
1841 Checks the command line arguments passed against the list of valid ones.
1842 Optionally removes NULL values first.
1844 If no initialization is required, then return RETURN_SUCCESS.
1846 @param CheckList pointer to list of parameters to check
1847 @param CheckPackage pointer to pointer to list checked values
1848 @param ProblemParam optional pointer to pointer to unicode string for
1849 the paramater that caused failure.
1850 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1852 @retval EFI_SUCCESS The operation completed sucessfully.
1853 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1854 @retval EFI_INVALID_PARAMETER A parameter was invalid
1855 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1856 duplicated. the duplicated command line argument
1857 was returned in ProblemParam if provided.
1858 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1859 of the command line arguments was returned in
1860 ProblemParam if provided.
1861 @retval EFI_NOT_FOUND a argument required a value that was missing.
1862 the invalid command line argument was returned in
1863 ProblemParam if provided.
1867 ShellCommandLineParse (
1868 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1869 OUT LIST_ENTRY
**CheckPackage
,
1870 OUT CHAR16
**ProblemParam OPTIONAL
,
1871 IN BOOLEAN AutoPageBreak
1875 // ASSERT that CheckList and CheckPackage aren't NULL
1877 ASSERT(CheckList
!= NULL
);
1878 ASSERT(CheckPackage
!= NULL
);
1881 // Check for UEFI Shell 2.0 protocols
1883 if (mEfiShellParametersProtocol
!= NULL
) {
1884 return (InternalCommandLineParse(CheckList
,
1888 mEfiShellParametersProtocol
->Argv
,
1889 mEfiShellParametersProtocol
->Argc
));
1893 // ASSERT That EFI Shell is not required
1895 ASSERT (mEfiShellInterface
!= NULL
);
1896 return (InternalCommandLineParse(CheckList
,
1900 mEfiShellInterface
->Argv
,
1901 mEfiShellInterface
->Argc
));
1905 Frees shell variable list that was returned from ShellCommandLineParse.
1907 This function will free all the memory that was used for the CheckPackage
1908 list of postprocessed shell arguments.
1910 this function has no return value.
1912 if CheckPackage is NULL, then return
1914 @param CheckPackage the list to de-allocate
1918 ShellCommandLineFreeVarList (
1919 IN LIST_ENTRY
*CheckPackage
1925 // check for CheckPackage == NULL
1927 if (CheckPackage
== NULL
) {
1932 // for each node in the list
1934 for (Node
= GetFirstNode(CheckPackage
); Node
!= CheckPackage
; Node
= GetFirstNode(CheckPackage
)) {
1936 // Remove it from the list
1938 RemoveEntryList(Node
);
1941 // if it has a name free the name
1943 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1944 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1948 // if it has a value free the value
1950 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
1951 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1955 // free the node structure
1957 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
1960 // free the list head node
1962 FreePool(CheckPackage
);
1965 Checks for presence of a flag parameter
1967 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1969 if CheckPackage is NULL then return FALSE.
1970 if KeyString is NULL then ASSERT()
1972 @param CheckPackage The package of parsed command line arguments
1973 @param KeyString the Key of the command line argument to check for
1975 @retval TRUE the flag is on the command line
1976 @retval FALSE the flag is not on the command line
1980 ShellCommandLineGetFlag (
1981 IN CONST LIST_ENTRY
*CheckPackage
,
1982 IN CHAR16
*KeyString
1988 // ASSERT that both CheckPackage and KeyString aren't NULL
1990 ASSERT(KeyString
!= NULL
);
1993 // return FALSE for no package
1995 if (CheckPackage
== NULL
) {
2000 // enumerate through the list of parametrs
2002 for (Node
= GetFirstNode(CheckPackage
) ; Node
!= CheckPackage
; Node
= GetNextNode(CheckPackage
, Node
) ) {
2004 // If the Name matches, return TRUE (and there may be NULL name)
2006 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2007 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2015 returns value from command line argument
2017 value parameters are in the form of "-<Key> value" or "/<Key> value"
2019 if CheckPackage is NULL, then return NULL;
2021 @param CheckPackage The package of parsed command line arguments
2022 @param KeyString the Key of the command line argument to check for
2024 @retval NULL the flag is not on the command line
2025 @return !=NULL pointer to unicode string of the value
2029 ShellCommandLineGetValue (
2030 IN CONST LIST_ENTRY
*CheckPackage
,
2031 IN CHAR16
*KeyString
2037 // check for CheckPackage == NULL
2039 if (CheckPackage
== NULL
) {
2044 // enumerate through the list of parametrs
2046 for (Node
= GetFirstNode(CheckPackage
) ; Node
!= NULL
; Node
= GetNextNode(CheckPackage
, Node
) ) {
2048 // If the Name matches, return the value (name can be NULL)
2050 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2051 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2052 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2059 returns raw value from command line argument
2061 raw value parameters are in the form of "value" in a specific position in the list
2063 if CheckPackage is NULL, then return NULL;
2065 @param CheckPackage The package of parsed command line arguments
2066 @param Position the position of the value
2068 @retval NULL the flag is not on the command line
2069 @return !=NULL pointer to unicode string of the value
2073 ShellCommandLineGetRawValue (
2074 IN CONST LIST_ENTRY
*CheckPackage
,
2081 // check for CheckPackage == NULL
2083 if (CheckPackage
== NULL
) {
2088 // enumerate through the list of parametrs
2090 for (Node
= GetFirstNode(CheckPackage
) ; Node
!= NULL
; Node
= GetNextNode(CheckPackage
, Node
) ) {
2092 // If the position matches, return the value
2094 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2095 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);