2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLib.h"
17 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
18 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
21 // This is not static since it's extern in the .h file
23 SHELL_PARAM_ITEM EmptyParamList
[] = {
28 // Static file globals for the shell library
30 STATIC EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
31 STATIC EFI_SHELL_INTERFACE
*mEfiShellInterface
;
32 STATIC EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
33 STATIC EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
34 STATIC EFI_HANDLE mEfiShellEnvironment2Handle
;
35 STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
36 STATIC UINTN mTotalParameterCount
;
37 STATIC CHAR16
*mPostReplaceFormat
;
38 STATIC CHAR16
*mPostReplaceFormat2
;
41 Check if a Unicode character is a hexadecimal character.
43 This internal function checks if a Unicode character is a
44 decimal character. The valid hexadecimal character is
45 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
48 @param Char The character to check against.
50 @retval TRUE If the Char is a hexadecmial character.
51 @retval FALSE If the Char is not a hexadecmial character.
56 ShellIsHexaDecimalDigitCharacter (
59 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
63 helper function to find ShellEnvironment2 for constructor
68 IN EFI_HANDLE ImageHandle
77 Status
= gBS
->OpenProtocol(ImageHandle
,
78 &gEfiShellEnvironment2Guid
,
79 (VOID
**)&mEfiShellEnvironment2
,
82 EFI_OPEN_PROTOCOL_GET_PROTOCOL
85 // look for the mEfiShellEnvironment2 protocol at a higher level
87 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
)){
89 // figure out how big of a buffer we need.
91 Status
= gBS
->LocateHandle (ByProtocol
,
92 &gEfiShellEnvironment2Guid
,
93 NULL
, // ignored for ByProtocol
98 // maybe it's not there???
100 if (Status
== EFI_BUFFER_TOO_SMALL
) {
101 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
102 ASSERT(Buffer
!= NULL
);
103 Status
= gBS
->LocateHandle (ByProtocol
,
104 &gEfiShellEnvironment2Guid
,
105 NULL
, // ignored for ByProtocol
110 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
112 // now parse the list of returned handles
114 Status
= EFI_NOT_FOUND
;
115 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
116 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
117 &gEfiShellEnvironment2Guid
,
118 (VOID
**)&mEfiShellEnvironment2
,
121 EFI_OPEN_PROTOCOL_GET_PROTOCOL
123 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
) {
124 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
125 Status
= EFI_SUCCESS
;
131 if (Buffer
!= NULL
) {
139 ShellLibConstructorWorker (
140 IN EFI_HANDLE ImageHandle
,
141 IN EFI_SYSTEM_TABLE
*SystemTable
145 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
146 ASSERT (mPostReplaceFormat
!= NULL
);
147 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
148 ASSERT (mPostReplaceFormat2
!= NULL
);
151 // Set the parameter count to an invalid number
153 mTotalParameterCount
= (UINTN
)(-1);
156 // UEFI 2.0 shell interfaces (used preferentially)
158 Status
= gBS
->OpenProtocol(ImageHandle
,
159 &gEfiShellProtocolGuid
,
160 (VOID
**)&mEfiShellProtocol
,
163 EFI_OPEN_PROTOCOL_GET_PROTOCOL
165 if (EFI_ERROR(Status
)) {
166 mEfiShellProtocol
= NULL
;
168 Status
= gBS
->OpenProtocol(ImageHandle
,
169 &gEfiShellParametersProtocolGuid
,
170 (VOID
**)&mEfiShellParametersProtocol
,
173 EFI_OPEN_PROTOCOL_GET_PROTOCOL
175 if (EFI_ERROR(Status
)) {
176 mEfiShellParametersProtocol
= NULL
;
179 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
181 // Moved to seperate function due to complexity
183 Status
= ShellFindSE2(ImageHandle
);
185 if (EFI_ERROR(Status
)) {
186 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
187 mEfiShellEnvironment2
= NULL
;
189 Status
= gBS
->OpenProtocol(ImageHandle
,
190 &gEfiShellInterfaceGuid
,
191 (VOID
**)&mEfiShellInterface
,
194 EFI_OPEN_PROTOCOL_GET_PROTOCOL
196 if (EFI_ERROR(Status
)) {
197 mEfiShellInterface
= NULL
;
202 // only success getting 2 of either the old or new, but no 1/2 and 1/2
204 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
205 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
206 if (mEfiShellProtocol
!= NULL
) {
207 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
208 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
209 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
210 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
211 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
212 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
213 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
214 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
215 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
216 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
218 FileFunctionMap
.GetFileInfo
= FileHandleGetInfo
;
219 FileFunctionMap
.SetFileInfo
= FileHandleSetInfo
;
220 FileFunctionMap
.ReadFile
= FileHandleRead
;
221 FileFunctionMap
.WriteFile
= FileHandleWrite
;
222 FileFunctionMap
.CloseFile
= FileHandleClose
;
223 FileFunctionMap
.DeleteFile
= FileHandleDelete
;
224 FileFunctionMap
.GetFilePosition
= FileHandleGetPosition
;
225 FileFunctionMap
.SetFilePosition
= FileHandleSetPosition
;
226 FileFunctionMap
.FlushFile
= FileHandleFlush
;
227 FileFunctionMap
.GetFileSize
= FileHandleGetSize
;
229 return (EFI_SUCCESS
);
231 return (EFI_NOT_FOUND
);
234 Constructor for the Shell library.
236 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
238 @param ImageHandle the image handle of the process
239 @param SystemTable the EFI System Table pointer
241 @retval EFI_SUCCESS the initialization was complete sucessfully
242 @return others an error ocurred during initialization
246 ShellLibConstructor (
247 IN EFI_HANDLE ImageHandle
,
248 IN EFI_SYSTEM_TABLE
*SystemTable
251 mEfiShellEnvironment2
= NULL
;
252 mEfiShellProtocol
= NULL
;
253 mEfiShellParametersProtocol
= NULL
;
254 mEfiShellInterface
= NULL
;
255 mEfiShellEnvironment2Handle
= NULL
;
256 mPostReplaceFormat
= NULL
;
257 mPostReplaceFormat2
= NULL
;
260 // verify that auto initialize is not set false
262 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
263 return (EFI_SUCCESS
);
266 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
270 Destructory for the library. free any resources.
275 IN EFI_HANDLE ImageHandle
,
276 IN EFI_SYSTEM_TABLE
*SystemTable
278 if (mEfiShellEnvironment2
!= NULL
) {
279 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
280 &gEfiShellEnvironment2Guid
,
283 mEfiShellEnvironment2
= NULL
;
285 if (mEfiShellInterface
!= NULL
) {
286 gBS
->CloseProtocol(ImageHandle
,
287 &gEfiShellInterfaceGuid
,
290 mEfiShellInterface
= NULL
;
292 if (mEfiShellProtocol
!= NULL
) {
293 gBS
->CloseProtocol(ImageHandle
,
294 &gEfiShellProtocolGuid
,
297 mEfiShellProtocol
= NULL
;
299 if (mEfiShellParametersProtocol
!= NULL
) {
300 gBS
->CloseProtocol(ImageHandle
,
301 &gEfiShellParametersProtocolGuid
,
304 mEfiShellParametersProtocol
= NULL
;
306 mEfiShellEnvironment2Handle
= NULL
;
308 if (mPostReplaceFormat
!= NULL
) {
309 FreePool(mPostReplaceFormat
);
311 if (mPostReplaceFormat2
!= NULL
) {
312 FreePool(mPostReplaceFormat2
);
314 mPostReplaceFormat
= NULL
;
315 mPostReplaceFormat2
= NULL
;
317 return (EFI_SUCCESS
);
321 This function causes the shell library to initialize itself. If the shell library
322 is already initialized it will de-initialize all the current protocol poitners and
323 re-populate them again.
325 When the library is used with PcdShellLibAutoInitialize set to true this function
326 will return EFI_SUCCESS and perform no actions.
328 This function is intended for internal access for shell commands only.
330 @retval EFI_SUCCESS the initialization was complete sucessfully
338 // if auto initialize is not false then skip
340 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
341 return (EFI_SUCCESS
);
345 // deinit the current stuff
347 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
350 // init the new stuff
352 return (ShellLibConstructorWorker(gImageHandle
, gST
));
356 This function will retrieve the information about the file for the handle
357 specified and store it in allocated pool memory.
359 This function allocates a buffer to store the file's information. It is the
360 caller's responsibility to free the buffer
362 @param FileHandle The file handle of the file for which information is
365 @retval NULL information could not be retrieved.
367 @return the information about the file
372 IN EFI_FILE_HANDLE FileHandle
374 return (FileFunctionMap
.GetFileInfo(FileHandle
));
378 This function will set the information about the file for the opened handle
381 @param FileHandle The file handle of the file for which information
384 @param FileInfo The infotmation to set.
386 @retval EFI_SUCCESS The information was set.
387 @retval EFI_UNSUPPORTED The InformationType is not known.
388 @retval EFI_NO_MEDIA The device has no medium.
389 @retval EFI_DEVICE_ERROR The device reported an error.
390 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
391 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
392 @retval EFI_ACCESS_DENIED The file was opened read only.
393 @retval EFI_VOLUME_FULL The volume is full.
398 IN EFI_FILE_HANDLE FileHandle
,
399 IN EFI_FILE_INFO
*FileInfo
401 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
405 This function will open a file or directory referenced by DevicePath.
407 This function opens a file with the open mode according to the file path. The
408 Attributes is valid only for EFI_FILE_MODE_CREATE.
410 @param FilePath on input the device path to the file. On output
411 the remaining device path.
412 @param DeviceHandle pointer to the system device handle.
413 @param FileHandle pointer to the file handle.
414 @param OpenMode the mode to open the file with.
415 @param Attributes the file's file attributes.
417 @retval EFI_SUCCESS The information was set.
418 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
419 @retval EFI_UNSUPPORTED Could not open the file path.
420 @retval EFI_NOT_FOUND The specified file could not be found on the
421 device or the file system could not be found on
423 @retval EFI_NO_MEDIA The device has no medium.
424 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
425 medium is no longer supported.
426 @retval EFI_DEVICE_ERROR The device reported an error.
427 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
428 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
429 @retval EFI_ACCESS_DENIED The file was opened read only.
430 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
432 @retval EFI_VOLUME_FULL The volume is full.
436 ShellOpenFileByDevicePath(
437 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
438 OUT EFI_HANDLE
*DeviceHandle
,
439 OUT EFI_FILE_HANDLE
*FileHandle
,
445 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
446 EFI_FILE_HANDLE LastHandle
;
449 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
451 ASSERT(FilePath
!= NULL
);
452 ASSERT(FileHandle
!= NULL
);
453 ASSERT(DeviceHandle
!= NULL
);
455 // which shell interface should we use
457 if (mEfiShellProtocol
!= NULL
) {
459 // use UEFI Shell 2.0 method.
461 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
462 if (FileName
== NULL
) {
463 return (EFI_INVALID_PARAMETER
);
465 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
472 // use old shell method.
474 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
477 if (EFI_ERROR (Status
)) {
480 Status
= gBS
->OpenProtocol(*DeviceHandle
,
481 &gEfiSimpleFileSystemProtocolGuid
,
482 (VOID
**)&EfiSimpleFileSystemProtocol
,
485 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
486 if (EFI_ERROR (Status
)) {
489 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
490 if (EFI_ERROR (Status
)) {
496 // go down directories one node at a time.
498 while (!IsDevicePathEnd (*FilePath
)) {
500 // For file system access each node should be a file path component
502 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
503 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
506 return (EFI_INVALID_PARAMETER
);
509 // Open this file path node
511 LastHandle
= *FileHandle
;
515 // Try to test opening an existing file
517 Status
= LastHandle
->Open (
520 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
521 OpenMode
&~EFI_FILE_MODE_CREATE
,
526 // see if the error was that it needs to be created
528 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
529 Status
= LastHandle
->Open (
532 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
538 // Close the last node
540 LastHandle
->Close (LastHandle
);
542 if (EFI_ERROR(Status
)) {
549 *FilePath
= NextDevicePathNode (*FilePath
);
551 return (EFI_SUCCESS
);
555 This function will open a file or directory referenced by filename.
557 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
558 otherwise, the Filehandle is NULL. The Attributes is valid only for
559 EFI_FILE_MODE_CREATE.
561 if FileNAme is NULL then ASSERT()
563 @param FileName pointer to file name
564 @param FileHandle pointer to the file handle.
565 @param OpenMode the mode to open the file with.
566 @param Attributes the file's file attributes.
568 @retval EFI_SUCCESS The information was set.
569 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
570 @retval EFI_UNSUPPORTED Could not open the file path.
571 @retval EFI_NOT_FOUND The specified file could not be found on the
572 device or the file system could not be found
574 @retval EFI_NO_MEDIA The device has no medium.
575 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
576 medium is no longer supported.
577 @retval EFI_DEVICE_ERROR The device reported an error.
578 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
579 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
580 @retval EFI_ACCESS_DENIED The file was opened read only.
581 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
583 @retval EFI_VOLUME_FULL The volume is full.
588 IN CONST CHAR16
*FileName
,
589 OUT EFI_FILE_HANDLE
*FileHandle
,
593 EFI_HANDLE DeviceHandle
;
594 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
596 EFI_FILE_INFO
*FileInfo
;
599 // ASSERT if FileName is NULL
601 ASSERT(FileName
!= NULL
);
603 if (mEfiShellProtocol
!= NULL
) {
605 // Use UEFI Shell 2.0 method
607 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
610 if (!EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
611 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
612 ASSERT(FileInfo
!= NULL
);
613 FileInfo
->Attribute
= Attributes
;
614 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
620 // Using EFI Shell version
621 // this means convert name to path and call that function
622 // since this will use EFI method again that will open it.
624 ASSERT(mEfiShellEnvironment2
!= NULL
);
625 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
626 if (FileDevicePath
!= NULL
) {
627 return (ShellOpenFileByDevicePath(&FilePath
,
633 return (EFI_DEVICE_ERROR
);
636 This function create a directory
638 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
639 otherwise, the Filehandle is NULL. If the directory already existed, this
640 function opens the existing directory.
642 @param DirectoryName pointer to directory name
643 @param FileHandle pointer to the file handle.
645 @retval EFI_SUCCESS The information was set.
646 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
647 @retval EFI_UNSUPPORTED Could not open the file path.
648 @retval EFI_NOT_FOUND The specified file could not be found on the
649 device or the file system could not be found
651 @retval EFI_NO_MEDIA The device has no medium.
652 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
653 medium is no longer supported.
654 @retval EFI_DEVICE_ERROR The device reported an error.
655 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
656 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
657 @retval EFI_ACCESS_DENIED The file was opened read only.
658 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
660 @retval EFI_VOLUME_FULL The volume is full.
661 @sa ShellOpenFileByName
665 ShellCreateDirectory(
666 IN CONST CHAR16
*DirectoryName
,
667 OUT EFI_FILE_HANDLE
*FileHandle
669 if (mEfiShellProtocol
!= NULL
) {
671 // Use UEFI Shell 2.0 method
673 return (mEfiShellProtocol
->CreateFile(DirectoryName
,
678 return (ShellOpenFileByName(DirectoryName
,
680 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
687 This function reads information from an opened file.
689 If FileHandle is not a directory, the function reads the requested number of
690 bytes from the file at the file's current position and returns them in Buffer.
691 If the read goes beyond the end of the file, the read length is truncated to the
692 end of the file. The file's current position is increased by the number of bytes
693 returned. If FileHandle is a directory, the function reads the directory entry
694 at the file's current position and returns the entry in Buffer. If the Buffer
695 is not large enough to hold the current directory entry, then
696 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
697 BufferSize is set to be the size of the buffer needed to read the entry. On
698 success, the current position is updated to the next directory entry. If there
699 are no more directory entries, the read returns a zero-length buffer.
700 EFI_FILE_INFO is the structure returned as the directory entry.
702 @param FileHandle the opened file handle
703 @param BufferSize on input the size of buffer in bytes. on return
704 the number of bytes written.
705 @param Buffer the buffer to put read data into.
707 @retval EFI_SUCCESS Data was read.
708 @retval EFI_NO_MEDIA The device has no media.
709 @retval EFI_DEVICE_ERROR The device reported an error.
710 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
711 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
718 IN EFI_FILE_HANDLE FileHandle
,
719 IN OUT UINTN
*BufferSize
,
722 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
727 Write data to a file.
729 This function writes the specified number of bytes to the file at the current
730 file position. The current file position is advanced the actual number of bytes
731 written, which is returned in BufferSize. Partial writes only occur when there
732 has been a data error during the write attempt (such as "volume space full").
733 The file is automatically grown to hold the data if required. Direct writes to
734 opened directories are not supported.
736 @param FileHandle The opened file for writing
737 @param BufferSize on input the number of bytes in Buffer. On output
738 the number of bytes written.
739 @param Buffer the buffer containing data to write is stored.
741 @retval EFI_SUCCESS Data was written.
742 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
743 @retval EFI_NO_MEDIA The device has no media.
744 @retval EFI_DEVICE_ERROR The device reported an error.
745 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
746 @retval EFI_WRITE_PROTECTED The device is write-protected.
747 @retval EFI_ACCESS_DENIED The file was open for read only.
748 @retval EFI_VOLUME_FULL The volume is full.
753 IN EFI_FILE_HANDLE FileHandle
,
754 IN OUT UINTN
*BufferSize
,
757 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
761 Close an open file handle.
763 This function closes a specified file handle. All "dirty" cached file data is
764 flushed to the device, and the file is closed. In all cases the handle is
767 @param FileHandle the file handle to close.
769 @retval EFI_SUCCESS the file handle was closed sucessfully.
774 IN EFI_FILE_HANDLE
*FileHandle
776 return (FileFunctionMap
.CloseFile(*FileHandle
));
780 Delete a file and close the handle
782 This function closes and deletes a file. In all cases the file handle is closed.
783 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
784 returned, but the handle is still closed.
786 @param FileHandle the file handle to delete
788 @retval EFI_SUCCESS the file was closed sucessfully
789 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
791 @retval INVALID_PARAMETER One of the parameters has an invalid value.
796 IN EFI_FILE_HANDLE
*FileHandle
798 return (FileFunctionMap
.DeleteFile(*FileHandle
));
802 Set the current position in a file.
804 This function sets the current file position for the handle to the position
805 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
806 absolute positioning is supported, and seeking past the end of the file is
807 allowed (a subsequent write would grow the file). Seeking to position
808 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
809 If FileHandle is a directory, the only position that may be set is zero. This
810 has the effect of starting the read process of the directory entries over.
812 @param FileHandle The file handle on which the position is being set
813 @param Position Byte position from begining of file
815 @retval EFI_SUCCESS Operation completed sucessfully.
816 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
818 @retval INVALID_PARAMETER One of the parameters has an invalid value.
822 ShellSetFilePosition (
823 IN EFI_FILE_HANDLE FileHandle
,
826 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
830 Gets a file's current position
832 This function retrieves the current file position for the file handle. For
833 directories, the current file position has no meaning outside of the file
834 system driver and as such the operation is not supported. An error is returned
835 if FileHandle is a directory.
837 @param FileHandle The open file handle on which to get the position.
838 @param Position Byte position from begining of file.
840 @retval EFI_SUCCESS the operation completed sucessfully.
841 @retval INVALID_PARAMETER One of the parameters has an invalid value.
842 @retval EFI_UNSUPPORTED the request is not valid on directories.
846 ShellGetFilePosition (
847 IN EFI_FILE_HANDLE FileHandle
,
850 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
853 Flushes data on a file
855 This function flushes all modified data associated with a file to a device.
857 @param FileHandle The file handle on which to flush data
859 @retval EFI_SUCCESS The data was flushed.
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 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
864 @retval EFI_ACCESS_DENIED The file was opened for read only.
869 IN EFI_FILE_HANDLE FileHandle
871 return (FileFunctionMap
.FlushFile(FileHandle
));
875 Retrieves the first file from a directory
877 This function opens a directory and gets the first file's info in the
878 directory. Caller can use ShellFindNextFile() to get other files. When
879 complete the caller is responsible for calling FreePool() on Buffer.
881 @param DirHandle The file handle of the directory to search
882 @param Buffer Pointer to buffer for file's information
884 @retval EFI_SUCCESS Found the first file.
885 @retval EFI_NOT_FOUND Cannot find the directory.
886 @retval EFI_NO_MEDIA The device has no media.
887 @retval EFI_DEVICE_ERROR The device reported an error.
888 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
889 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
895 IN EFI_FILE_HANDLE DirHandle
,
896 OUT EFI_FILE_INFO
**Buffer
899 // pass to file handle lib
901 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
904 Retrieves the next file in a directory.
906 To use this function, caller must call the LibFindFirstFile() to get the
907 first file, and then use this function get other files. This function can be
908 called for several times to get each file's information in the directory. If
909 the call of ShellFindNextFile() got the last file in the directory, the next
910 call of this function has no file to get. *NoFile will be set to TRUE and the
911 Buffer memory will be automatically freed.
913 @param DirHandle the file handle of the directory
914 @param Buffer pointer to buffer for file's information
915 @param NoFile pointer to boolean when last file is found
917 @retval EFI_SUCCESS Found the next file, or reached last file
918 @retval EFI_NO_MEDIA The device has no media.
919 @retval EFI_DEVICE_ERROR The device reported an error.
920 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
925 IN EFI_FILE_HANDLE DirHandle
,
926 OUT EFI_FILE_INFO
*Buffer
,
930 // pass to file handle lib
932 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
935 Retrieve the size of a file.
937 if FileHandle is NULL then ASSERT()
938 if Size is NULL then ASSERT()
940 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
943 @param FileHandle file handle from which size is retrieved
944 @param Size pointer to size
946 @retval EFI_SUCCESS operation was completed sucessfully
947 @retval EFI_DEVICE_ERROR cannot access the file
952 IN EFI_FILE_HANDLE FileHandle
,
955 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
958 Retrieves the status of the break execution flag
960 this function is useful to check whether the application is being asked to halt by the shell.
962 @retval TRUE the execution break is enabled
963 @retval FALSE the execution break is not enabled
967 ShellGetExecutionBreakFlag(
972 // Check for UEFI Shell 2.0 protocols
974 if (mEfiShellProtocol
!= NULL
) {
977 // We are using UEFI Shell 2.0; see if the event has been triggered
979 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
986 // using EFI Shell; call the function to check
988 ASSERT(mEfiShellEnvironment2
!= NULL
);
989 return (mEfiShellEnvironment2
->GetExecutionBreak());
992 return the value of an environment variable
994 this function gets the value of the environment variable set by the
995 ShellSetEnvironmentVariable function
997 @param EnvKey The key name of the environment variable.
999 @retval NULL the named environment variable does not exist.
1000 @return != NULL pointer to the value of the environment variable
1004 ShellGetEnvironmentVariable (
1005 IN CONST CHAR16
*EnvKey
1009 // Check for UEFI Shell 2.0 protocols
1011 if (mEfiShellProtocol
!= NULL
) {
1012 return (mEfiShellProtocol
->GetEnv(EnvKey
));
1016 // ASSERT that we must have EFI shell
1018 ASSERT(mEfiShellEnvironment2
!= NULL
);
1023 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1026 set the value of an environment variable
1028 This function changes the current value of the specified environment variable. If the
1029 environment variable exists and the Value is an empty string, then the environment
1030 variable is deleted. If the environment variable exists and the Value is not an empty
1031 string, then the value of the environment variable is changed. If the environment
1032 variable does not exist and the Value is an empty string, there is no action. If the
1033 environment variable does not exist and the Value is a non-empty string, then the
1034 environment variable is created and assigned the specified value.
1036 This is not supported pre-UEFI Shell 2.0.
1038 @param EnvKey The key name of the environment variable.
1039 @param EnvVal The Value of the environment variable
1040 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1042 @retval EFI_SUCCESS the operation was completed sucessfully
1043 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1047 ShellSetEnvironmentVariable (
1048 IN CONST CHAR16
*EnvKey
,
1049 IN CONST CHAR16
*EnvVal
,
1054 // Check for UEFI Shell 2.0 protocols
1056 if (mEfiShellProtocol
!= NULL
) {
1057 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1061 // This feature does not exist under EFI shell
1063 return (EFI_UNSUPPORTED
);
1066 cause the shell to parse and execute a command line.
1068 This function creates a nested instance of the shell and executes the specified
1069 command (CommandLine) with the specified environment (Environment). Upon return,
1070 the status code returned by the specified command is placed in StatusCode.
1071 If Environment is NULL, then the current environment is used and all changes made
1072 by the commands executed will be reflected in the current environment. If the
1073 Environment is non-NULL, then the changes made will be discarded.
1074 The CommandLine is executed from the current working directory on the current
1077 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1078 Output is only supported for pre-UEFI Shell 2.0
1080 @param ImageHandle Parent image that is starting the operation
1081 @param CommandLine pointer to NULL terminated command line.
1082 @param Output true to display debug output. false to hide it.
1083 @param EnvironmentVariables optional pointer to array of environment variables
1084 in the form "x=y". if NULL current set is used.
1085 @param Status the status of the run command line.
1087 @retval EFI_SUCCESS the operation completed sucessfully. Status
1088 contains the status code returned.
1089 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1090 @retval EFI_OUT_OF_RESOURCES out of resources
1091 @retval EFI_UNSUPPORTED the operation is not allowed.
1096 IN EFI_HANDLE
*ParentHandle
,
1097 IN CHAR16
*CommandLine OPTIONAL
,
1098 IN BOOLEAN Output OPTIONAL
,
1099 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1100 OUT EFI_STATUS
*Status OPTIONAL
1104 // Check for UEFI Shell 2.0 protocols
1106 if (mEfiShellProtocol
!= NULL
) {
1108 // Call UEFI Shell 2.0 version (not using Output parameter)
1110 return (mEfiShellProtocol
->Execute(ParentHandle
,
1112 EnvironmentVariables
,
1116 // ASSERT that we must have EFI shell
1118 ASSERT(mEfiShellEnvironment2
!= NULL
);
1120 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1121 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1123 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1128 Retreives the current directory path
1130 If the DeviceName is NULL, it returns the current device's current directory
1131 name. If the DeviceName is not NULL, it returns the current directory name
1134 @param DeviceName the name of the drive to get directory on
1136 @retval NULL the directory does not exist
1137 @return != NULL the directory
1141 ShellGetCurrentDir (
1142 IN CHAR16
*DeviceName OPTIONAL
1146 // Check for UEFI Shell 2.0 protocols
1148 if (mEfiShellProtocol
!= NULL
) {
1149 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1152 // ASSERT that we must have EFI shell
1154 ASSERT(mEfiShellEnvironment2
!= NULL
);
1155 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1158 sets (enabled or disabled) the page break mode
1160 when page break mode is enabled the screen will stop scrolling
1161 and wait for operator input before scrolling a subsequent screen.
1163 @param CurrentState TRUE to enable and FALSE to disable
1167 ShellSetPageBreakMode (
1168 IN BOOLEAN CurrentState
1172 // check for enabling
1174 if (CurrentState
!= 0x00) {
1176 // check for UEFI Shell 2.0
1178 if (mEfiShellProtocol
!= NULL
) {
1180 // Enable with UEFI 2.0 Shell
1182 mEfiShellProtocol
->EnablePageBreak();
1186 // ASSERT that must have EFI Shell
1188 ASSERT(mEfiShellEnvironment2
!= NULL
);
1190 // Enable with EFI Shell
1192 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1197 // check for UEFI Shell 2.0
1199 if (mEfiShellProtocol
!= NULL
) {
1201 // Disable with UEFI 2.0 Shell
1203 mEfiShellProtocol
->DisablePageBreak();
1207 // ASSERT that must have EFI Shell
1209 ASSERT(mEfiShellEnvironment2
!= NULL
);
1211 // Disable with EFI Shell
1213 mEfiShellEnvironment2
->DisablePageBreak ();
1220 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1221 /// This allows for the struct to be populated.
1228 EFI_FILE_HANDLE Handle
;
1229 EFI_FILE_INFO
*Info
;
1230 } EFI_SHELL_FILE_INFO_NO_CONST
;
1233 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1235 if OldStyleFileList is NULL then ASSERT()
1237 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1238 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1239 the ShellCloseFileMetaArg function.
1241 @param[in] FileList the EFI shell list type
1242 @param[in,out] ListHead the list to add to
1244 @retval the resultant head of the double linked new format list;
1248 InternalShellConvertFileListType (
1249 IN LIST_ENTRY
*FileList
,
1250 IN OUT LIST_ENTRY
*ListHead
1253 SHELL_FILE_ARG
*OldInfo
;
1255 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1260 ASSERT(FileList
!= NULL
);
1261 ASSERT(ListHead
!= NULL
);
1264 // enumerate through each member of the old list and copy
1266 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1267 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1270 // make sure the old list was valid
1272 ASSERT(OldInfo
!= NULL
);
1273 ASSERT(OldInfo
->Info
!= NULL
);
1274 ASSERT(OldInfo
->FullName
!= NULL
);
1275 ASSERT(OldInfo
->FileName
!= NULL
);
1278 // allocate a new EFI_SHELL_FILE_INFO object
1280 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1281 ASSERT(NewInfo
!= NULL
);
1282 if (NewInfo
== NULL
) {
1287 // copy the simple items
1289 NewInfo
->Handle
= OldInfo
->Handle
;
1290 NewInfo
->Status
= OldInfo
->Status
;
1292 // old shell checks for 0 not NULL
1293 OldInfo
->Handle
= 0;
1296 // allocate new space to copy strings and structure
1298 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1299 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1300 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1303 // make sure all the memory allocations were sucessful
1305 ASSERT(NewInfo
->FullName
!= NULL
);
1306 ASSERT(NewInfo
->FileName
!= NULL
);
1307 ASSERT(NewInfo
->Info
!= NULL
);
1310 // Copt the strings and structure
1312 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1313 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1314 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1317 // add that to the list
1319 InsertTailList(ListHead
, &NewInfo
->Link
);
1324 Opens a group of files based on a path.
1326 This function uses the Arg to open all the matching files. Each matched
1327 file has a SHELL_FILE_ARG structure to record the file information. These
1328 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1329 structures from ListHead to access each file. This function supports wildcards
1330 and will process '?' and '*' as such. the list must be freed with a call to
1331 ShellCloseFileMetaArg().
1333 If you are NOT appending to an existing list *ListHead must be NULL. If
1334 *ListHead is NULL then it must be callee freed.
1336 @param Arg pointer to path string
1337 @param OpenMode mode to open files with
1338 @param ListHead head of linked list of results
1340 @retval EFI_SUCCESS the operation was sucessful and the list head
1341 contains the list of opened files
1342 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1343 *ListHead is set to NULL.
1344 @return != EFI_SUCCESS the operation failed
1346 @sa InternalShellConvertFileListType
1350 ShellOpenFileMetaArg (
1353 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1357 LIST_ENTRY mOldStyleFileList
;
1360 // ASSERT that Arg and ListHead are not NULL
1362 ASSERT(Arg
!= NULL
);
1363 ASSERT(ListHead
!= NULL
);
1366 // Check for UEFI Shell 2.0 protocols
1368 if (mEfiShellProtocol
!= NULL
) {
1369 if (*ListHead
== NULL
) {
1370 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1371 if (*ListHead
== NULL
) {
1372 return (EFI_OUT_OF_RESOURCES
);
1374 InitializeListHead(&((*ListHead
)->Link
));
1376 Status
= mEfiShellProtocol
->OpenFileList(Arg
,
1379 if (EFI_ERROR(Status
)) {
1380 mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1382 Status
= mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1388 // ASSERT that we must have EFI shell
1390 ASSERT(mEfiShellEnvironment2
!= NULL
);
1393 // make sure the list head is initialized
1395 InitializeListHead(&mOldStyleFileList
);
1398 // Get the EFI Shell list of files
1400 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1401 if (EFI_ERROR(Status
)) {
1406 if (*ListHead
== NULL
) {
1407 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1408 if (*ListHead
== NULL
) {
1409 return (EFI_OUT_OF_RESOURCES
);
1414 // Convert that to equivalent of UEFI Shell 2.0 structure
1416 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1419 // Free the EFI Shell version that was converted.
1421 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1426 Free the linked list returned from ShellOpenFileMetaArg
1428 if ListHead is NULL then ASSERT()
1430 @param ListHead the pointer to free
1432 @retval EFI_SUCCESS the operation was sucessful
1436 ShellCloseFileMetaArg (
1437 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1443 // ASSERT that ListHead is not NULL
1445 ASSERT(ListHead
!= NULL
);
1448 // Check for UEFI Shell 2.0 protocols
1450 if (mEfiShellProtocol
!= NULL
) {
1451 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1454 // Since this is EFI Shell version we need to free our internally made copy
1457 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1458 ; IsListEmpty(&(*ListHead
)->Link
) == FALSE
1459 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1460 RemoveEntryList(Node
);
1461 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1462 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1463 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1464 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1465 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1472 Find a file by searching the CWD and then the path.
1474 If FileName is NULL then ASSERT.
1476 If the return value is not NULL then the memory must be caller freed.
1478 @param FileName Filename string.
1480 @retval NULL the file was not found
1481 @return !NULL the full path to the file.
1486 IN CONST CHAR16
*FileName
1490 EFI_FILE_HANDLE Handle
;
1494 CONST CHAR16
*Walker
;
1500 Path
= ShellGetEnvironmentVariable(L
"cwd");
1502 Size
= StrSize(Path
);
1503 Size
+= StrSize(FileName
);
1504 TestPath
= AllocateZeroPool(Size
);
1505 ASSERT(TestPath
!= NULL
);
1506 if (TestPath
== NULL
) {
1509 StrCpy(TestPath
, Path
);
1510 StrCat(TestPath
, FileName
);
1511 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1512 if (!EFI_ERROR(Status
)){
1513 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1514 ShellCloseFile(&Handle
);
1520 Path
= ShellGetEnvironmentVariable(L
"path");
1522 Size
= StrSize(Path
);
1523 Size
+= StrSize(FileName
);
1524 TestPath
= AllocateZeroPool(Size
);
1525 Walker
= (CHAR16
*)Path
;
1527 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1528 TempChar
= StrStr(TestPath
, L
";");
1529 if (TempChar
!= NULL
) {
1530 *TempChar
= CHAR_NULL
;
1532 StrCat(TestPath
, FileName
);
1533 if (StrStr(Walker
, L
";") != NULL
) {
1534 Walker
= StrStr(Walker
, L
";") + 1;
1538 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1539 if (!EFI_ERROR(Status
)){
1540 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1541 ShellCloseFile(&Handle
);
1544 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1551 Find a file by searching the CWD and then the path with a variable set of file
1552 extensions. If the file is not found it will append each extension in the list
1553 in the order provided and return the first one that is successful.
1555 If FileName is NULL, then ASSERT.
1556 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1558 If the return value is not NULL then the memory must be caller freed.
1560 @param[in] FileName Filename string.
1561 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1563 @retval NULL The file was not found.
1564 @retval !NULL The path to the file.
1568 ShellFindFilePathEx (
1569 IN CONST CHAR16
*FileName
,
1570 IN CONST CHAR16
*FileExtension
1575 CONST CHAR16
*ExtensionWalker
;
1580 ASSERT(FileName
!= NULL
);
1581 if (FileExtension
== NULL
) {
1582 return (ShellFindFilePath(FileName
));
1584 RetVal
= ShellFindFilePath(FileName
);
1585 if (RetVal
!= NULL
) {
1588 Size
= StrSize(FileName
);
1589 Size
+= StrSize(FileExtension
);
1590 TestPath
= AllocateZeroPool(Size
);
1591 ASSERT(TestPath
!= NULL
);
1592 if (TestPath
== NULL
) {
1595 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1 ){
1596 StrCpy(TestPath
, FileName
);
1597 if (ExtensionWalker
!= NULL
) {
1598 StrCat(TestPath
, ExtensionWalker
);
1600 TempChar
= StrStr(TestPath
, L
";");
1601 if (TempChar
!= NULL
) {
1602 *TempChar
= CHAR_NULL
;
1604 RetVal
= ShellFindFilePath(TestPath
);
1605 if (RetVal
!= NULL
) {
1608 TempChar2
= StrStr(ExtensionWalker
, L
";");
1619 UINTN OriginalPosition
;
1620 } SHELL_PARAM_PACKAGE
;
1623 Checks the list of valid arguments and returns TRUE if the item was found. If the
1624 return value is TRUE then the type parameter is set also.
1626 if CheckList is NULL then ASSERT();
1627 if Name is NULL then ASSERT();
1628 if Type is NULL then ASSERT();
1630 @param Type pointer to type of parameter if it was found
1631 @param Name pointer to Name of parameter found
1632 @param CheckList List to check against
1634 @retval TRUE the Parameter was found. Type is valid.
1635 @retval FALSE the Parameter was not found. Type is not valid.
1639 InternalIsOnCheckList (
1640 IN CONST CHAR16
*Name
,
1641 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1644 SHELL_PARAM_ITEM
*TempListItem
;
1647 // ASSERT that all 3 pointer parameters aren't NULL
1649 ASSERT(CheckList
!= NULL
);
1650 ASSERT(Type
!= NULL
);
1651 ASSERT(Name
!= NULL
);
1654 // question mark and page break mode are always supported
1656 if ((StrCmp(Name
, L
"-?") == 0) ||
1657 (StrCmp(Name
, L
"-b") == 0)
1663 // Enumerate through the list
1665 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1667 // If the Type is TypeStart only check the first characters of the passed in param
1668 // If it matches set the type and return TRUE
1670 if (TempListItem
->Type
== TypeStart
&& StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1671 *Type
= TempListItem
->Type
;
1673 } else if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1674 *Type
= TempListItem
->Type
;
1682 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1684 @param Name pointer to Name of parameter found
1686 @retval TRUE the Parameter is a flag.
1687 @retval FALSE the Parameter not a flag
1692 IN CONST CHAR16
*Name
,
1693 IN BOOLEAN AlwaysAllowNumbers
1697 // ASSERT that Name isn't NULL
1699 ASSERT(Name
!= NULL
);
1702 // If we accept numbers then dont return TRUE. (they will be values)
1704 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
!= FALSE
) {
1709 // If the Name has a / or - as the first character return TRUE
1711 if ((Name
[0] == L
'/') ||
1712 (Name
[0] == L
'-') ||
1721 Checks the command line arguments passed against the list of valid ones.
1723 If no initialization is required, then return RETURN_SUCCESS.
1725 @param CheckList pointer to list of parameters to check
1726 @param CheckPackage pointer to pointer to list checked values
1727 @param ProblemParam optional pointer to pointer to unicode string for
1728 the paramater that caused failure. If used then the
1729 caller is responsible for freeing the memory.
1730 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1731 @param Argc Count of parameters in Argv
1732 @param Argv pointer to array of parameters
1734 @retval EFI_SUCCESS The operation completed sucessfully.
1735 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1736 @retval EFI_INVALID_PARAMETER A parameter was invalid
1737 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1738 duplicated. the duplicated command line argument
1739 was returned in ProblemParam if provided.
1740 @retval EFI_NOT_FOUND a argument required a value that was missing.
1741 the invalid command line argument was returned in
1742 ProblemParam if provided.
1747 InternalCommandLineParse (
1748 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1749 OUT LIST_ENTRY
**CheckPackage
,
1750 OUT CHAR16
**ProblemParam OPTIONAL
,
1751 IN BOOLEAN AutoPageBreak
,
1752 IN CONST CHAR16
**Argv
,
1754 IN BOOLEAN AlwaysAllowNumbers
1757 ParamType CurrentItemType
;
1758 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1762 CurrentItemPackage
= NULL
;
1763 mTotalParameterCount
= 0;
1768 // If there is only 1 item we dont need to do anything
1771 *CheckPackage
= NULL
;
1772 return (EFI_SUCCESS
);
1778 ASSERT(CheckList
!= NULL
);
1779 ASSERT(Argv
!= NULL
);
1782 // initialize the linked list
1784 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1785 InitializeListHead(*CheckPackage
);
1788 // loop through each of the arguments
1790 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1791 if (Argv
[LoopCounter
] == NULL
) {
1793 // do nothing for NULL argv
1795 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) != FALSE
) {
1797 // We might have leftover if last parameter didnt have optional value
1799 if (GetItemValue
!= 0) {
1801 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1806 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1807 ASSERT(CurrentItemPackage
!= NULL
);
1808 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1809 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1810 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1811 CurrentItemPackage
->Type
= CurrentItemType
;
1812 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1813 CurrentItemPackage
->Value
= NULL
;
1816 // Does this flag require a value
1818 switch (CurrentItemPackage
->Type
) {
1820 // possibly trigger the next loop(s) to populate the value of this item
1826 case TypeDoubleValue
:
1831 GetItemValue
= (UINTN
)(-1);
1836 // this item has no value expected; we are done
1838 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1839 ASSERT(GetItemValue
== 0);
1842 } else if (GetItemValue
!= 0 && InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) == FALSE
) {
1843 ASSERT(CurrentItemPackage
!= NULL
);
1845 // get the item VALUE for a previous flag
1847 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
1848 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1849 if (ValueSize
== 0) {
1850 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1852 StrCat(CurrentItemPackage
->Value
, L
" ");
1853 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1855 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
1857 if (GetItemValue
== 0) {
1858 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1860 } else if (InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) == FALSE
) {
1862 // add this one as a non-flag
1864 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1865 ASSERT(CurrentItemPackage
!= NULL
);
1866 CurrentItemPackage
->Name
= NULL
;
1867 CurrentItemPackage
->Type
= TypePosition
;
1868 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1869 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1870 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1871 CurrentItemPackage
->OriginalPosition
= mTotalParameterCount
++;
1872 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1873 } else if (ProblemParam
) {
1875 // this was a non-recognised flag... error!
1877 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1878 ASSERT(*ProblemParam
!= NULL
);
1879 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1880 ShellCommandLineFreeVarList(*CheckPackage
);
1881 *CheckPackage
= NULL
;
1882 return (EFI_VOLUME_CORRUPTED
);
1884 ShellCommandLineFreeVarList(*CheckPackage
);
1885 *CheckPackage
= NULL
;
1886 return (EFI_VOLUME_CORRUPTED
);
1889 if (GetItemValue
!= 0) {
1891 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1894 // support for AutoPageBreak
1896 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1897 ShellSetPageBreakMode(TRUE
);
1899 return (EFI_SUCCESS
);
1903 Checks the command line arguments passed against the list of valid ones.
1904 Optionally removes NULL values first.
1906 If no initialization is required, then return RETURN_SUCCESS.
1908 @param CheckList pointer to list of parameters to check
1909 @param CheckPackage pointer to pointer to list checked values
1910 @param ProblemParam optional pointer to pointer to unicode string for
1911 the paramater that caused failure.
1912 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1914 @retval EFI_SUCCESS The operation completed sucessfully.
1915 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1916 @retval EFI_INVALID_PARAMETER A parameter was invalid
1917 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1918 duplicated. the duplicated command line argument
1919 was returned in ProblemParam if provided.
1920 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1921 of the command line arguments was returned in
1922 ProblemParam if provided.
1923 @retval EFI_NOT_FOUND a argument required a value that was missing.
1924 the invalid command line argument was returned in
1925 ProblemParam if provided.
1929 ShellCommandLineParseEx (
1930 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1931 OUT LIST_ENTRY
**CheckPackage
,
1932 OUT CHAR16
**ProblemParam OPTIONAL
,
1933 IN BOOLEAN AutoPageBreak
,
1934 IN BOOLEAN AlwaysAllowNumbers
1937 // ASSERT that CheckList and CheckPackage aren't NULL
1939 ASSERT(CheckList
!= NULL
);
1940 ASSERT(CheckPackage
!= NULL
);
1943 // Check for UEFI Shell 2.0 protocols
1945 if (mEfiShellParametersProtocol
!= NULL
) {
1946 return (InternalCommandLineParse(CheckList
,
1950 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
1951 mEfiShellParametersProtocol
->Argc
,
1952 AlwaysAllowNumbers
));
1956 // ASSERT That EFI Shell is not required
1958 ASSERT (mEfiShellInterface
!= NULL
);
1959 return (InternalCommandLineParse(CheckList
,
1963 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
1964 mEfiShellInterface
->Argc
,
1965 AlwaysAllowNumbers
));
1969 Frees shell variable list that was returned from ShellCommandLineParse.
1971 This function will free all the memory that was used for the CheckPackage
1972 list of postprocessed shell arguments.
1974 this function has no return value.
1976 if CheckPackage is NULL, then return
1978 @param CheckPackage the list to de-allocate
1982 ShellCommandLineFreeVarList (
1983 IN LIST_ENTRY
*CheckPackage
1988 // check for CheckPackage == NULL
1990 if (CheckPackage
== NULL
) {
1995 // for each node in the list
1997 for ( Node
= GetFirstNode(CheckPackage
)
1998 ; IsListEmpty(CheckPackage
) == FALSE
1999 ; Node
= GetFirstNode(CheckPackage
)
2002 // Remove it from the list
2004 RemoveEntryList(Node
);
2007 // if it has a name free the name
2009 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2010 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2014 // if it has a value free the value
2016 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2017 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2021 // free the node structure
2023 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2026 // free the list head node
2028 FreePool(CheckPackage
);
2031 Checks for presence of a flag parameter
2033 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2035 if CheckPackage is NULL then return FALSE.
2036 if KeyString is NULL then ASSERT()
2038 @param CheckPackage The package of parsed command line arguments
2039 @param KeyString the Key of the command line argument to check for
2041 @retval TRUE the flag is on the command line
2042 @retval FALSE the flag is not on the command line
2046 ShellCommandLineGetFlag (
2047 IN CONST LIST_ENTRY
*CheckPackage
,
2048 IN CHAR16
*KeyString
2053 // ASSERT that both CheckPackage and KeyString aren't NULL
2055 ASSERT(KeyString
!= NULL
);
2058 // return FALSE for no package
2060 if (CheckPackage
== NULL
) {
2065 // enumerate through the list of parametrs
2067 for ( Node
= GetFirstNode(CheckPackage
)
2068 ; !IsNull (CheckPackage
, Node
)
2069 ; Node
= GetNextNode(CheckPackage
, Node
)
2072 // If the Name matches, return TRUE (and there may be NULL name)
2074 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2076 // If Type is TypeStart then only compare the begining of the strings
2078 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2079 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2082 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2090 returns value from command line argument
2092 value parameters are in the form of "-<Key> value" or "/<Key> value"
2094 if CheckPackage is NULL, then return NULL;
2096 @param CheckPackage The package of parsed command line arguments
2097 @param KeyString the Key of the command line argument to check for
2099 @retval NULL the flag is not on the command line
2100 @return !=NULL pointer to unicode string of the value
2104 ShellCommandLineGetValue (
2105 IN CONST LIST_ENTRY
*CheckPackage
,
2106 IN CHAR16
*KeyString
2111 // check for CheckPackage == NULL
2113 if (CheckPackage
== NULL
) {
2118 // enumerate through the list of parametrs
2120 for ( Node
= GetFirstNode(CheckPackage
)
2121 ; !IsNull (CheckPackage
, Node
)
2122 ; Node
= GetNextNode(CheckPackage
, Node
)
2125 // If the Name matches, return the value (name can be NULL)
2127 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2129 // If Type is TypeStart then only compare the begining of the strings
2131 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2132 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2135 // return the string part after the flag
2137 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2138 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2142 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2149 returns raw value from command line argument
2151 raw value parameters are in the form of "value" in a specific position in the list
2153 if CheckPackage is NULL, then return NULL;
2155 @param CheckPackage The package of parsed command line arguments
2156 @param Position the position of the value
2158 @retval NULL the flag is not on the command line
2159 @return !=NULL pointer to unicode string of the value
2163 ShellCommandLineGetRawValue (
2164 IN CONST LIST_ENTRY
*CheckPackage
,
2170 // check for CheckPackage == NULL
2172 if (CheckPackage
== NULL
) {
2177 // enumerate through the list of parametrs
2179 for ( Node
= GetFirstNode(CheckPackage
)
2180 ; !IsNull (CheckPackage
, Node
)
2181 ; Node
= GetNextNode(CheckPackage
, Node
)
2184 // If the position matches, return the value
2186 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2187 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2194 returns the number of command line value parameters that were parsed.
2196 this will not include flags.
2198 @retval (UINTN)-1 No parsing has ocurred
2199 @return other The number of value parameters found
2203 ShellCommandLineGetCount(
2207 return (mTotalParameterCount
);
2211 Determins if a parameter is duplicated.
2213 If Param is not NULL then it will point to a callee allocated string buffer
2214 with the parameter value if a duplicate is found.
2216 If CheckPackage is NULL, then ASSERT.
2218 @param[in] CheckPackage The package of parsed command line arguments.
2219 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2221 @retval EFI_SUCCESS No parameters were duplicated.
2222 @retval EFI_DEVICE_ERROR A duplicate was found.
2226 ShellCommandLineCheckDuplicate (
2227 IN CONST LIST_ENTRY
*CheckPackage
,
2234 ASSERT(CheckPackage
!= NULL
);
2236 for ( Node1
= GetFirstNode(CheckPackage
)
2237 ; !IsNull (CheckPackage
, Node1
)
2238 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2240 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2241 ; !IsNull (CheckPackage
, Node2
)
2242 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2244 if (StrCmp(((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, ((SHELL_PARAM_PACKAGE
*)Node2
)->Name
) == 0) {
2245 if (Param
!= NULL
) {
2247 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2249 return (EFI_DEVICE_ERROR
);
2253 return (EFI_SUCCESS
);
2257 This is a find and replace function. Upon successful return the NewString is a copy of
2258 SourceString with each instance of FindTarget replaced with ReplaceWith.
2260 If SourceString and NewString overlap the behavior is undefined.
2262 If the string would grow bigger than NewSize it will halt and return error.
2264 @param[in] SourceString String with source buffer
2265 @param[in,out] NewString String with resultant buffer
2266 @param[in] NewSize Size in bytes of NewString
2267 @param[in] FindTarget String to look for
2268 @param[in[ ReplaceWith String to replace FindTarget with
2269 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2270 immediately before it.
2272 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2273 @retval EFI_INVALID_PARAMETER NewString was NULL.
2274 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2275 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2276 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2277 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2278 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2279 the new string (truncation occurred).
2280 @retval EFI_SUCCESS the string was sucessfully copied with replacement.
2285 ShellCopySearchAndReplace2(
2286 IN CHAR16 CONST
*SourceString
,
2287 IN CHAR16
*NewString
,
2289 IN CONST CHAR16
*FindTarget
,
2290 IN CONST CHAR16
*ReplaceWith
,
2291 IN CONST BOOLEAN SkipPreCarrot
2295 if ( (SourceString
== NULL
)
2296 || (NewString
== NULL
)
2297 || (FindTarget
== NULL
)
2298 || (ReplaceWith
== NULL
)
2299 || (StrLen(FindTarget
) < 1)
2300 || (StrLen(SourceString
) < 1)
2302 return (EFI_INVALID_PARAMETER
);
2304 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2305 while (*SourceString
!= CHAR_NULL
) {
2307 // if we find the FindTarget and either Skip == FALSE or Skip == TRUE and we
2308 // dont have a carrot do a replace...
2310 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2311 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || SkipPreCarrot
== FALSE
)
2313 SourceString
+= StrLen(FindTarget
);
2314 Size
= StrSize(NewString
);
2315 if ((Size
+ (StrLen(ReplaceWith
)*sizeof(CHAR16
))) > NewSize
) {
2316 return (EFI_BUFFER_TOO_SMALL
);
2318 StrCat(NewString
, ReplaceWith
);
2320 Size
= StrSize(NewString
);
2321 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2322 return (EFI_BUFFER_TOO_SMALL
);
2324 StrnCat(NewString
, SourceString
, 1);
2328 return (EFI_SUCCESS
);
2332 Internal worker function to output a string.
2334 This function will output a string to the correct StdOut.
2336 @param[in] String The string to print out.
2338 @retval EFI_SUCCESS The operation was sucessful.
2339 @retval !EFI_SUCCESS The operation failed.
2344 IN CONST CHAR16
*String
2348 Size
= StrSize(String
) - sizeof(CHAR16
);
2349 if (mEfiShellParametersProtocol
!= NULL
) {
2350 return (mEfiShellParametersProtocol
->StdOut
->Write(mEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2352 if (mEfiShellInterface
!= NULL
) {
2354 // Divide in half for old shell. Must be string length not size.
2357 return ( mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2360 return (EFI_UNSUPPORTED
);
2364 Print at a specific location on the screen.
2366 This function will move the cursor to a given screen location and print the specified string
2368 If -1 is specified for either the Row or Col the current screen location for BOTH
2371 if either Row or Col is out of range for the current console, then ASSERT
2372 if Format is NULL, then ASSERT
2374 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2375 the following additional flags:
2376 %N - Set output attribute to normal
2377 %H - Set output attribute to highlight
2378 %E - Set output attribute to error
2379 %B - Set output attribute to blue color
2380 %V - Set output attribute to green color
2382 Note: The background color is controlled by the shell command cls.
2384 @param[in] Row the row to print at
2385 @param[in] Col the column to print at
2386 @param[in] Format the format string
2387 @param[in] Marker the marker for the variable argument list
2389 @return the number of characters printed to the screen
2394 InternalShellPrintWorker(
2395 IN INT32 Col OPTIONAL
,
2396 IN INT32 Row OPTIONAL
,
2397 IN CONST CHAR16
*Format
,
2403 UINTN NormalAttribute
;
2404 CHAR16
*ResumeLocation
;
2405 CHAR16
*FormatWalker
;
2408 // Back and forth each time fixing up 1 of our flags...
2410 Status
= ShellLibCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N");
2411 ASSERT_EFI_ERROR(Status
);
2412 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E");
2413 ASSERT_EFI_ERROR(Status
);
2414 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H");
2415 ASSERT_EFI_ERROR(Status
);
2416 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B");
2417 ASSERT_EFI_ERROR(Status
);
2418 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V");
2419 ASSERT_EFI_ERROR(Status
);
2422 // Use the last buffer from replacing to print from...
2424 Return
= UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2426 if (Col
!= -1 && Row
!= -1) {
2427 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2428 ASSERT_EFI_ERROR(Status
);
2431 NormalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2432 FormatWalker
= mPostReplaceFormat2
;
2433 while (*FormatWalker
!= CHAR_NULL
) {
2435 // Find the next attribute change request
2437 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2438 if (ResumeLocation
!= NULL
) {
2439 *ResumeLocation
= CHAR_NULL
;
2442 // print the current FormatWalker string
2444 Status
= InternalPrintTo(FormatWalker
);
2445 ASSERT_EFI_ERROR(Status
);
2447 // update the attribute
2449 if (ResumeLocation
!= NULL
) {
2450 switch (*(ResumeLocation
+1)) {
2452 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2455 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2458 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2461 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2464 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2468 // Print a simple '%' symbol
2470 Status
= InternalPrintTo(L
"%");
2471 ASSERT_EFI_ERROR(Status
);
2472 ResumeLocation
= ResumeLocation
- 1;
2477 // reset to normal now...
2479 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2484 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2486 FormatWalker
= ResumeLocation
+ 2;
2493 Print at a specific location on the screen.
2495 This function will move the cursor to a given screen location and print the specified string.
2497 If -1 is specified for either the Row or Col the current screen location for BOTH
2500 If either Row or Col is out of range for the current console, then ASSERT.
2501 If Format is NULL, then ASSERT.
2503 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2504 the following additional flags:
2505 %N - Set output attribute to normal
2506 %H - Set output attribute to highlight
2507 %E - Set output attribute to error
2508 %B - Set output attribute to blue color
2509 %V - Set output attribute to green color
2511 Note: The background color is controlled by the shell command cls.
2513 @param[in] Row the row to print at
2514 @param[in] Col the column to print at
2515 @param[in] Format the format string
2517 @return the number of characters printed to the screen
2523 IN INT32 Col OPTIONAL
,
2524 IN INT32 Row OPTIONAL
,
2525 IN CONST CHAR16
*Format
,
2531 VA_START (Marker
, Format
);
2532 Status
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2538 Print at a specific location on the screen.
2540 This function will move the cursor to a given screen location and print the specified string.
2542 If -1 is specified for either the Row or Col the current screen location for BOTH
2545 If either Row or Col is out of range for the current console, then ASSERT.
2546 If Format is NULL, then ASSERT.
2548 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2549 the following additional flags:
2550 %N - Set output attribute to normal.
2551 %H - Set output attribute to highlight.
2552 %E - Set output attribute to error.
2553 %B - Set output attribute to blue color.
2554 %V - Set output attribute to green color.
2556 Note: The background color is controlled by the shell command cls.
2558 @param[in] Row The row to print at.
2559 @param[in] Col The column to print at.
2560 @param[in] Language The language of the string to retrieve. If this parameter
2561 is NULL, then the current platform language is used.
2562 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2563 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2565 @return the number of characters printed to the screen.
2570 IN INT32 Col OPTIONAL
,
2571 IN INT32 Row OPTIONAL
,
2572 IN CONST CHAR8
*Language OPTIONAL
,
2573 IN CONST EFI_STRING_ID HiiFormatStringId
,
2574 IN CONST EFI_HANDLE HiiFormatHandle
,
2579 CHAR16
*HiiFormatString
;
2582 VA_START (Marker
, HiiFormatHandle
);
2583 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2584 ASSERT(HiiFormatString
!= NULL
);
2586 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2588 FreePool(HiiFormatString
);
2595 Function to determine if a given filename represents a file or a directory.
2597 @param[in] DirName Path to directory to test.
2599 @retval EFI_SUCCESS The Path represents a directory
2600 @retval EFI_NOT_FOUND The Path does not represent a directory
2601 @return other The path failed to open
2606 IN CONST CHAR16
*DirName
2610 EFI_FILE_HANDLE Handle
;
2612 ASSERT(DirName
!= NULL
);
2616 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2617 if (EFI_ERROR(Status
)) {
2621 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2622 ShellCloseFile(&Handle
);
2623 return (EFI_SUCCESS
);
2625 ShellCloseFile(&Handle
);
2626 return (EFI_NOT_FOUND
);
2630 Function to determine if a given filename represents a file.
2632 @param[in] Name Path to file to test.
2634 @retval EFI_SUCCESS The Path represents a file.
2635 @retval EFI_NOT_FOUND The Path does not represent a file.
2636 @retval other The path failed to open.
2641 IN CONST CHAR16
*Name
2645 EFI_FILE_HANDLE Handle
;
2647 ASSERT(Name
!= NULL
);
2651 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
2652 if (EFI_ERROR(Status
)) {
2656 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
2657 ShellCloseFile(&Handle
);
2658 return (EFI_SUCCESS
);
2660 ShellCloseFile(&Handle
);
2661 return (EFI_NOT_FOUND
);
2665 Function to determine if a given filename represents a file.
2667 This will search the CWD and then the Path.
2669 If Name is NULL, then ASSERT.
2671 @param[in] Name Path to file to test.
2673 @retval EFI_SUCCESS The Path represents a file.
2674 @retval EFI_NOT_FOUND The Path does not represent a file.
2675 @retval other The path failed to open.
2680 IN CONST CHAR16
*Name
2685 if (!EFI_ERROR(ShellIsFile(Name
))) {
2689 NewName
= ShellFindFilePath(Name
);
2690 if (NewName
== NULL
) {
2691 return (EFI_NOT_FOUND
);
2693 Status
= ShellIsFile(NewName
);
2698 Function to determine whether a string is decimal or hex representation of a number
2699 and return the number converted from the string.
2701 @param[in] String String representation of a number
2703 @retval all the number
2708 IN CONST CHAR16
*String
2711 CONST CHAR16
*Walker
;
2712 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
2713 if (Walker
== NULL
|| *Walker
== CHAR_NULL
) {
2715 return ((UINTN
)(-1));
2717 if (StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
2718 return (StrHexToUintn(Walker
));
2720 return (StrDecimalToUintn(Walker
));
2725 Safely append with automatic string resizing given length of Destination and
2726 desired length of copy from Source.
2728 append the first D characters of Source to the end of Destination, where D is
2729 the lesser of Count and the StrLen() of Source. If appending those D characters
2730 will fit within Destination (whose Size is given as CurrentSize) and
2731 still leave room for a NULL terminator, then those characters are appended,
2732 starting at the original terminating NULL of Destination, and a new terminating
2735 If appending D characters onto Destination will result in a overflow of the size
2736 given in CurrentSize the string will be grown such that the copy can be performed
2737 and CurrentSize will be updated to the new size.
2739 If Source is NULL, there is nothing to append, just return the current buffer in
2742 if Destination is NULL, then ASSERT()
2743 if Destination's current length (including NULL terminator) is already more then
2744 CurrentSize, then ASSERT()
2746 @param[in,out] Destination The String to append onto
2747 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2748 return possibly the new size (still in bytes). if NULL
2749 then allocate whatever is needed.
2750 @param[in] Source The String to append from
2751 @param[in] Count Maximum number of characters to append. if 0 then
2754 @return Destination return the resultant string.
2759 IN OUT CHAR16
**Destination
,
2760 IN OUT UINTN
*CurrentSize
,
2761 IN CONST CHAR16
*Source
,
2765 UINTN DestinationStartSize
;
2771 ASSERT(Destination
!= NULL
);
2774 // If there's nothing to do then just return Destination
2776 if (Source
== NULL
) {
2777 return (*Destination
);
2781 // allow for un-initialized pointers, based on size being 0
2783 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
2784 *Destination
= NULL
;
2788 // allow for NULL pointers address as Destination
2790 if (*Destination
!= NULL
) {
2791 ASSERT(CurrentSize
!= 0);
2792 DestinationStartSize
= StrSize(*Destination
);
2793 ASSERT(DestinationStartSize
<= *CurrentSize
);
2795 DestinationStartSize
= 0;
2796 // ASSERT(*CurrentSize == 0);
2800 // Append all of Source?
2803 Count
= StrLen(Source
);
2807 // Test and grow if required
2809 if (CurrentSize
!= NULL
) {
2810 NewSize
= *CurrentSize
;
2811 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
2812 NewSize
+= 2 * Count
* sizeof(CHAR16
);
2814 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
2815 ASSERT(*Destination
!= NULL
);
2816 *CurrentSize
= NewSize
;
2818 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
2819 ASSERT(*Destination
!= NULL
);
2823 // Now use standard StrnCat on a big enough buffer
2825 if (*Destination
== NULL
) {
2828 return StrnCat(*Destination
, Source
, Count
);
2832 Prompt the user and return the resultant answer to the requestor.
2834 This function will display the requested question on the shell prompt and then
2835 wait for an apropriate answer to be input from the console.
2837 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, SHELL_PROMPT_REQUEST_TYPE_QUIT_CONTINUE
2838 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
2840 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_FREEFORM then *Response is of type
2843 In either case *Response must be callee freed if Response was not NULL;
2845 @param Type What type of question is asked. This is used to filter the input
2846 to prevent invalid answers to question.
2847 @param Prompt Pointer to string prompt to use to request input.
2848 @param Response Pointer to Response which will be populated upon return.
2850 @retval EFI_SUCCESS The operation was sucessful.
2851 @retval EFI_UNSUPPORTED The operation is not supported as requested.
2852 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2853 @return other The operation failed.
2857 ShellPromptForResponse (
2858 IN SHELL_PROMPT_REQUEST_TYPE Type
,
2859 IN CHAR16
*Prompt OPTIONAL
,
2860 IN OUT VOID
**Response OPTIONAL
2866 SHELL_PROMPT_RESPONSE
*Resp
;
2868 Status
= EFI_SUCCESS
;
2869 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocatePool(sizeof(SHELL_PROMPT_RESPONSE
));
2872 case SHELL_PROMPT_REQUEST_TYPE_QUIT_CONTINUE
:
2873 if (Prompt
!= NULL
) {
2874 ShellPrintEx(-1, -1, L
"%s", Prompt
);
2877 // wait for valid response
2879 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
2880 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
2881 ASSERT_EFI_ERROR(Status
);
2882 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
2883 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
2884 *Resp
= SHELL_PROMPT_RESPONSE_QUIT
;
2886 *Resp
= SHELL_PROMPT_RESPONSE_CONTINUE
;
2889 case SHELL_PROMPT_REQUEST_TYPE_YES_NO_ALL_CANCEL
:
2890 if (Prompt
!= NULL
) {
2891 ShellPrintEx(-1, -1, L
"%s", Prompt
);
2894 // wait for valid response
2896 *Resp
= SHELL_PROMPT_RESPONSE_MAX
;
2897 while (*Resp
== SHELL_PROMPT_RESPONSE_MAX
) {
2898 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
2899 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
2900 ASSERT_EFI_ERROR(Status
);
2901 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
2902 switch (Key
.UnicodeChar
) {
2905 *Resp
= SHELL_PROMPT_RESPONSE_YES
;
2909 *Resp
= SHELL_PROMPT_RESPONSE_NO
;
2913 *Resp
= SHELL_PROMPT_RESPONSE_ALL
;
2917 *Resp
= SHELL_PROMPT_RESPONSE_CANCEL
;
2922 case SHELL_PROMPT_REQUEST_TYPE_ENTER_TO_COMTINUE
:
2923 case SHELL_PROMPT_REQUEST_TYPE_ANYKEY_TO_COMTINUE
:
2924 if (Prompt
!= NULL
) {
2925 ShellPrintEx(-1, -1, L
"%s", Prompt
);
2928 // wait for valid response
2930 *Resp
= SHELL_PROMPT_RESPONSE_MAX
;
2931 while (*Resp
== SHELL_PROMPT_RESPONSE_MAX
) {
2932 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
2933 if (Type
== SHELL_PROMPT_REQUEST_TYPE_ENTER_TO_COMTINUE
) {
2934 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
2935 ASSERT_EFI_ERROR(Status
);
2936 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
2937 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
2938 *Resp
= SHELL_PROMPT_RESPONSE_CONTINUE
;
2942 if (Type
== SHELL_PROMPT_REQUEST_TYPE_ANYKEY_TO_COMTINUE
) {
2943 *Resp
= SHELL_PROMPT_RESPONSE_CONTINUE
;
2948 ///@todo add more request types here!
2950 Status
= EFI_UNSUPPORTED
;
2953 if (Response
!= NULL
) {
2963 Prompt the user and return the resultant answer to the requestor.
2965 This function is the same as ShellPromptForResponse, except that the prompt is
2966 automatically pulled from HII.
2968 @param Type What type of question is asked. This is used to filter the input
2969 to prevent invalid answers to question.
2970 @param Prompt Pointer to string prompt to use to request input.
2971 @param Response Pointer to Response which will be populated upon return.
2973 @retval EFI_SUCCESS the operation was sucessful.
2974 @return other the operation failed.
2976 @sa ShellPromptForResponse
2980 ShellPromptForResponseHii (
2981 IN SHELL_PROMPT_REQUEST_TYPE Type
,
2982 IN CONST EFI_STRING_ID HiiFormatStringId
,
2983 IN CONST EFI_HANDLE HiiFormatHandle
,
2984 IN OUT VOID
**Response
2990 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
2991 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);