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 <Library/PcdLib.h>
24 #include <Library/FileHandleLib.h>
25 #include <Library/PrintLib.h>
26 #include <Library/UefiLib.h>
28 #include <Protocol/EfiShellEnvironment2.h>
29 #include <Protocol/EfiShellInterface.h>
30 #include <Protocol/EfiShell.h>
31 #include <Protocol/EfiShellParameters.h>
32 #include <Protocol/SimpleFileSystem.h>
34 #include "UefiShellLib.h"
36 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
37 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
40 // This is not static since it's extern in the .h file
42 SHELL_PARAM_ITEM EmptyParamList
[] = {
47 // Static file globals for the shell library
49 STATIC EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
50 STATIC EFI_SHELL_INTERFACE
*mEfiShellInterface
;
51 STATIC EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
52 STATIC EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
53 STATIC EFI_HANDLE mEfiShellEnvironment2Handle
;
54 STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
57 helper function to find ShellEnvironment2 for constructor
62 IN EFI_HANDLE ImageHandle
72 Status
= gBS
->OpenProtocol(ImageHandle
,
73 &gEfiShellEnvironment2Guid
,
74 (VOID
**)&mEfiShellEnvironment2
,
77 EFI_OPEN_PROTOCOL_GET_PROTOCOL
80 // look for the mEfiShellEnvironment2 protocol at a higher level
82 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
)){
84 // figure out how big of a buffer we need.
86 Status
= gBS
->LocateHandle (ByProtocol
,
87 &gEfiShellEnvironment2Guid
,
88 NULL
, // ignored for ByProtocol
92 ASSERT(Status
== EFI_BUFFER_TOO_SMALL
);
93 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
94 ASSERT(Buffer
!= NULL
);
95 Status
= gBS
->LocateHandle (ByProtocol
,
96 &gEfiShellEnvironment2Guid
,
97 NULL
, // ignored for ByProtocol
101 if (!EFI_ERROR (Status
)) {
103 // now parse the list of returned handles
105 Status
= EFI_NOT_FOUND
;
106 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
107 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
108 &gEfiShellEnvironment2Guid
,
109 (VOID
**)&mEfiShellEnvironment2
,
112 EFI_OPEN_PROTOCOL_GET_PROTOCOL
114 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
) {
115 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
116 Status
= EFI_SUCCESS
;
122 if (Buffer
!= NULL
) {
130 ShellLibConstructorWorker (
131 IN EFI_HANDLE ImageHandle
,
132 IN EFI_SYSTEM_TABLE
*SystemTable
137 // UEFI 2.0 shell interfaces (used preferentially)
139 Status
= gBS
->OpenProtocol(ImageHandle
,
140 &gEfiShellProtocolGuid
,
141 (VOID
**)&mEfiShellProtocol
,
144 EFI_OPEN_PROTOCOL_GET_PROTOCOL
146 if (EFI_ERROR(Status
)) {
147 mEfiShellProtocol
= NULL
;
149 Status
= gBS
->OpenProtocol(ImageHandle
,
150 &gEfiShellParametersProtocolGuid
,
151 (VOID
**)&mEfiShellParametersProtocol
,
154 EFI_OPEN_PROTOCOL_GET_PROTOCOL
156 if (EFI_ERROR(Status
)) {
157 mEfiShellParametersProtocol
= NULL
;
160 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
162 // Moved to seperate function due to complexity
164 Status
= ShellFindSE2(ImageHandle
);
166 if (EFI_ERROR(Status
)) {
167 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
168 mEfiShellEnvironment2
= NULL
;
170 Status
= gBS
->OpenProtocol(ImageHandle
,
171 &gEfiShellInterfaceGuid
,
172 (VOID
**)&mEfiShellInterface
,
175 EFI_OPEN_PROTOCOL_GET_PROTOCOL
177 if (EFI_ERROR(Status
)) {
178 mEfiShellInterface
= NULL
;
182 // only success getting 2 of either the old or new, but no 1/2 and 1/2
184 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
185 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
186 if (mEfiShellProtocol
!= NULL
) {
187 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
188 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
189 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
190 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
191 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
192 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
193 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
194 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
195 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
196 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
198 FileFunctionMap
.GetFileInfo
= FileHandleGetInfo
;
199 FileFunctionMap
.SetFileInfo
= FileHandleSetInfo
;
200 FileFunctionMap
.ReadFile
= FileHandleRead
;
201 FileFunctionMap
.WriteFile
= FileHandleWrite
;
202 FileFunctionMap
.CloseFile
= FileHandleClose
;
203 FileFunctionMap
.DeleteFile
= FileHandleDelete
;
204 FileFunctionMap
.GetFilePosition
= FileHandleGetPosition
;
205 FileFunctionMap
.SetFilePosition
= FileHandleSetPosition
;
206 FileFunctionMap
.FlushFile
= FileHandleFlush
;
207 FileFunctionMap
.GetFileSize
= FileHandleGetSize
;
209 return (EFI_SUCCESS
);
211 return (EFI_NOT_FOUND
);
214 Constructor for the Shell library.
216 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
218 @param ImageHandle the image handle of the process
219 @param SystemTable the EFI System Table pointer
221 @retval EFI_SUCCESS the initialization was complete sucessfully
222 @return others an error ocurred during initialization
226 ShellLibConstructor (
227 IN EFI_HANDLE ImageHandle
,
228 IN EFI_SYSTEM_TABLE
*SystemTable
233 mEfiShellEnvironment2
= NULL
;
234 mEfiShellProtocol
= NULL
;
235 mEfiShellParametersProtocol
= NULL
;
236 mEfiShellInterface
= NULL
;
237 mEfiShellEnvironment2Handle
= NULL
;
240 // verify that auto initialize is not set false
242 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
243 return (EFI_SUCCESS
);
246 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
250 Destructory for the library. free any resources.
255 IN EFI_HANDLE ImageHandle
,
256 IN EFI_SYSTEM_TABLE
*SystemTable
258 if (mEfiShellEnvironment2
!= NULL
) {
259 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
260 &gEfiShellEnvironment2Guid
,
263 mEfiShellEnvironment2
= NULL
;
265 if (mEfiShellInterface
!= NULL
) {
266 gBS
->CloseProtocol(ImageHandle
,
267 &gEfiShellInterfaceGuid
,
270 mEfiShellInterface
= NULL
;
272 if (mEfiShellProtocol
!= NULL
) {
273 gBS
->CloseProtocol(ImageHandle
,
274 &gEfiShellProtocolGuid
,
277 mEfiShellProtocol
= NULL
;
279 if (mEfiShellParametersProtocol
!= NULL
) {
280 gBS
->CloseProtocol(ImageHandle
,
281 &gEfiShellParametersProtocolGuid
,
284 mEfiShellParametersProtocol
= NULL
;
286 mEfiShellEnvironment2Handle
= NULL
;
287 return (EFI_SUCCESS
);
291 This function causes the shell library to initialize itself. If the shell library
292 is already initialized it will de-initialize all the current protocol poitners and
293 re-populate them again.
295 When the library is used with PcdShellLibAutoInitialize set to true this function
296 will return EFI_SUCCESS and perform no actions.
298 This function is intended for internal access for shell commands only.
300 @retval EFI_SUCCESS the initialization was complete sucessfully
308 // if auto initialize is not false then skip
310 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
311 return (EFI_SUCCESS
);
315 // deinit the current stuff
317 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
320 // init the new stuff
322 return (ShellLibConstructorWorker(gImageHandle
, gST
));
326 This function will retrieve the information about the file for the handle
327 specified and store it in allocated pool memory.
329 This function allocates a buffer to store the file's information. It is the
330 caller's responsibility to free the buffer
332 @param FileHandle The file handle of the file for which information is
335 @retval NULL information could not be retrieved.
337 @return the information about the file
342 IN EFI_FILE_HANDLE FileHandle
345 return (FileFunctionMap
.GetFileInfo(FileHandle
));
349 This function will set the information about the file for the opened handle
352 @param FileHandle The file handle of the file for which information
355 @param FileInfo The infotmation to set.
357 @retval EFI_SUCCESS The information was set.
358 @retval EFI_UNSUPPORTED The InformationType is not known.
359 @retval EFI_NO_MEDIA The device has no medium.
360 @retval EFI_DEVICE_ERROR The device reported an error.
361 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
362 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
363 @retval EFI_ACCESS_DENIED The file was opened read only.
364 @retval EFI_VOLUME_FULL The volume is full.
369 IN EFI_FILE_HANDLE FileHandle
,
370 IN EFI_FILE_INFO
*FileInfo
373 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
377 This function will open a file or directory referenced by DevicePath.
379 This function opens a file with the open mode according to the file path. The
380 Attributes is valid only for EFI_FILE_MODE_CREATE.
382 @param FilePath on input the device path to the file. On output
383 the remaining device path.
384 @param DeviceHandle pointer to the system device handle.
385 @param FileHandle pointer to the file handle.
386 @param OpenMode the mode to open the file with.
387 @param Attributes the file's file attributes.
389 @retval EFI_SUCCESS The information was set.
390 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
391 @retval EFI_UNSUPPORTED Could not open the file path.
392 @retval EFI_NOT_FOUND The specified file could not be found on the
393 device or the file system could not be found on
395 @retval EFI_NO_MEDIA The device has no medium.
396 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
397 medium is no longer supported.
398 @retval EFI_DEVICE_ERROR The device reported an error.
399 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
400 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
401 @retval EFI_ACCESS_DENIED The file was opened read only.
402 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
404 @retval EFI_VOLUME_FULL The volume is full.
408 ShellOpenFileByDevicePath(
409 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
410 OUT EFI_HANDLE
*DeviceHandle
,
411 OUT EFI_FILE_HANDLE
*FileHandle
,
418 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
419 EFI_FILE_HANDLE LastHandle
;
422 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
424 ASSERT(FilePath
!= NULL
);
425 ASSERT(FileHandle
!= NULL
);
426 ASSERT(DeviceHandle
!= NULL
);
428 // which shell interface should we use
430 if (mEfiShellProtocol
!= NULL
) {
432 // use UEFI Shell 2.0 method.
434 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
435 if (FileName
== NULL
) {
436 return (EFI_INVALID_PARAMETER
);
438 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
445 // use old shell method.
447 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
450 if (EFI_ERROR (Status
)) {
453 Status
= gBS
->OpenProtocol(*DeviceHandle
,
454 &gEfiSimpleFileSystemProtocolGuid
,
455 (VOID
**)&EfiSimpleFileSystemProtocol
,
458 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
459 if (EFI_ERROR (Status
)) {
462 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
463 if (EFI_ERROR (Status
)) {
469 // go down directories one node at a time.
471 while (!IsDevicePathEnd (*FilePath
)) {
473 // For file system access each node should be a file path component
475 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
476 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
479 return (EFI_INVALID_PARAMETER
);
482 // Open this file path node
484 LastHandle
= *FileHandle
;
488 // Try to test opening an existing file
490 Status
= LastHandle
->Open (
493 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
494 OpenMode
&~EFI_FILE_MODE_CREATE
,
499 // see if the error was that it needs to be created
501 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
502 Status
= LastHandle
->Open (
505 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
511 // Close the last node
513 LastHandle
->Close (LastHandle
);
515 if (EFI_ERROR(Status
)) {
522 *FilePath
= NextDevicePathNode (*FilePath
);
524 return (EFI_SUCCESS
);
528 This function will open a file or directory referenced by filename.
530 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
531 otherwise, the Filehandle is NULL. The Attributes is valid only for
532 EFI_FILE_MODE_CREATE.
534 if FileNAme is NULL then ASSERT()
536 @param FileName pointer to file name
537 @param FileHandle pointer to the file handle.
538 @param OpenMode the mode to open the file with.
539 @param Attributes the file's file attributes.
541 @retval EFI_SUCCESS The information was set.
542 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
543 @retval EFI_UNSUPPORTED Could not open the file path.
544 @retval EFI_NOT_FOUND The specified file could not be found on the
545 device or the file system could not be found
547 @retval EFI_NO_MEDIA The device has no medium.
548 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
549 medium is no longer supported.
550 @retval EFI_DEVICE_ERROR The device reported an error.
551 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
552 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
553 @retval EFI_ACCESS_DENIED The file was opened read only.
554 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
556 @retval EFI_VOLUME_FULL The volume is full.
561 IN CONST CHAR16
*FileName
,
562 OUT EFI_FILE_HANDLE
*FileHandle
,
567 EFI_HANDLE DeviceHandle
;
568 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
570 EFI_FILE_INFO
*FileInfo
;
573 // ASSERT if FileName is NULL
575 ASSERT(FileName
!= NULL
);
577 if (mEfiShellProtocol
!= NULL
) {
579 // Use UEFI Shell 2.0 method
581 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
584 if (!EFI_ERROR(Status
)){
585 FileInfo
= FileHandleGetInfo(*FileHandle
);
586 ASSERT(FileInfo
!= NULL
);
587 FileInfo
->Attribute
= Attributes
;
588 Status
= FileHandleSetInfo(*FileHandle
, FileInfo
);
593 // Using EFI Shell version
594 // this means convert name to path and call that function
595 // since this will use EFI method again that will open it.
597 ASSERT(mEfiShellEnvironment2
!= NULL
);
598 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
599 if (FileDevicePath
!= NULL
) {
600 return (ShellOpenFileByDevicePath(&FilePath
,
606 return (EFI_DEVICE_ERROR
);
609 This function create a directory
611 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
612 otherwise, the Filehandle is NULL. If the directory already existed, this
613 function opens the existing directory.
615 @param DirectoryName pointer to directory name
616 @param FileHandle pointer to the file handle.
618 @retval EFI_SUCCESS The information was set.
619 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
620 @retval EFI_UNSUPPORTED Could not open the file path.
621 @retval EFI_NOT_FOUND The specified file could not be found on the
622 device or the file system could not be found
624 @retval EFI_NO_MEDIA The device has no medium.
625 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
626 medium is no longer supported.
627 @retval EFI_DEVICE_ERROR The device reported an error.
628 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
629 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
630 @retval EFI_ACCESS_DENIED The file was opened read only.
631 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
633 @retval EFI_VOLUME_FULL The volume is full.
634 @sa ShellOpenFileByName
638 ShellCreateDirectory(
639 IN CONST CHAR16
*DirectoryName
,
640 OUT EFI_FILE_HANDLE
*FileHandle
644 // this is a pass thru to the open file function with sepcific open mode and attributes
646 return (ShellOpenFileByName(DirectoryName
,
648 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
654 This function reads information from an opened file.
656 If FileHandle is not a directory, the function reads the requested number of
657 bytes from the file at the file's current position and returns them in Buffer.
658 If the read goes beyond the end of the file, the read length is truncated to the
659 end of the file. The file's current position is increased by the number of bytes
660 returned. If FileHandle is a directory, the function reads the directory entry
661 at the file's current position and returns the entry in Buffer. If the Buffer
662 is not large enough to hold the current directory entry, then
663 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
664 BufferSize is set to be the size of the buffer needed to read the entry. On
665 success, the current position is updated to the next directory entry. If there
666 are no more directory entries, the read returns a zero-length buffer.
667 EFI_FILE_INFO is the structure returned as the directory entry.
669 @param FileHandle the opened file handle
670 @param BufferSize on input the size of buffer in bytes. on return
671 the number of bytes written.
672 @param Buffer the buffer to put read data into.
674 @retval EFI_SUCCESS Data was read.
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_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
685 IN EFI_FILE_HANDLE FileHandle
,
686 IN OUT UINTN
*BufferSize
,
690 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
695 Write data to a file.
697 This function writes the specified number of bytes to the file at the current
698 file position. The current file position is advanced the actual number of bytes
699 written, which is returned in BufferSize. Partial writes only occur when there
700 has been a data error during the write attempt (such as "volume space full").
701 The file is automatically grown to hold the data if required. Direct writes to
702 opened directories are not supported.
704 @param FileHandle The opened file for writing
705 @param BufferSize on input the number of bytes in Buffer. On output
706 the number of bytes written.
707 @param Buffer the buffer containing data to write is stored.
709 @retval EFI_SUCCESS Data was written.
710 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
711 @retval EFI_NO_MEDIA The device has no media.
712 @retval EFI_DEVICE_ERROR The device reported an error.
713 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
714 @retval EFI_WRITE_PROTECTED The device is write-protected.
715 @retval EFI_ACCESS_DENIED The file was open for read only.
716 @retval EFI_VOLUME_FULL The volume is full.
721 IN EFI_FILE_HANDLE FileHandle
,
722 IN OUT UINTN
*BufferSize
,
726 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
730 Close an open file handle.
732 This function closes a specified file handle. All "dirty" cached file data is
733 flushed to the device, and the file is closed. In all cases the handle is
736 @param FileHandle the file handle to close.
738 @retval EFI_SUCCESS the file handle was closed sucessfully.
743 IN EFI_FILE_HANDLE
*FileHandle
746 return (FileFunctionMap
.CloseFile(*FileHandle
));
750 Delete a file and close the handle
752 This function closes and deletes a file. In all cases the file handle is closed.
753 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
754 returned, but the handle is still closed.
756 @param FileHandle the file handle to delete
758 @retval EFI_SUCCESS the file was closed sucessfully
759 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
761 @retval INVALID_PARAMETER One of the parameters has an invalid value.
766 IN EFI_FILE_HANDLE
*FileHandle
769 return (FileFunctionMap
.DeleteFile(*FileHandle
));
773 Set the current position in a file.
775 This function sets the current file position for the handle to the position
776 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
777 absolute positioning is supported, and seeking past the end of the file is
778 allowed (a subsequent write would grow the file). Seeking to position
779 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
780 If FileHandle is a directory, the only position that may be set is zero. This
781 has the effect of starting the read process of the directory entries over.
783 @param FileHandle The file handle on which the position is being set
784 @param Position Byte position from begining of file
786 @retval EFI_SUCCESS Operation completed sucessfully.
787 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
789 @retval INVALID_PARAMETER One of the parameters has an invalid value.
793 ShellSetFilePosition (
794 IN EFI_FILE_HANDLE FileHandle
,
798 return (FileFunctionMap
.SetFilePosition(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
,
823 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
826 Flushes data on a file
828 This function flushes all modified data associated with a file to a device.
830 @param FileHandle The file handle on which to flush data
832 @retval EFI_SUCCESS The data was flushed.
833 @retval EFI_NO_MEDIA The device has no media.
834 @retval EFI_DEVICE_ERROR The device reported an error.
835 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
836 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
837 @retval EFI_ACCESS_DENIED The file was opened for read only.
842 IN EFI_FILE_HANDLE FileHandle
845 return (FileFunctionMap
.FlushFile(FileHandle
));
849 Retrieves the first file from a directory
851 This function opens a directory and gets the first file's info in the
852 directory. Caller can use ShellFindNextFile() to get other files. When
853 complete the caller is responsible for calling FreePool() on Buffer.
855 @param DirHandle The file handle of the directory to search
856 @param Buffer Pointer to buffer for file's information
858 @retval EFI_SUCCESS Found the first file.
859 @retval EFI_NOT_FOUND Cannot find the directory.
860 @retval EFI_NO_MEDIA The device has no media.
861 @retval EFI_DEVICE_ERROR The device reported an error.
862 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
863 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
869 IN EFI_FILE_HANDLE DirHandle
,
870 OUT EFI_FILE_INFO
**Buffer
874 // pass to file handle lib
876 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
879 Retrieves the next file in a directory.
881 To use this function, caller must call the LibFindFirstFile() to get the
882 first file, and then use this function get other files. This function can be
883 called for several times to get each file's information in the directory. If
884 the call of ShellFindNextFile() got the last file in the directory, the next
885 call of this function has no file to get. *NoFile will be set to TRUE and the
886 Buffer memory will be automatically freed.
888 @param DirHandle the file handle of the directory
889 @param Buffer pointer to buffer for file's information
890 @param NoFile pointer to boolean when last file is found
892 @retval EFI_SUCCESS Found the next file, or reached last file
893 @retval EFI_NO_MEDIA The device has no media.
894 @retval EFI_DEVICE_ERROR The device reported an error.
895 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
900 IN EFI_FILE_HANDLE DirHandle
,
901 OUT EFI_FILE_INFO
*Buffer
,
906 // pass to file handle lib
908 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
911 Retrieve the size of a file.
913 if FileHandle is NULL then ASSERT()
914 if Size is NULL then ASSERT()
916 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
919 @param FileHandle file handle from which size is retrieved
920 @param Size pointer to size
922 @retval EFI_SUCCESS operation was completed sucessfully
923 @retval EFI_DEVICE_ERROR cannot access the file
928 IN EFI_FILE_HANDLE FileHandle
,
932 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
935 Retrieves the status of the break execution flag
937 this function is useful to check whether the application is being asked to halt by the shell.
939 @retval TRUE the execution break is enabled
940 @retval FALSE the execution break is not enabled
944 ShellGetExecutionBreakFlag(
949 // Check for UEFI Shell 2.0 protocols
951 if (mEfiShellProtocol
!= NULL
) {
954 // We are using UEFI Shell 2.0; see if the event has been triggered
956 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
963 // using EFI Shell; call the function to check
965 ASSERT(mEfiShellEnvironment2
!= NULL
);
966 return (mEfiShellEnvironment2
->GetExecutionBreak());
969 return the value of an environment variable
971 this function gets the value of the environment variable set by the
972 ShellSetEnvironmentVariable function
974 @param EnvKey The key name of the environment variable.
976 @retval NULL the named environment variable does not exist.
977 @return != NULL pointer to the value of the environment variable
981 ShellGetEnvironmentVariable (
982 IN CONST CHAR16
*EnvKey
986 // Check for UEFI Shell 2.0 protocols
988 if (mEfiShellProtocol
!= NULL
) {
989 return (mEfiShellProtocol
->GetEnv(EnvKey
));
993 // ASSERT that we must have EFI shell
995 ASSERT(mEfiShellEnvironment2
!= NULL
);
1000 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1003 set the value of an environment variable
1005 This function changes the current value of the specified environment variable. If the
1006 environment variable exists and the Value is an empty string, then the environment
1007 variable is deleted. If the environment variable exists and the Value is not an empty
1008 string, then the value of the environment variable is changed. If the environment
1009 variable does not exist and the Value is an empty string, there is no action. If the
1010 environment variable does not exist and the Value is a non-empty string, then the
1011 environment variable is created and assigned the specified value.
1013 This is not supported pre-UEFI Shell 2.0.
1015 @param EnvKey The key name of the environment variable.
1016 @param EnvVal The Value of the environment variable
1017 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1019 @retval EFI_SUCCESS the operation was completed sucessfully
1020 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1024 ShellSetEnvironmentVariable (
1025 IN CONST CHAR16
*EnvKey
,
1026 IN CONST CHAR16
*EnvVal
,
1031 // Check for UEFI Shell 2.0 protocols
1033 if (mEfiShellProtocol
!= NULL
) {
1034 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1038 // This feature does not exist under EFI shell
1040 return (EFI_UNSUPPORTED
);
1043 cause the shell to parse and execute a command line.
1045 This function creates a nested instance of the shell and executes the specified
1046 command (CommandLine) with the specified environment (Environment). Upon return,
1047 the status code returned by the specified command is placed in StatusCode.
1048 If Environment is NULL, then the current environment is used and all changes made
1049 by the commands executed will be reflected in the current environment. If the
1050 Environment is non-NULL, then the changes made will be discarded.
1051 The CommandLine is executed from the current working directory on the current
1054 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1055 Output is only supported for pre-UEFI Shell 2.0
1057 @param ImageHandle Parent image that is starting the operation
1058 @param CommandLine pointer to null terminated command line.
1059 @param Output true to display debug output. false to hide it.
1060 @param EnvironmentVariables optional pointer to array of environment variables
1061 in the form "x=y". if NULL current set is used.
1062 @param Status the status of the run command line.
1064 @retval EFI_SUCCESS the operation completed sucessfully. Status
1065 contains the status code returned.
1066 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1067 @retval EFI_OUT_OF_RESOURCES out of resources
1068 @retval EFI_UNSUPPORTED the operation is not allowed.
1073 IN EFI_HANDLE
*ParentHandle
,
1074 IN CHAR16
*CommandLine OPTIONAL
,
1075 IN BOOLEAN Output OPTIONAL
,
1076 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1077 OUT EFI_STATUS
*Status OPTIONAL
1081 // Check for UEFI Shell 2.0 protocols
1083 if (mEfiShellProtocol
!= NULL
) {
1085 // Call UEFI Shell 2.0 version (not using Output parameter)
1087 return (mEfiShellProtocol
->Execute(ParentHandle
,
1089 EnvironmentVariables
,
1093 // ASSERT that we must have EFI shell
1095 ASSERT(mEfiShellEnvironment2
!= NULL
);
1097 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1098 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1100 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1105 Retreives the current directory path
1107 If the DeviceName is NULL, it returns the current device's current directory
1108 name. If the DeviceName is not NULL, it returns the current directory name
1111 @param DeviceName the name of the drive to get directory on
1113 @retval NULL the directory does not exist
1114 @return != NULL the directory
1118 ShellGetCurrentDir (
1119 IN CHAR16
*DeviceName OPTIONAL
1123 // Check for UEFI Shell 2.0 protocols
1125 if (mEfiShellProtocol
!= NULL
) {
1126 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1129 // ASSERT that we must have EFI shell
1131 ASSERT(mEfiShellEnvironment2
!= NULL
);
1132 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1135 sets (enabled or disabled) the page break mode
1137 when page break mode is enabled the screen will stop scrolling
1138 and wait for operator input before scrolling a subsequent screen.
1140 @param CurrentState TRUE to enable and FALSE to disable
1144 ShellSetPageBreakMode (
1145 IN BOOLEAN CurrentState
1149 // check for enabling
1151 if (CurrentState
!= 0x00) {
1153 // check for UEFI Shell 2.0
1155 if (mEfiShellProtocol
!= NULL
) {
1157 // Enable with UEFI 2.0 Shell
1159 mEfiShellProtocol
->EnablePageBreak();
1163 // ASSERT that must have EFI Shell
1165 ASSERT(mEfiShellEnvironment2
!= NULL
);
1167 // Enable with EFI Shell
1169 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1174 // check for UEFI Shell 2.0
1176 if (mEfiShellProtocol
!= NULL
) {
1178 // Disable with UEFI 2.0 Shell
1180 mEfiShellProtocol
->DisablePageBreak();
1184 // ASSERT that must have EFI Shell
1186 ASSERT(mEfiShellEnvironment2
!= NULL
);
1188 // Disable with EFI Shell
1190 mEfiShellEnvironment2
->DisablePageBreak ();
1197 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1198 /// This allows for the struct to be populated.
1205 EFI_FILE_HANDLE Handle
;
1206 EFI_FILE_INFO
*Info
;
1207 } EFI_SHELL_FILE_INFO_NO_CONST
;
1210 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1212 if OldStyleFileList is NULL then ASSERT()
1214 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1215 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1216 the ShellCloseFileMetaArg function.
1218 @param[in] FileList the EFI shell list type
1219 @param[in,out] ListHead the list to add to
1221 @retval the resultant head of the double linked new format list;
1225 InternalShellConvertFileListType (
1226 IN LIST_ENTRY
*FileList
,
1227 IN OUT LIST_ENTRY
*ListHead
1229 SHELL_FILE_ARG
*OldInfo
;
1231 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1236 ASSERT(FileList
!= NULL
);
1237 ASSERT(ListHead
!= NULL
);
1240 // enumerate through each member of the old list and copy
1242 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1243 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1246 // make sure the old list was valid
1248 ASSERT(OldInfo
!= NULL
);
1249 ASSERT(OldInfo
->Info
!= NULL
);
1250 ASSERT(OldInfo
->FullName
!= NULL
);
1251 ASSERT(OldInfo
->FileName
!= NULL
);
1254 // allocate a new EFI_SHELL_FILE_INFO object
1256 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1259 // copy the simple items
1261 NewInfo
->Handle
= OldInfo
->Handle
;
1262 NewInfo
->Status
= OldInfo
->Status
;
1264 // old shell checks for 0 not NULL
1265 OldInfo
->Handle
= 0;
1268 // allocate new space to copy strings and structure
1270 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1271 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1272 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1275 // make sure all the memory allocations were sucessful
1277 ASSERT(NewInfo
->FullName
!= NULL
);
1278 ASSERT(NewInfo
->FileName
!= NULL
);
1279 ASSERT(NewInfo
->Info
!= NULL
);
1282 // Copt the strings and structure
1284 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1285 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1286 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1289 // add that to the list
1291 InsertTailList(ListHead
, &NewInfo
->Link
);
1296 Opens a group of files based on a path.
1298 This function uses the Arg to open all the matching files. Each matched
1299 file has a SHELL_FILE_ARG structure to record the file information. These
1300 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1301 structures from ListHead to access each file. This function supports wildcards
1302 and will process '?' and '*' as such. the list must be freed with a call to
1303 ShellCloseFileMetaArg().
1305 If you are NOT appending to an existing list *ListHead must be NULL. If
1306 *ListHead is NULL then it must be callee freed.
1308 @param Arg pointer to path string
1309 @param OpenMode mode to open files with
1310 @param ListHead head of linked list of results
1312 @retval EFI_SUCCESS the operation was sucessful and the list head
1313 contains the list of opened files
1314 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1315 *ListHead is set to NULL.
1316 @return != EFI_SUCCESS the operation failed
1318 @sa InternalShellConvertFileListType
1322 ShellOpenFileMetaArg (
1325 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1329 LIST_ENTRY mOldStyleFileList
;
1332 // ASSERT that Arg and ListHead are not NULL
1334 ASSERT(Arg
!= NULL
);
1335 ASSERT(ListHead
!= NULL
);
1338 // Check for UEFI Shell 2.0 protocols
1340 if (mEfiShellProtocol
!= NULL
) {
1341 if (*ListHead
== NULL
) {
1342 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1343 if (*ListHead
== NULL
) {
1344 return (EFI_OUT_OF_RESOURCES
);
1346 InitializeListHead(&((*ListHead
)->Link
));
1348 return (mEfiShellProtocol
->OpenFileList(Arg
,
1354 // ASSERT that we must have EFI shell
1356 ASSERT(mEfiShellEnvironment2
!= NULL
);
1359 // make sure the list head is initialized
1361 InitializeListHead(&mOldStyleFileList
);
1364 // Get the EFI Shell list of files
1366 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1367 if (EFI_ERROR(Status
)) {
1372 if (*ListHead
== NULL
) {
1373 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1374 if (*ListHead
== NULL
) {
1375 return (EFI_OUT_OF_RESOURCES
);
1380 // Convert that to equivalent of UEFI Shell 2.0 structure
1382 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1385 // Free the EFI Shell version that was converted.
1387 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1392 Free the linked list returned from ShellOpenFileMetaArg
1394 if ListHead is NULL then ASSERT()
1396 @param ListHead the pointer to free
1398 @retval EFI_SUCCESS the operation was sucessful
1402 ShellCloseFileMetaArg (
1403 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1409 // ASSERT that ListHead is not NULL
1411 ASSERT(ListHead
!= NULL
);
1414 // Check for UEFI Shell 2.0 protocols
1416 if (mEfiShellProtocol
!= NULL
) {
1417 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1420 // Since this is EFI Shell version we need to free our internally made copy
1423 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1424 ; IsListEmpty(&(*ListHead
)->Link
) == FALSE
1425 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1426 RemoveEntryList(Node
);
1427 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1428 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1429 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1430 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1431 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1442 UINTN OriginalPosition
;
1443 } SHELL_PARAM_PACKAGE
;
1446 Checks the list of valid arguments and returns TRUE if the item was found. If the
1447 return value is TRUE then the type parameter is set also.
1449 if CheckList is NULL then ASSERT();
1450 if Name is NULL then ASSERT();
1451 if Type is NULL then ASSERT();
1453 @param Type pointer to type of parameter if it was found
1454 @param Name pointer to Name of parameter found
1455 @param CheckList List to check against
1457 @retval TRUE the Parameter was found. Type is valid.
1458 @retval FALSE the Parameter was not found. Type is not valid.
1462 InternalIsOnCheckList (
1463 IN CONST CHAR16
*Name
,
1464 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1468 SHELL_PARAM_ITEM
*TempListItem
;
1471 // ASSERT that all 3 pointer parameters aren't NULL
1473 ASSERT(CheckList
!= NULL
);
1474 ASSERT(Type
!= NULL
);
1475 ASSERT(Name
!= NULL
);
1478 // question mark and page break mode are always supported
1480 if ((StrCmp(Name
, L
"-?") == 0) ||
1481 (StrCmp(Name
, L
"-b") == 0)
1487 // Enumerate through the list
1489 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1491 // If the Type is TypeStart only check the first characters of the passed in param
1492 // If it matches set the type and return TRUE
1494 if (TempListItem
->Type
== TypeStart
&& StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1495 *Type
= TempListItem
->Type
;
1497 } else if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1498 *Type
= TempListItem
->Type
;
1505 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1507 @param Name pointer to Name of parameter found
1509 @retval TRUE the Parameter is a flag.
1510 @retval FALSE the Parameter not a flag
1515 IN CONST CHAR16
*Name
1519 // ASSERT that Name isn't NULL
1521 ASSERT(Name
!= NULL
);
1524 // If the Name has a / or - as the first character return TRUE
1526 if ((Name
[0] == L
'/') ||
1527 (Name
[0] == L
'-') ||
1536 Checks the command line arguments passed against the list of valid ones.
1538 If no initialization is required, then return RETURN_SUCCESS.
1540 @param CheckList pointer to list of parameters to check
1541 @param CheckPackage pointer to pointer to list checked values
1542 @param ProblemParam optional pointer to pointer to unicode string for
1543 the paramater that caused failure. If used then the
1544 caller is responsible for freeing the memory.
1545 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1546 @param Argc Count of parameters in Argv
1547 @param Argv pointer to array of parameters
1549 @retval EFI_SUCCESS The operation completed sucessfully.
1550 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1551 @retval EFI_INVALID_PARAMETER A parameter was invalid
1552 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1553 duplicated. the duplicated command line argument
1554 was returned in ProblemParam if provided.
1555 @retval EFI_NOT_FOUND a argument required a value that was missing.
1556 the invalid command line argument was returned in
1557 ProblemParam if provided.
1561 InternalCommandLineParse (
1562 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1563 OUT LIST_ENTRY
**CheckPackage
,
1564 OUT CHAR16
**ProblemParam OPTIONAL
,
1565 IN BOOLEAN AutoPageBreak
,
1566 IN CONST CHAR16
**Argv
,
1572 ParamType CurrentItemType
;
1573 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1574 BOOLEAN GetItemValue
;
1576 CurrentItemPackage
= NULL
;
1581 ASSERT(CheckList
!= NULL
);
1582 ASSERT(Argv
!= NULL
);
1585 GetItemValue
= FALSE
;
1588 // If there is only 1 item we dont need to do anything
1591 *CheckPackage
= NULL
;
1592 return (EFI_SUCCESS
);
1596 // initialize the linked list
1598 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1599 InitializeListHead(*CheckPackage
);
1602 // loop through each of the arguments
1604 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1605 if (Argv
[LoopCounter
] == NULL
) {
1607 // do nothing for NULL argv
1609 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) == TRUE
) {
1613 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1614 ASSERT(CurrentItemPackage
!= NULL
);
1615 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1616 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1617 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1618 CurrentItemPackage
->Type
= CurrentItemType
;
1619 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1620 CurrentItemPackage
->Value
= NULL
;
1623 // Does this flag require a value
1625 if (CurrentItemPackage
->Type
== TypeValue
) {
1627 // trigger the next loop to populate the value of this item
1629 GetItemValue
= TRUE
;
1632 // this item has no value expected; we are done
1634 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1636 } else if (GetItemValue
== TRUE
&& InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1637 ASSERT(CurrentItemPackage
!= NULL
);
1639 // get the item VALUE for the previous flag
1641 GetItemValue
= FALSE
;
1642 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1643 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1644 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1645 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1646 } else if (InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1648 // add this one as a non-flag
1650 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1651 ASSERT(CurrentItemPackage
!= NULL
);
1652 CurrentItemPackage
->Name
= NULL
;
1653 CurrentItemPackage
->Type
= TypePosition
;
1654 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1655 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1656 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1657 CurrentItemPackage
->OriginalPosition
= Count
++;
1658 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1659 } else if (ProblemParam
) {
1661 // this was a non-recognised flag... error!
1663 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1664 ASSERT(*ProblemParam
!= NULL
);
1665 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1666 ShellCommandLineFreeVarList(*CheckPackage
);
1667 *CheckPackage
= NULL
;
1668 return (EFI_VOLUME_CORRUPTED
);
1670 ShellCommandLineFreeVarList(*CheckPackage
);
1671 *CheckPackage
= NULL
;
1672 return (EFI_VOLUME_CORRUPTED
);
1676 // support for AutoPageBreak
1678 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1679 ShellSetPageBreakMode(TRUE
);
1681 return (EFI_SUCCESS
);
1685 Checks the command line arguments passed against the list of valid ones.
1686 Optionally removes NULL values first.
1688 If no initialization is required, then return RETURN_SUCCESS.
1690 @param CheckList pointer to list of parameters to check
1691 @param CheckPackage pointer to pointer to list checked values
1692 @param ProblemParam optional pointer to pointer to unicode string for
1693 the paramater that caused failure.
1694 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1696 @retval EFI_SUCCESS The operation completed sucessfully.
1697 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1698 @retval EFI_INVALID_PARAMETER A parameter was invalid
1699 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1700 duplicated. the duplicated command line argument
1701 was returned in ProblemParam if provided.
1702 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1703 of the command line arguments was returned in
1704 ProblemParam if provided.
1705 @retval EFI_NOT_FOUND a argument required a value that was missing.
1706 the invalid command line argument was returned in
1707 ProblemParam if provided.
1711 ShellCommandLineParse (
1712 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1713 OUT LIST_ENTRY
**CheckPackage
,
1714 OUT CHAR16
**ProblemParam OPTIONAL
,
1715 IN BOOLEAN AutoPageBreak
1719 // ASSERT that CheckList and CheckPackage aren't NULL
1721 ASSERT(CheckList
!= NULL
);
1722 ASSERT(CheckPackage
!= NULL
);
1725 // Check for UEFI Shell 2.0 protocols
1727 if (mEfiShellParametersProtocol
!= NULL
) {
1728 return (InternalCommandLineParse(CheckList
,
1732 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
1733 mEfiShellParametersProtocol
->Argc
));
1737 // ASSERT That EFI Shell is not required
1739 ASSERT (mEfiShellInterface
!= NULL
);
1740 return (InternalCommandLineParse(CheckList
,
1744 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
1745 mEfiShellInterface
->Argc
));
1749 Frees shell variable list that was returned from ShellCommandLineParse.
1751 This function will free all the memory that was used for the CheckPackage
1752 list of postprocessed shell arguments.
1754 this function has no return value.
1756 if CheckPackage is NULL, then return
1758 @param CheckPackage the list to de-allocate
1762 ShellCommandLineFreeVarList (
1763 IN LIST_ENTRY
*CheckPackage
1769 // check for CheckPackage == NULL
1771 if (CheckPackage
== NULL
) {
1776 // for each node in the list
1778 for ( Node
= GetFirstNode(CheckPackage
)
1779 ; Node
!= CheckPackage
1780 ; Node
= GetFirstNode(CheckPackage
)
1783 // Remove it from the list
1785 RemoveEntryList(Node
);
1788 // if it has a name free the name
1790 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1791 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1795 // if it has a value free the value
1797 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
1798 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1802 // free the node structure
1804 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
1807 // free the list head node
1809 FreePool(CheckPackage
);
1812 Checks for presence of a flag parameter
1814 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1816 if CheckPackage is NULL then return FALSE.
1817 if KeyString is NULL then ASSERT()
1819 @param CheckPackage The package of parsed command line arguments
1820 @param KeyString the Key of the command line argument to check for
1822 @retval TRUE the flag is on the command line
1823 @retval FALSE the flag is not on the command line
1827 ShellCommandLineGetFlag (
1828 IN CONST LIST_ENTRY
*CheckPackage
,
1829 IN CHAR16
*KeyString
1835 // ASSERT that both CheckPackage and KeyString aren't NULL
1837 ASSERT(KeyString
!= NULL
);
1840 // return FALSE for no package
1842 if (CheckPackage
== NULL
) {
1847 // enumerate through the list of parametrs
1849 for ( Node
= GetFirstNode(CheckPackage
)
1850 ; !IsNull (CheckPackage
, Node
)
1851 ; Node
= GetNextNode(CheckPackage
, Node
)
1854 // If the Name matches, return TRUE (and there may be NULL name)
1856 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1858 // If Type is TypeStart then only compare the begining of the strings
1860 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
1861 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
1864 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1872 returns value from command line argument
1874 value parameters are in the form of "-<Key> value" or "/<Key> value"
1876 if CheckPackage is NULL, then return NULL;
1878 @param CheckPackage The package of parsed command line arguments
1879 @param KeyString the Key of the command line argument to check for
1881 @retval NULL the flag is not on the command line
1882 @return !=NULL pointer to unicode string of the value
1886 ShellCommandLineGetValue (
1887 IN CONST LIST_ENTRY
*CheckPackage
,
1888 IN CHAR16
*KeyString
1894 // check for CheckPackage == NULL
1896 if (CheckPackage
== NULL
) {
1901 // enumerate through the list of parametrs
1903 for ( Node
= GetFirstNode(CheckPackage
)
1904 ; !IsNull (CheckPackage
, Node
)
1905 ; Node
= GetNextNode(CheckPackage
, Node
)
1908 // If the Name matches, return the value (name can be NULL)
1910 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1912 // If Type is TypeStart then only compare the begining of the strings
1914 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
1915 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
1918 // return the string part after the flag
1920 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
1921 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1925 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1932 returns raw value from command line argument
1934 raw value parameters are in the form of "value" in a specific position in the list
1936 if CheckPackage is NULL, then return NULL;
1938 @param CheckPackage The package of parsed command line arguments
1939 @param Position the position of the value
1941 @retval NULL the flag is not on the command line
1942 @return !=NULL pointer to unicode string of the value
1946 ShellCommandLineGetRawValue (
1947 IN CONST LIST_ENTRY
*CheckPackage
,
1954 // check for CheckPackage == NULL
1956 if (CheckPackage
== NULL
) {
1961 // enumerate through the list of parametrs
1963 for ( Node
= GetFirstNode(CheckPackage
)
1964 ; !IsNull (CheckPackage
, Node
)
1965 ; Node
= GetNextNode(CheckPackage
, Node
)
1968 // If the position matches, return the value
1970 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
1971 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1977 This is a find and replace function. it will return the NewString as a copy of
1978 SourceString with each instance of FindTarget replaced with ReplaceWith.
1980 If the string would grow bigger than NewSize it will halt and return error.
1982 @param[in] SourceString String with source buffer
1983 @param[in,out] NewString String with resultant buffer
1984 @param[in] NewSize Size in bytes of NewString
1985 @param[in] FindTarget String to look for
1986 @param[in[ ReplaceWith String to replace FindTarget with
1988 @retval EFI_INVALID_PARAMETER SourceString was NULL
1989 @retval EFI_INVALID_PARAMETER NewString was NULL
1990 @retval EFI_INVALID_PARAMETER FindTarget was NULL
1991 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
1992 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
1993 @retval EFI_INVALID_PARAMETER SourceString had length < 1
1994 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
1995 the new string (truncation occurred)
1996 @retval EFI_SUCCESS the string was sucessfully copied with replacement
2002 IN CHAR16 CONST
*SourceString
,
2003 IN CHAR16
*NewString
,
2005 IN CONST CHAR16
*FindTarget
,
2006 IN CONST CHAR16
*ReplaceWith
2008 if ( (SourceString
== NULL
)
2009 || (NewString
== NULL
)
2010 || (FindTarget
== NULL
)
2011 || (ReplaceWith
== NULL
)
2012 || (StrLen(FindTarget
) < 1)
2013 || (StrLen(SourceString
) < 1)
2015 return (EFI_INVALID_PARAMETER
);
2017 NewString
= SetMem16(NewString
, NewSize
, L
'\0');
2018 while (*SourceString
!= L
'\0') {
2019 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0) {
2020 SourceString
+= StrLen(FindTarget
);
2021 if ((StrSize(NewString
) + (StrLen(ReplaceWith
)*sizeof(CHAR16
))) > NewSize
) {
2022 return (EFI_BUFFER_TOO_SMALL
);
2024 StrCat(NewString
, ReplaceWith
);
2026 if (StrSize(NewString
) + sizeof(CHAR16
) > NewSize
) {
2027 return (EFI_BUFFER_TOO_SMALL
);
2029 StrnCat(NewString
, SourceString
, 1);
2033 return (EFI_SUCCESS
);
2037 Print at a specific location on the screen.
2039 This function will move the cursor to a given screen location and print the specified string
2041 If -1 is specified for either the Row or Col the current screen location for BOTH
2044 if either Row or Col is out of range for the current console, then ASSERT
2045 if Format is NULL, then ASSERT
2047 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2048 the following additional flags:
2049 %N - Set output attribute to normal
2050 %H - Set output attribute to highlight
2051 %E - Set output attribute to error
2052 %B - Set output attribute to blue color
2053 %V - Set output attribute to green color
2055 Note: The background color is controlled by the shell command cls.
2057 @param[in] Row the row to print at
2058 @param[in] Col the column to print at
2059 @param[in] Format the format string
2061 @return the number of characters printed to the screen
2067 IN INT32 Col OPTIONAL
,
2068 IN INT32 Row OPTIONAL
,
2069 IN CONST CHAR16
*Format
,
2074 CHAR16
*PostReplaceFormat
;
2075 CHAR16
*PostReplaceFormat2
;
2079 UINTN NormalAttribute
;
2080 CHAR16
*ResumeLocation
;
2081 CHAR16
*FormatWalker
;
2083 VA_START (Marker
, Format
);
2085 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
2086 PostReplaceFormat
= AllocateZeroPool (BufferSize
);
2087 ASSERT (PostReplaceFormat
!= NULL
);
2088 PostReplaceFormat2
= AllocateZeroPool (BufferSize
);
2089 ASSERT (PostReplaceFormat2
!= NULL
);
2092 // Back and forth each time fixing up 1 of our flags...
2094 Status
= CopyReplace(Format
, PostReplaceFormat
, BufferSize
, L
"%N", L
"%%N");
2095 ASSERT_EFI_ERROR(Status
);
2096 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%E", L
"%%E");
2097 ASSERT_EFI_ERROR(Status
);
2098 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%H", L
"%%H");
2099 ASSERT_EFI_ERROR(Status
);
2100 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%B", L
"%%B");
2101 ASSERT_EFI_ERROR(Status
);
2102 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%V", L
"%%V");
2103 ASSERT_EFI_ERROR(Status
);
2106 // Use the last buffer from replacing to print from...
2108 Return
= UnicodeVSPrint (PostReplaceFormat2
, BufferSize
, PostReplaceFormat
, Marker
);
2110 FreePool(PostReplaceFormat
);
2112 if (Col
!= -1 && Row
!= -1) {
2113 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2114 ASSERT_EFI_ERROR(Status
);
2117 NormalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2118 FormatWalker
= PostReplaceFormat2
;
2119 while (*FormatWalker
!= L
'\0') {
2121 // Find the next attribute change request
2123 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2124 if (ResumeLocation
!= NULL
) {
2125 *ResumeLocation
= L
'\0';
2128 // print the current FormatWalker string
2130 Status
= gST
->ConOut
->OutputString(gST
->ConOut
, FormatWalker
);
2131 ASSERT_EFI_ERROR(Status
);
2133 // update the attribute
2135 if (ResumeLocation
!= NULL
) {
2136 switch (*(ResumeLocation
+1)) {
2138 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2141 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2144 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2147 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2150 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2158 // reset to normal now...
2160 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2165 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2167 FormatWalker
= ResumeLocation
+ 2;
2170 FreePool(PostReplaceFormat2
);