2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2010, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #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
)) {
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 ASSERT(PcdGet16 (PcdShellPrintBufferSize
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
146 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
147 ASSERT (mPostReplaceFormat
!= NULL
);
148 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
149 ASSERT (mPostReplaceFormat2
!= NULL
);
152 // Set the parameter count to an invalid number
154 mTotalParameterCount
= (UINTN
)(-1);
157 // UEFI 2.0 shell interfaces (used preferentially)
159 Status
= gBS
->OpenProtocol(ImageHandle
,
160 &gEfiShellProtocolGuid
,
161 (VOID
**)&mEfiShellProtocol
,
164 EFI_OPEN_PROTOCOL_GET_PROTOCOL
166 if (EFI_ERROR(Status
)) {
167 mEfiShellProtocol
= NULL
;
169 Status
= gBS
->OpenProtocol(ImageHandle
,
170 &gEfiShellParametersProtocolGuid
,
171 (VOID
**)&mEfiShellParametersProtocol
,
174 EFI_OPEN_PROTOCOL_GET_PROTOCOL
176 if (EFI_ERROR(Status
)) {
177 mEfiShellParametersProtocol
= NULL
;
180 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
182 // Moved to seperate function due to complexity
184 Status
= ShellFindSE2(ImageHandle
);
186 if (EFI_ERROR(Status
)) {
187 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
188 mEfiShellEnvironment2
= NULL
;
190 Status
= gBS
->OpenProtocol(ImageHandle
,
191 &gEfiShellInterfaceGuid
,
192 (VOID
**)&mEfiShellInterface
,
195 EFI_OPEN_PROTOCOL_GET_PROTOCOL
197 if (EFI_ERROR(Status
)) {
198 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
252 mEfiShellEnvironment2
= NULL
;
253 mEfiShellProtocol
= NULL
;
254 mEfiShellParametersProtocol
= NULL
;
255 mEfiShellInterface
= NULL
;
256 mEfiShellEnvironment2Handle
= NULL
;
257 mPostReplaceFormat
= NULL
;
258 mPostReplaceFormat2
= NULL
;
261 // verify that auto initialize is not set false
263 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
264 return (EFI_SUCCESS
);
267 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
271 Destructory for the library. free any resources.
276 IN EFI_HANDLE ImageHandle
,
277 IN EFI_SYSTEM_TABLE
*SystemTable
279 if (mEfiShellEnvironment2
!= NULL
) {
280 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
281 &gEfiShellEnvironment2Guid
,
284 mEfiShellEnvironment2
= NULL
;
286 if (mEfiShellInterface
!= NULL
) {
287 gBS
->CloseProtocol(ImageHandle
,
288 &gEfiShellInterfaceGuid
,
291 mEfiShellInterface
= NULL
;
293 if (mEfiShellProtocol
!= NULL
) {
294 gBS
->CloseProtocol(ImageHandle
,
295 &gEfiShellProtocolGuid
,
298 mEfiShellProtocol
= NULL
;
300 if (mEfiShellParametersProtocol
!= NULL
) {
301 gBS
->CloseProtocol(ImageHandle
,
302 &gEfiShellParametersProtocolGuid
,
305 mEfiShellParametersProtocol
= NULL
;
307 mEfiShellEnvironment2Handle
= NULL
;
309 if (mPostReplaceFormat
!= NULL
) {
310 FreePool(mPostReplaceFormat
);
312 if (mPostReplaceFormat2
!= NULL
) {
313 FreePool(mPostReplaceFormat2
);
315 mPostReplaceFormat
= NULL
;
316 mPostReplaceFormat2
= NULL
;
318 return (EFI_SUCCESS
);
322 This function causes the shell library to initialize itself. If the shell library
323 is already initialized it will de-initialize all the current protocol poitners and
324 re-populate them again.
326 When the library is used with PcdShellLibAutoInitialize set to true this function
327 will return EFI_SUCCESS and perform no actions.
329 This function is intended for internal access for shell commands only.
331 @retval EFI_SUCCESS the initialization was complete sucessfully
339 // if auto initialize is not false then skip
341 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
342 return (EFI_SUCCESS
);
346 // deinit the current stuff
348 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
351 // init the new stuff
353 return (ShellLibConstructorWorker(gImageHandle
, gST
));
357 This function will retrieve the information about the file for the handle
358 specified and store it in allocated pool memory.
360 This function allocates a buffer to store the file's information. It is the
361 caller's responsibility to free the buffer
363 @param FileHandle The file handle of the file for which information is
366 @retval NULL information could not be retrieved.
368 @return the information about the file
373 IN EFI_FILE_HANDLE FileHandle
375 return (FileFunctionMap
.GetFileInfo(FileHandle
));
379 This function will set the information about the file for the opened handle
382 @param FileHandle The file handle of the file for which information
385 @param FileInfo The infotmation to set.
387 @retval EFI_SUCCESS The information was set.
388 @retval EFI_UNSUPPORTED The InformationType is not known.
389 @retval EFI_NO_MEDIA The device has no medium.
390 @retval EFI_DEVICE_ERROR The device reported an error.
391 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
392 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
393 @retval EFI_ACCESS_DENIED The file was opened read only.
394 @retval EFI_VOLUME_FULL The volume is full.
399 IN EFI_FILE_HANDLE FileHandle
,
400 IN EFI_FILE_INFO
*FileInfo
402 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
406 This function will open a file or directory referenced by DevicePath.
408 This function opens a file with the open mode according to the file path. The
409 Attributes is valid only for EFI_FILE_MODE_CREATE.
411 @param FilePath on input the device path to the file. On output
412 the remaining device path.
413 @param DeviceHandle pointer to the system device handle.
414 @param FileHandle pointer to the file handle.
415 @param OpenMode the mode to open the file with.
416 @param Attributes the file's file attributes.
418 @retval EFI_SUCCESS The information was set.
419 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
420 @retval EFI_UNSUPPORTED Could not open the file path.
421 @retval EFI_NOT_FOUND The specified file could not be found on the
422 device or the file system could not be found on
424 @retval EFI_NO_MEDIA The device has no medium.
425 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
426 medium is no longer supported.
427 @retval EFI_DEVICE_ERROR The device reported an error.
428 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
429 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
430 @retval EFI_ACCESS_DENIED The file was opened read only.
431 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
433 @retval EFI_VOLUME_FULL The volume is full.
437 ShellOpenFileByDevicePath(
438 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
439 OUT EFI_HANDLE
*DeviceHandle
,
440 OUT EFI_FILE_HANDLE
*FileHandle
,
446 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
447 EFI_FILE_HANDLE LastHandle
;
450 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
452 ASSERT(FilePath
!= NULL
);
453 ASSERT(FileHandle
!= NULL
);
454 ASSERT(DeviceHandle
!= NULL
);
456 // which shell interface should we use
458 if (mEfiShellProtocol
!= NULL
) {
460 // use UEFI Shell 2.0 method.
462 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
463 if (FileName
== NULL
) {
464 return (EFI_INVALID_PARAMETER
);
466 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
473 // use old shell method.
475 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
478 if (EFI_ERROR (Status
)) {
481 Status
= gBS
->OpenProtocol(*DeviceHandle
,
482 &gEfiSimpleFileSystemProtocolGuid
,
483 (VOID
**)&EfiSimpleFileSystemProtocol
,
486 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
487 if (EFI_ERROR (Status
)) {
490 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
491 if (EFI_ERROR (Status
)) {
497 // go down directories one node at a time.
499 while (!IsDevicePathEnd (*FilePath
)) {
501 // For file system access each node should be a file path component
503 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
504 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
507 return (EFI_INVALID_PARAMETER
);
510 // Open this file path node
512 LastHandle
= *FileHandle
;
516 // Try to test opening an existing file
518 Status
= LastHandle
->Open (
521 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
522 OpenMode
&~EFI_FILE_MODE_CREATE
,
527 // see if the error was that it needs to be created
529 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
530 Status
= LastHandle
->Open (
533 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
539 // Close the last node
541 LastHandle
->Close (LastHandle
);
543 if (EFI_ERROR(Status
)) {
550 *FilePath
= NextDevicePathNode (*FilePath
);
552 return (EFI_SUCCESS
);
556 This function will open a file or directory referenced by filename.
558 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
559 otherwise, the Filehandle is NULL. The Attributes is valid only for
560 EFI_FILE_MODE_CREATE.
562 if FileNAme is NULL then ASSERT()
564 @param FileName pointer to file name
565 @param FileHandle pointer to the file handle.
566 @param OpenMode the mode to open the file with.
567 @param Attributes the file's file attributes.
569 @retval EFI_SUCCESS The information was set.
570 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
571 @retval EFI_UNSUPPORTED Could not open the file path.
572 @retval EFI_NOT_FOUND The specified file could not be found on the
573 device or the file system could not be found
575 @retval EFI_NO_MEDIA The device has no medium.
576 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
577 medium is no longer supported.
578 @retval EFI_DEVICE_ERROR The device reported an error.
579 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
580 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
581 @retval EFI_ACCESS_DENIED The file was opened read only.
582 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
584 @retval EFI_VOLUME_FULL The volume is full.
589 IN CONST CHAR16
*FileName
,
590 OUT EFI_FILE_HANDLE
*FileHandle
,
594 EFI_HANDLE DeviceHandle
;
595 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
597 EFI_FILE_INFO
*FileInfo
;
600 // ASSERT if FileName is NULL
602 ASSERT(FileName
!= NULL
);
604 if (mEfiShellProtocol
!= NULL
) {
606 // Use UEFI Shell 2.0 method
608 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
611 if (!EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
612 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
613 ASSERT(FileInfo
!= NULL
);
614 FileInfo
->Attribute
= Attributes
;
615 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
621 // Using EFI Shell version
622 // this means convert name to path and call that function
623 // since this will use EFI method again that will open it.
625 ASSERT(mEfiShellEnvironment2
!= NULL
);
626 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
627 if (FileDevicePath
!= NULL
) {
628 return (ShellOpenFileByDevicePath(&FilePath
,
634 return (EFI_DEVICE_ERROR
);
637 This function create a directory
639 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
640 otherwise, the Filehandle is NULL. If the directory already existed, this
641 function opens the existing directory.
643 @param DirectoryName pointer to directory name
644 @param FileHandle pointer to the file handle.
646 @retval EFI_SUCCESS The information was set.
647 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
648 @retval EFI_UNSUPPORTED Could not open the file path.
649 @retval EFI_NOT_FOUND The specified file could not be found on the
650 device or the file system could not be found
652 @retval EFI_NO_MEDIA The device has no medium.
653 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
654 medium is no longer supported.
655 @retval EFI_DEVICE_ERROR The device reported an error.
656 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
657 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
658 @retval EFI_ACCESS_DENIED The file was opened read only.
659 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
661 @retval EFI_VOLUME_FULL The volume is full.
662 @sa ShellOpenFileByName
666 ShellCreateDirectory(
667 IN CONST CHAR16
*DirectoryName
,
668 OUT EFI_FILE_HANDLE
*FileHandle
670 if (mEfiShellProtocol
!= NULL
) {
672 // Use UEFI Shell 2.0 method
674 return (mEfiShellProtocol
->CreateFile(DirectoryName
,
679 return (ShellOpenFileByName(DirectoryName
,
681 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
688 This function reads information from an opened file.
690 If FileHandle is not a directory, the function reads the requested number of
691 bytes from the file at the file's current position and returns them in Buffer.
692 If the read goes beyond the end of the file, the read length is truncated to the
693 end of the file. The file's current position is increased by the number of bytes
694 returned. If FileHandle is a directory, the function reads the directory entry
695 at the file's current position and returns the entry in Buffer. If the Buffer
696 is not large enough to hold the current directory entry, then
697 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
698 BufferSize is set to be the size of the buffer needed to read the entry. On
699 success, the current position is updated to the next directory entry. If there
700 are no more directory entries, the read returns a zero-length buffer.
701 EFI_FILE_INFO is the structure returned as the directory entry.
703 @param FileHandle the opened file handle
704 @param BufferSize on input the size of buffer in bytes. on return
705 the number of bytes written.
706 @param Buffer the buffer to put read data into.
708 @retval EFI_SUCCESS Data was read.
709 @retval EFI_NO_MEDIA The device has no media.
710 @retval EFI_DEVICE_ERROR The device reported an error.
711 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
712 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
719 IN EFI_FILE_HANDLE FileHandle
,
720 IN OUT UINTN
*BufferSize
,
723 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
728 Write data to a file.
730 This function writes the specified number of bytes to the file at the current
731 file position. The current file position is advanced the actual number of bytes
732 written, which is returned in BufferSize. Partial writes only occur when there
733 has been a data error during the write attempt (such as "volume space full").
734 The file is automatically grown to hold the data if required. Direct writes to
735 opened directories are not supported.
737 @param FileHandle The opened file for writing
738 @param BufferSize on input the number of bytes in Buffer. On output
739 the number of bytes written.
740 @param Buffer the buffer containing data to write is stored.
742 @retval EFI_SUCCESS Data was written.
743 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
744 @retval EFI_NO_MEDIA The device has no media.
745 @retval EFI_DEVICE_ERROR The device reported an error.
746 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
747 @retval EFI_WRITE_PROTECTED The device is write-protected.
748 @retval EFI_ACCESS_DENIED The file was open for read only.
749 @retval EFI_VOLUME_FULL The volume is full.
754 IN EFI_FILE_HANDLE FileHandle
,
755 IN OUT UINTN
*BufferSize
,
758 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
762 Close an open file handle.
764 This function closes a specified file handle. All "dirty" cached file data is
765 flushed to the device, and the file is closed. In all cases the handle is
768 @param FileHandle the file handle to close.
770 @retval EFI_SUCCESS the file handle was closed sucessfully.
775 IN EFI_FILE_HANDLE
*FileHandle
777 return (FileFunctionMap
.CloseFile(*FileHandle
));
781 Delete a file and close the handle
783 This function closes and deletes a file. In all cases the file handle is closed.
784 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
785 returned, but the handle is still closed.
787 @param FileHandle the file handle to delete
789 @retval EFI_SUCCESS the file was closed sucessfully
790 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
792 @retval INVALID_PARAMETER One of the parameters has an invalid value.
797 IN EFI_FILE_HANDLE
*FileHandle
799 return (FileFunctionMap
.DeleteFile(*FileHandle
));
803 Set the current position in a file.
805 This function sets the current file position for the handle to the position
806 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
807 absolute positioning is supported, and seeking past the end of the file is
808 allowed (a subsequent write would grow the file). Seeking to position
809 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
810 If FileHandle is a directory, the only position that may be set is zero. This
811 has the effect of starting the read process of the directory entries over.
813 @param FileHandle The file handle on which the position is being set
814 @param Position Byte position from begining of file
816 @retval EFI_SUCCESS Operation completed sucessfully.
817 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
819 @retval INVALID_PARAMETER One of the parameters has an invalid value.
823 ShellSetFilePosition (
824 IN EFI_FILE_HANDLE FileHandle
,
827 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
831 Gets a file's current position
833 This function retrieves the current file position for the file handle. For
834 directories, the current file position has no meaning outside of the file
835 system driver and as such the operation is not supported. An error is returned
836 if FileHandle is a directory.
838 @param FileHandle The open file handle on which to get the position.
839 @param Position Byte position from begining of file.
841 @retval EFI_SUCCESS the operation completed sucessfully.
842 @retval INVALID_PARAMETER One of the parameters has an invalid value.
843 @retval EFI_UNSUPPORTED the request is not valid on directories.
847 ShellGetFilePosition (
848 IN EFI_FILE_HANDLE FileHandle
,
851 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
854 Flushes data on a file
856 This function flushes all modified data associated with a file to a device.
858 @param FileHandle The file handle on which to flush data
860 @retval EFI_SUCCESS The data was flushed.
861 @retval EFI_NO_MEDIA The device has no media.
862 @retval EFI_DEVICE_ERROR The device reported an error.
863 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
864 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
865 @retval EFI_ACCESS_DENIED The file was opened for read only.
870 IN EFI_FILE_HANDLE FileHandle
872 return (FileFunctionMap
.FlushFile(FileHandle
));
876 Retrieves the first file from a directory
878 This function opens a directory and gets the first file's info in the
879 directory. Caller can use ShellFindNextFile() to get other files. When
880 complete the caller is responsible for calling FreePool() on Buffer.
882 @param DirHandle The file handle of the directory to search
883 @param Buffer Pointer to buffer for file's information
885 @retval EFI_SUCCESS Found the first file.
886 @retval EFI_NOT_FOUND Cannot find the directory.
887 @retval EFI_NO_MEDIA The device has no media.
888 @retval EFI_DEVICE_ERROR The device reported an error.
889 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
890 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
896 IN EFI_FILE_HANDLE DirHandle
,
897 OUT EFI_FILE_INFO
**Buffer
900 // pass to file handle lib
902 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
905 Retrieves the next file in a directory.
907 To use this function, caller must call the LibFindFirstFile() to get the
908 first file, and then use this function get other files. This function can be
909 called for several times to get each file's information in the directory. If
910 the call of ShellFindNextFile() got the last file in the directory, the next
911 call of this function has no file to get. *NoFile will be set to TRUE and the
912 Buffer memory will be automatically freed.
914 @param DirHandle the file handle of the directory
915 @param Buffer pointer to buffer for file's information
916 @param NoFile pointer to boolean when last file is found
918 @retval EFI_SUCCESS Found the next file, or reached last file
919 @retval EFI_NO_MEDIA The device has no media.
920 @retval EFI_DEVICE_ERROR The device reported an error.
921 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
926 IN EFI_FILE_HANDLE DirHandle
,
927 OUT EFI_FILE_INFO
*Buffer
,
931 // pass to file handle lib
933 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
936 Retrieve the size of a file.
938 if FileHandle is NULL then ASSERT()
939 if Size is NULL then ASSERT()
941 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
944 @param FileHandle file handle from which size is retrieved
945 @param Size pointer to size
947 @retval EFI_SUCCESS operation was completed sucessfully
948 @retval EFI_DEVICE_ERROR cannot access the file
953 IN EFI_FILE_HANDLE FileHandle
,
956 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
959 Retrieves the status of the break execution flag
961 this function is useful to check whether the application is being asked to halt by the shell.
963 @retval TRUE the execution break is enabled
964 @retval FALSE the execution break is not enabled
968 ShellGetExecutionBreakFlag(
973 // Check for UEFI Shell 2.0 protocols
975 if (mEfiShellProtocol
!= NULL
) {
978 // We are using UEFI Shell 2.0; see if the event has been triggered
980 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
987 // using EFI Shell; call the function to check
989 ASSERT(mEfiShellEnvironment2
!= NULL
);
990 return (mEfiShellEnvironment2
->GetExecutionBreak());
993 return the value of an environment variable
995 this function gets the value of the environment variable set by the
996 ShellSetEnvironmentVariable function
998 @param EnvKey The key name of the environment variable.
1000 @retval NULL the named environment variable does not exist.
1001 @return != NULL pointer to the value of the environment variable
1005 ShellGetEnvironmentVariable (
1006 IN CONST CHAR16
*EnvKey
1010 // Check for UEFI Shell 2.0 protocols
1012 if (mEfiShellProtocol
!= NULL
) {
1013 return (mEfiShellProtocol
->GetEnv(EnvKey
));
1017 // ASSERT that we must have EFI shell
1019 ASSERT(mEfiShellEnvironment2
!= NULL
);
1024 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1027 set the value of an environment variable
1029 This function changes the current value of the specified environment variable. If the
1030 environment variable exists and the Value is an empty string, then the environment
1031 variable is deleted. If the environment variable exists and the Value is not an empty
1032 string, then the value of the environment variable is changed. If the environment
1033 variable does not exist and the Value is an empty string, there is no action. If the
1034 environment variable does not exist and the Value is a non-empty string, then the
1035 environment variable is created and assigned the specified value.
1037 This is not supported pre-UEFI Shell 2.0.
1039 @param EnvKey The key name of the environment variable.
1040 @param EnvVal The Value of the environment variable
1041 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1043 @retval EFI_SUCCESS the operation was completed sucessfully
1044 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1048 ShellSetEnvironmentVariable (
1049 IN CONST CHAR16
*EnvKey
,
1050 IN CONST CHAR16
*EnvVal
,
1055 // Check for UEFI Shell 2.0 protocols
1057 if (mEfiShellProtocol
!= NULL
) {
1058 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1062 // This feature does not exist under EFI shell
1064 return (EFI_UNSUPPORTED
);
1067 cause the shell to parse and execute a command line.
1069 This function creates a nested instance of the shell and executes the specified
1070 command (CommandLine) with the specified environment (Environment). Upon return,
1071 the status code returned by the specified command is placed in StatusCode.
1072 If Environment is NULL, then the current environment is used and all changes made
1073 by the commands executed will be reflected in the current environment. If the
1074 Environment is non-NULL, then the changes made will be discarded.
1075 The CommandLine is executed from the current working directory on the current
1078 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1079 Output is only supported for pre-UEFI Shell 2.0
1081 @param ImageHandle Parent image that is starting the operation
1082 @param CommandLine pointer to null terminated command line.
1083 @param Output true to display debug output. false to hide it.
1084 @param EnvironmentVariables optional pointer to array of environment variables
1085 in the form "x=y". if NULL current set is used.
1086 @param Status the status of the run command line.
1088 @retval EFI_SUCCESS the operation completed sucessfully. Status
1089 contains the status code returned.
1090 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1091 @retval EFI_OUT_OF_RESOURCES out of resources
1092 @retval EFI_UNSUPPORTED the operation is not allowed.
1097 IN EFI_HANDLE
*ParentHandle
,
1098 IN CHAR16
*CommandLine OPTIONAL
,
1099 IN BOOLEAN Output OPTIONAL
,
1100 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1101 OUT EFI_STATUS
*Status OPTIONAL
1105 // Check for UEFI Shell 2.0 protocols
1107 if (mEfiShellProtocol
!= NULL
) {
1109 // Call UEFI Shell 2.0 version (not using Output parameter)
1111 return (mEfiShellProtocol
->Execute(ParentHandle
,
1113 EnvironmentVariables
,
1117 // ASSERT that we must have EFI shell
1119 ASSERT(mEfiShellEnvironment2
!= NULL
);
1121 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1122 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1124 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1129 Retreives the current directory path
1131 If the DeviceName is NULL, it returns the current device's current directory
1132 name. If the DeviceName is not NULL, it returns the current directory name
1135 @param DeviceName the name of the drive to get directory on
1137 @retval NULL the directory does not exist
1138 @return != NULL the directory
1142 ShellGetCurrentDir (
1143 IN CHAR16
*DeviceName OPTIONAL
1147 // Check for UEFI Shell 2.0 protocols
1149 if (mEfiShellProtocol
!= NULL
) {
1150 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1153 // ASSERT that we must have EFI shell
1155 ASSERT(mEfiShellEnvironment2
!= NULL
);
1156 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1159 sets (enabled or disabled) the page break mode
1161 when page break mode is enabled the screen will stop scrolling
1162 and wait for operator input before scrolling a subsequent screen.
1164 @param CurrentState TRUE to enable and FALSE to disable
1168 ShellSetPageBreakMode (
1169 IN BOOLEAN CurrentState
1173 // check for enabling
1175 if (CurrentState
!= 0x00) {
1177 // check for UEFI Shell 2.0
1179 if (mEfiShellProtocol
!= NULL
) {
1181 // Enable with UEFI 2.0 Shell
1183 mEfiShellProtocol
->EnablePageBreak();
1187 // ASSERT that must have EFI Shell
1189 ASSERT(mEfiShellEnvironment2
!= NULL
);
1191 // Enable with EFI Shell
1193 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1198 // check for UEFI Shell 2.0
1200 if (mEfiShellProtocol
!= NULL
) {
1202 // Disable with UEFI 2.0 Shell
1204 mEfiShellProtocol
->DisablePageBreak();
1208 // ASSERT that must have EFI Shell
1210 ASSERT(mEfiShellEnvironment2
!= NULL
);
1212 // Disable with EFI Shell
1214 mEfiShellEnvironment2
->DisablePageBreak ();
1221 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1222 /// This allows for the struct to be populated.
1229 EFI_FILE_HANDLE Handle
;
1230 EFI_FILE_INFO
*Info
;
1231 } EFI_SHELL_FILE_INFO_NO_CONST
;
1234 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1236 if OldStyleFileList is NULL then ASSERT()
1238 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1239 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1240 the ShellCloseFileMetaArg function.
1242 @param[in] FileList the EFI shell list type
1243 @param[in,out] ListHead the list to add to
1245 @retval the resultant head of the double linked new format list;
1249 InternalShellConvertFileListType (
1250 IN LIST_ENTRY
*FileList
,
1251 IN OUT LIST_ENTRY
*ListHead
1254 SHELL_FILE_ARG
*OldInfo
;
1256 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1261 ASSERT(FileList
!= NULL
);
1262 ASSERT(ListHead
!= NULL
);
1265 // enumerate through each member of the old list and copy
1267 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1268 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1271 // make sure the old list was valid
1273 ASSERT(OldInfo
!= NULL
);
1274 ASSERT(OldInfo
->Info
!= NULL
);
1275 ASSERT(OldInfo
->FullName
!= NULL
);
1276 ASSERT(OldInfo
->FileName
!= NULL
);
1279 // allocate a new EFI_SHELL_FILE_INFO object
1281 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1284 // copy the simple items
1286 NewInfo
->Handle
= OldInfo
->Handle
;
1287 NewInfo
->Status
= OldInfo
->Status
;
1289 // old shell checks for 0 not NULL
1290 OldInfo
->Handle
= 0;
1293 // allocate new space to copy strings and structure
1295 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1296 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1297 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1300 // make sure all the memory allocations were sucessful
1302 ASSERT(NewInfo
->FullName
!= NULL
);
1303 ASSERT(NewInfo
->FileName
!= NULL
);
1304 ASSERT(NewInfo
->Info
!= NULL
);
1307 // Copt the strings and structure
1309 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1310 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1311 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1314 // add that to the list
1316 InsertTailList(ListHead
, &NewInfo
->Link
);
1321 Opens a group of files based on a path.
1323 This function uses the Arg to open all the matching files. Each matched
1324 file has a SHELL_FILE_ARG structure to record the file information. These
1325 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1326 structures from ListHead to access each file. This function supports wildcards
1327 and will process '?' and '*' as such. the list must be freed with a call to
1328 ShellCloseFileMetaArg().
1330 If you are NOT appending to an existing list *ListHead must be NULL. If
1331 *ListHead is NULL then it must be callee freed.
1333 @param Arg pointer to path string
1334 @param OpenMode mode to open files with
1335 @param ListHead head of linked list of results
1337 @retval EFI_SUCCESS the operation was sucessful and the list head
1338 contains the list of opened files
1339 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1340 *ListHead is set to NULL.
1341 @return != EFI_SUCCESS the operation failed
1343 @sa InternalShellConvertFileListType
1347 ShellOpenFileMetaArg (
1350 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1354 LIST_ENTRY mOldStyleFileList
;
1357 // ASSERT that Arg and ListHead are not NULL
1359 ASSERT(Arg
!= NULL
);
1360 ASSERT(ListHead
!= NULL
);
1363 // Check for UEFI Shell 2.0 protocols
1365 if (mEfiShellProtocol
!= NULL
) {
1366 if (*ListHead
== NULL
) {
1367 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1368 if (*ListHead
== NULL
) {
1369 return (EFI_OUT_OF_RESOURCES
);
1371 InitializeListHead(&((*ListHead
)->Link
));
1373 Status
= mEfiShellProtocol
->OpenFileList(Arg
,
1376 if (EFI_ERROR(Status
)) {
1377 mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1379 Status
= mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1385 // ASSERT that we must have EFI shell
1387 ASSERT(mEfiShellEnvironment2
!= NULL
);
1390 // make sure the list head is initialized
1392 InitializeListHead(&mOldStyleFileList
);
1395 // Get the EFI Shell list of files
1397 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1398 if (EFI_ERROR(Status
)) {
1403 if (*ListHead
== NULL
) {
1404 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1405 if (*ListHead
== NULL
) {
1406 return (EFI_OUT_OF_RESOURCES
);
1411 // Convert that to equivalent of UEFI Shell 2.0 structure
1413 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1416 // Free the EFI Shell version that was converted.
1418 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1423 Free the linked list returned from ShellOpenFileMetaArg
1425 if ListHead is NULL then ASSERT()
1427 @param ListHead the pointer to free
1429 @retval EFI_SUCCESS the operation was sucessful
1433 ShellCloseFileMetaArg (
1434 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1440 // ASSERT that ListHead is not NULL
1442 ASSERT(ListHead
!= NULL
);
1445 // Check for UEFI Shell 2.0 protocols
1447 if (mEfiShellProtocol
!= NULL
) {
1448 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1451 // Since this is EFI Shell version we need to free our internally made copy
1454 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1455 ; IsListEmpty(&(*ListHead
)->Link
) == FALSE
1456 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1457 RemoveEntryList(Node
);
1458 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1459 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1460 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1461 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1462 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1469 Find a file by searching the CWD and then the path.
1471 If FileName is NULL then ASSERT.
1473 If the return value is not NULL then the memory must be caller freed.
1475 @param FileName Filename string.
1477 @retval NULL the file was not found
1478 @return !NULL the full path to the file.
1483 IN CONST CHAR16
*FileName
1487 EFI_FILE_HANDLE Handle
;
1491 CONST CHAR16
*Walker
;
1496 Path
= ShellGetEnvironmentVariable(L
"cwd");
1498 Size
= StrSize(Path
);
1499 Size
+= StrSize(FileName
);
1500 TestPath
= AllocateZeroPool(Size
);
1501 StrCpy(TestPath
, Path
);
1502 StrCat(TestPath
, FileName
);
1503 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1504 if (!EFI_ERROR(Status
)){
1505 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1506 ShellCloseFile(&Handle
);
1512 Path
= ShellGetEnvironmentVariable(L
"path");
1514 Size
= StrSize(Path
);
1515 Size
+= StrSize(FileName
);
1516 TestPath
= AllocateZeroPool(Size
);
1517 Walker
= (CHAR16
*)Path
;
1519 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1520 if (StrStr(TestPath
, L
";") != NULL
) {
1521 *(StrStr(TestPath
, L
";")) = CHAR_NULL
;
1523 StrCat(TestPath
, FileName
);
1524 if (StrStr(Walker
, L
";") != NULL
) {
1525 Walker
= StrStr(Walker
, L
";") + 1;
1529 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1530 if (!EFI_ERROR(Status
)){
1531 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1532 ShellCloseFile(&Handle
);
1535 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1542 Find a file by searching the CWD and then the path with a variable set of file
1543 extensions. If the file is not found it will append each extension in the list
1544 in the order provided and return the first one that is successful.
1546 If FileName is NULL, then ASSERT.
1547 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1549 If the return value is not NULL then the memory must be caller freed.
1551 @param[in] FileName Filename string.
1552 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1554 @retval NULL The file was not found.
1555 @retval !NULL The path to the file.
1559 ShellFindFilePathEx (
1560 IN CONST CHAR16
*FileName
,
1561 IN CONST CHAR16
*FileExtension
1566 CONST CHAR16
*ExtensionWalker
;
1567 ASSERT(FileName
!= NULL
);
1568 if (FileExtension
== NULL
) {
1569 return (ShellFindFilePath(FileName
));
1571 RetVal
= ShellFindFilePath(FileName
);
1572 if (RetVal
!= NULL
) {
1575 TestPath
= AllocateZeroPool(StrSize(FileName
) + StrSize(FileExtension
));
1576 for (ExtensionWalker
= FileExtension
; ; ExtensionWalker
= StrStr(ExtensionWalker
, L
";") + 1 ){
1577 StrCpy(TestPath
, FileName
);
1578 StrCat(TestPath
, ExtensionWalker
);
1579 if (StrStr(TestPath
, L
";") != NULL
) {
1580 *(StrStr(TestPath
, L
";")) = CHAR_NULL
;
1582 RetVal
= ShellFindFilePath(TestPath
);
1583 if (RetVal
!= NULL
) {
1587 // Must be after first loop...
1589 if (StrStr(ExtensionWalker
, L
";") == NULL
) {
1602 UINTN OriginalPosition
;
1603 } SHELL_PARAM_PACKAGE
;
1606 Checks the list of valid arguments and returns TRUE if the item was found. If the
1607 return value is TRUE then the type parameter is set also.
1609 if CheckList is NULL then ASSERT();
1610 if Name is NULL then ASSERT();
1611 if Type is NULL then ASSERT();
1613 @param Type pointer to type of parameter if it was found
1614 @param Name pointer to Name of parameter found
1615 @param CheckList List to check against
1617 @retval TRUE the Parameter was found. Type is valid.
1618 @retval FALSE the Parameter was not found. Type is not valid.
1622 InternalIsOnCheckList (
1623 IN CONST CHAR16
*Name
,
1624 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1627 SHELL_PARAM_ITEM
*TempListItem
;
1630 // ASSERT that all 3 pointer parameters aren't NULL
1632 ASSERT(CheckList
!= NULL
);
1633 ASSERT(Type
!= NULL
);
1634 ASSERT(Name
!= NULL
);
1637 // question mark and page break mode are always supported
1639 if ((StrCmp(Name
, L
"-?") == 0) ||
1640 (StrCmp(Name
, L
"-b") == 0)
1646 // Enumerate through the list
1648 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1650 // If the Type is TypeStart only check the first characters of the passed in param
1651 // If it matches set the type and return TRUE
1653 if (TempListItem
->Type
== TypeStart
&& StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1654 *Type
= TempListItem
->Type
;
1656 } else if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1657 *Type
= TempListItem
->Type
;
1665 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1667 @param Name pointer to Name of parameter found
1669 @retval TRUE the Parameter is a flag.
1670 @retval FALSE the Parameter not a flag
1675 IN CONST CHAR16
*Name
,
1676 IN BOOLEAN AlwaysAllowNumbers
1680 // ASSERT that Name isn't NULL
1682 ASSERT(Name
!= NULL
);
1685 // If we accept numbers then dont return TRUE. (they will be values)
1687 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
!= FALSE
) {
1692 // If the Name has a / or - as the first character return TRUE
1694 if ((Name
[0] == L
'/') ||
1695 (Name
[0] == L
'-') ||
1704 Checks the command line arguments passed against the list of valid ones.
1706 If no initialization is required, then return RETURN_SUCCESS.
1708 @param CheckList pointer to list of parameters to check
1709 @param CheckPackage pointer to pointer to list checked values
1710 @param ProblemParam optional pointer to pointer to unicode string for
1711 the paramater that caused failure. If used then the
1712 caller is responsible for freeing the memory.
1713 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1714 @param Argc Count of parameters in Argv
1715 @param Argv pointer to array of parameters
1717 @retval EFI_SUCCESS The operation completed sucessfully.
1718 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1719 @retval EFI_INVALID_PARAMETER A parameter was invalid
1720 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1721 duplicated. the duplicated command line argument
1722 was returned in ProblemParam if provided.
1723 @retval EFI_NOT_FOUND a argument required a value that was missing.
1724 the invalid command line argument was returned in
1725 ProblemParam if provided.
1730 InternalCommandLineParse (
1731 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1732 OUT LIST_ENTRY
**CheckPackage
,
1733 OUT CHAR16
**ProblemParam OPTIONAL
,
1734 IN BOOLEAN AutoPageBreak
,
1735 IN CONST CHAR16
**Argv
,
1737 IN BOOLEAN AlwaysAllowNumbers
1740 ParamType CurrentItemType
;
1741 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1745 CurrentItemPackage
= NULL
;
1746 mTotalParameterCount
= 0;
1751 // If there is only 1 item we dont need to do anything
1754 *CheckPackage
= NULL
;
1755 return (EFI_SUCCESS
);
1761 ASSERT(CheckList
!= NULL
);
1762 ASSERT(Argv
!= NULL
);
1765 // initialize the linked list
1767 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1768 InitializeListHead(*CheckPackage
);
1771 // loop through each of the arguments
1773 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1774 if (Argv
[LoopCounter
] == NULL
) {
1776 // do nothing for NULL argv
1778 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) != FALSE
) {
1780 // We might have leftover if last parameter didnt have optional value
1782 if (GetItemValue
!= 0) {
1784 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1789 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1790 ASSERT(CurrentItemPackage
!= NULL
);
1791 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1792 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1793 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1794 CurrentItemPackage
->Type
= CurrentItemType
;
1795 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1796 CurrentItemPackage
->Value
= NULL
;
1799 // Does this flag require a value
1801 switch (CurrentItemPackage
->Type
) {
1803 // possibly trigger the next loop(s) to populate the value of this item
1809 case TypeDoubleValue
:
1814 GetItemValue
= (UINTN
)(-1);
1819 // this item has no value expected; we are done
1821 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1822 ASSERT(GetItemValue
== 0);
1825 } else if (GetItemValue
!= 0 && InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) == FALSE
) {
1826 ASSERT(CurrentItemPackage
!= NULL
);
1828 // get the item VALUE for a previous flag
1830 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
1831 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1832 if (ValueSize
== 0) {
1833 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1835 StrCat(CurrentItemPackage
->Value
, L
" ");
1836 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1838 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
1840 if (GetItemValue
== 0) {
1841 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1843 } else if (InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) == FALSE
) {
1845 // add this one as a non-flag
1847 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1848 ASSERT(CurrentItemPackage
!= NULL
);
1849 CurrentItemPackage
->Name
= NULL
;
1850 CurrentItemPackage
->Type
= TypePosition
;
1851 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1852 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1853 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1854 CurrentItemPackage
->OriginalPosition
= mTotalParameterCount
++;
1855 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1856 } else if (ProblemParam
) {
1858 // this was a non-recognised flag... error!
1860 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1861 ASSERT(*ProblemParam
!= NULL
);
1862 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1863 ShellCommandLineFreeVarList(*CheckPackage
);
1864 *CheckPackage
= NULL
;
1865 return (EFI_VOLUME_CORRUPTED
);
1867 ShellCommandLineFreeVarList(*CheckPackage
);
1868 *CheckPackage
= NULL
;
1869 return (EFI_VOLUME_CORRUPTED
);
1872 if (GetItemValue
!= 0) {
1874 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1877 // support for AutoPageBreak
1879 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1880 ShellSetPageBreakMode(TRUE
);
1882 return (EFI_SUCCESS
);
1886 Checks the command line arguments passed against the list of valid ones.
1887 Optionally removes NULL values first.
1889 If no initialization is required, then return RETURN_SUCCESS.
1891 @param CheckList pointer to list of parameters to check
1892 @param CheckPackage pointer to pointer to list checked values
1893 @param ProblemParam optional pointer to pointer to unicode string for
1894 the paramater that caused failure.
1895 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1897 @retval EFI_SUCCESS The operation completed sucessfully.
1898 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1899 @retval EFI_INVALID_PARAMETER A parameter was invalid
1900 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1901 duplicated. the duplicated command line argument
1902 was returned in ProblemParam if provided.
1903 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1904 of the command line arguments was returned in
1905 ProblemParam if provided.
1906 @retval EFI_NOT_FOUND a argument required a value that was missing.
1907 the invalid command line argument was returned in
1908 ProblemParam if provided.
1912 ShellCommandLineParseEx (
1913 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1914 OUT LIST_ENTRY
**CheckPackage
,
1915 OUT CHAR16
**ProblemParam OPTIONAL
,
1916 IN BOOLEAN AutoPageBreak
,
1917 IN BOOLEAN AlwaysAllowNumbers
1920 // ASSERT that CheckList and CheckPackage aren't NULL
1922 ASSERT(CheckList
!= NULL
);
1923 ASSERT(CheckPackage
!= NULL
);
1926 // Check for UEFI Shell 2.0 protocols
1928 if (mEfiShellParametersProtocol
!= NULL
) {
1929 return (InternalCommandLineParse(CheckList
,
1933 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
1934 mEfiShellParametersProtocol
->Argc
,
1935 AlwaysAllowNumbers
));
1939 // ASSERT That EFI Shell is not required
1941 ASSERT (mEfiShellInterface
!= NULL
);
1942 return (InternalCommandLineParse(CheckList
,
1946 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
1947 mEfiShellInterface
->Argc
,
1948 AlwaysAllowNumbers
));
1952 Frees shell variable list that was returned from ShellCommandLineParse.
1954 This function will free all the memory that was used for the CheckPackage
1955 list of postprocessed shell arguments.
1957 this function has no return value.
1959 if CheckPackage is NULL, then return
1961 @param CheckPackage the list to de-allocate
1965 ShellCommandLineFreeVarList (
1966 IN LIST_ENTRY
*CheckPackage
1971 // check for CheckPackage == NULL
1973 if (CheckPackage
== NULL
) {
1978 // for each node in the list
1980 for ( Node
= GetFirstNode(CheckPackage
)
1981 ; IsListEmpty(CheckPackage
) == FALSE
1982 ; Node
= GetFirstNode(CheckPackage
)
1985 // Remove it from the list
1987 RemoveEntryList(Node
);
1990 // if it has a name free the name
1992 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1993 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1997 // if it has a value free the value
1999 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2000 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2004 // free the node structure
2006 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2009 // free the list head node
2011 FreePool(CheckPackage
);
2014 Checks for presence of a flag parameter
2016 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2018 if CheckPackage is NULL then return FALSE.
2019 if KeyString is NULL then ASSERT()
2021 @param CheckPackage The package of parsed command line arguments
2022 @param KeyString the Key of the command line argument to check for
2024 @retval TRUE the flag is on the command line
2025 @retval FALSE the flag is not on the command line
2029 ShellCommandLineGetFlag (
2030 IN CONST LIST_ENTRY
*CheckPackage
,
2031 IN CHAR16
*KeyString
2036 // ASSERT that both CheckPackage and KeyString aren't NULL
2038 ASSERT(KeyString
!= NULL
);
2041 // return FALSE for no package
2043 if (CheckPackage
== NULL
) {
2048 // enumerate through the list of parametrs
2050 for ( Node
= GetFirstNode(CheckPackage
)
2051 ; !IsNull (CheckPackage
, Node
)
2052 ; Node
= GetNextNode(CheckPackage
, Node
)
2055 // If the Name matches, return TRUE (and there may be NULL name)
2057 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2059 // If Type is TypeStart then only compare the begining of the strings
2061 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2062 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2065 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2073 returns value from command line argument
2075 value parameters are in the form of "-<Key> value" or "/<Key> value"
2077 if CheckPackage is NULL, then return NULL;
2079 @param CheckPackage The package of parsed command line arguments
2080 @param KeyString the Key of the command line argument to check for
2082 @retval NULL the flag is not on the command line
2083 @return !=NULL pointer to unicode string of the value
2087 ShellCommandLineGetValue (
2088 IN CONST LIST_ENTRY
*CheckPackage
,
2089 IN CHAR16
*KeyString
2094 // check for CheckPackage == NULL
2096 if (CheckPackage
== NULL
) {
2101 // enumerate through the list of parametrs
2103 for ( Node
= GetFirstNode(CheckPackage
)
2104 ; !IsNull (CheckPackage
, Node
)
2105 ; Node
= GetNextNode(CheckPackage
, Node
)
2108 // If the Name matches, return the value (name can be NULL)
2110 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2112 // If Type is TypeStart then only compare the begining of the strings
2114 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2115 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2118 // return the string part after the flag
2120 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2121 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2125 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2132 returns raw value from command line argument
2134 raw value parameters are in the form of "value" in a specific position in the list
2136 if CheckPackage is NULL, then return NULL;
2138 @param CheckPackage The package of parsed command line arguments
2139 @param Position the position of the value
2141 @retval NULL the flag is not on the command line
2142 @return !=NULL pointer to unicode string of the value
2146 ShellCommandLineGetRawValue (
2147 IN CONST LIST_ENTRY
*CheckPackage
,
2153 // check for CheckPackage == NULL
2155 if (CheckPackage
== NULL
) {
2160 // enumerate through the list of parametrs
2162 for ( Node
= GetFirstNode(CheckPackage
)
2163 ; !IsNull (CheckPackage
, Node
)
2164 ; Node
= GetNextNode(CheckPackage
, Node
)
2167 // If the position matches, return the value
2169 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2170 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2177 returns the number of command line value parameters that were parsed.
2179 this will not include flags.
2181 @retval (UINTN)-1 No parsing has ocurred
2182 @return other The number of value parameters found
2186 ShellCommandLineGetCount(
2190 return (mTotalParameterCount
);
2194 Determins if a parameter is duplicated.
2196 If Param is not NULL then it will point to a callee allocated string buffer
2197 with the parameter value if a duplicate is found.
2199 If CheckPackage is NULL, then ASSERT.
2201 @param[in] CheckPackage The package of parsed command line arguments.
2202 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2204 @retval EFI_SUCCESS No parameters were duplicated.
2205 @retval EFI_DEVICE_ERROR A duplicate was found.
2209 ShellCommandLineCheckDuplicate (
2210 IN CONST LIST_ENTRY
*CheckPackage
,
2217 ASSERT(CheckPackage
!= NULL
);
2219 for ( Node1
= GetFirstNode(CheckPackage
)
2220 ; !IsNull (CheckPackage
, Node1
)
2221 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2223 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2224 ; !IsNull (CheckPackage
, Node2
)
2225 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2227 if (StrCmp(((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, ((SHELL_PARAM_PACKAGE
*)Node2
)->Name
) == 0) {
2228 if (Param
!= NULL
) {
2230 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2232 return (EFI_DEVICE_ERROR
);
2236 return (EFI_SUCCESS
);
2240 This is a find and replace function. Upon successful return the NewString is a copy of
2241 SourceString with each instance of FindTarget replaced with ReplaceWith.
2243 If SourceString and NewString overlap the behavior is undefined.
2245 If the string would grow bigger than NewSize it will halt and return error.
2247 @param[in] SourceString String with source buffer
2248 @param[in,out] NewString String with resultant buffer
2249 @param[in] NewSize Size in bytes of NewString
2250 @param[in] FindTarget String to look for
2251 @param[in[ ReplaceWith String to replace FindTarget with
2252 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2253 immediately before it.
2255 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2256 @retval EFI_INVALID_PARAMETER NewString was NULL.
2257 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2258 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2259 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2260 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2261 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2262 the new string (truncation occurred).
2263 @retval EFI_SUCCESS the string was sucessfully copied with replacement.
2268 ShellCopySearchAndReplace2(
2269 IN CHAR16 CONST
*SourceString
,
2270 IN CHAR16
*NewString
,
2272 IN CONST CHAR16
*FindTarget
,
2273 IN CONST CHAR16
*ReplaceWith
,
2274 IN CONST BOOLEAN SkipPreCarrot
2278 if ( (SourceString
== NULL
)
2279 || (NewString
== NULL
)
2280 || (FindTarget
== NULL
)
2281 || (ReplaceWith
== NULL
)
2282 || (StrLen(FindTarget
) < 1)
2283 || (StrLen(SourceString
) < 1)
2285 return (EFI_INVALID_PARAMETER
);
2287 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2288 while (*SourceString
!= CHAR_NULL
) {
2290 // if we find the FindTarget and either Skip == FALSE or Skip == TRUE and we
2291 // dont have a carrot do a replace...
2293 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2294 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || SkipPreCarrot
== FALSE
)
2296 SourceString
+= StrLen(FindTarget
);
2297 Size
= StrSize(NewString
);
2298 if ((Size
+ (StrLen(ReplaceWith
)*sizeof(CHAR16
))) > NewSize
) {
2299 return (EFI_BUFFER_TOO_SMALL
);
2301 StrCat(NewString
, ReplaceWith
);
2303 Size
= StrSize(NewString
);
2304 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2305 return (EFI_BUFFER_TOO_SMALL
);
2307 StrnCat(NewString
, SourceString
, 1);
2311 return (EFI_SUCCESS
);
2315 Internal worker function to output a string.
2317 This function will output a string to the correct StdOut.
2319 @param[in] String The string to print out.
2321 @retval EFI_SUCCESS The operation was sucessful.
2322 @retval !EFI_SUCCESS The operation failed.
2327 IN CONST CHAR16
*String
2331 Size
= StrSize(String
) - sizeof(CHAR16
);
2332 if (mEfiShellParametersProtocol
!= NULL
) {
2333 return (mEfiShellParametersProtocol
->StdOut
->Write(mEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2335 if (mEfiShellInterface
!= NULL
) {
2337 // Divide in half for old shell. Must be string length not size.
2340 return ( mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2343 return (EFI_UNSUPPORTED
);
2347 Print at a specific location on the screen.
2349 This function will move the cursor to a given screen location and print the specified string
2351 If -1 is specified for either the Row or Col the current screen location for BOTH
2354 if either Row or Col is out of range for the current console, then ASSERT
2355 if Format is NULL, then ASSERT
2357 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2358 the following additional flags:
2359 %N - Set output attribute to normal
2360 %H - Set output attribute to highlight
2361 %E - Set output attribute to error
2362 %B - Set output attribute to blue color
2363 %V - Set output attribute to green color
2365 Note: The background color is controlled by the shell command cls.
2367 @param[in] Row the row to print at
2368 @param[in] Col the column to print at
2369 @param[in] Format the format string
2370 @param[in] Marker the marker for the variable argument list
2372 @return the number of characters printed to the screen
2377 InternalShellPrintWorker(
2378 IN INT32 Col OPTIONAL
,
2379 IN INT32 Row OPTIONAL
,
2380 IN CONST CHAR16
*Format
,
2386 UINTN NormalAttribute
;
2387 CHAR16
*ResumeLocation
;
2388 CHAR16
*FormatWalker
;
2391 // Back and forth each time fixing up 1 of our flags...
2393 Status
= ShellLibCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N");
2394 ASSERT_EFI_ERROR(Status
);
2395 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E");
2396 ASSERT_EFI_ERROR(Status
);
2397 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H");
2398 ASSERT_EFI_ERROR(Status
);
2399 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B");
2400 ASSERT_EFI_ERROR(Status
);
2401 Status
= ShellLibCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V");
2402 ASSERT_EFI_ERROR(Status
);
2405 // Use the last buffer from replacing to print from...
2407 Return
= UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2409 if (Col
!= -1 && Row
!= -1) {
2410 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2411 ASSERT_EFI_ERROR(Status
);
2414 NormalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2415 FormatWalker
= mPostReplaceFormat2
;
2416 while (*FormatWalker
!= CHAR_NULL
) {
2418 // Find the next attribute change request
2420 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2421 if (ResumeLocation
!= NULL
) {
2422 *ResumeLocation
= CHAR_NULL
;
2425 // print the current FormatWalker string
2427 Status
= InternalPrintTo(FormatWalker
);
2428 ASSERT_EFI_ERROR(Status
);
2430 // update the attribute
2432 if (ResumeLocation
!= NULL
) {
2433 switch (*(ResumeLocation
+1)) {
2435 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2438 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2441 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2444 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2447 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2451 // Print a simple '%' symbol
2453 Status
= InternalPrintTo(L
"%");
2454 ASSERT_EFI_ERROR(Status
);
2455 ResumeLocation
= ResumeLocation
- 1;
2460 // reset to normal now...
2462 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2467 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2469 FormatWalker
= ResumeLocation
+ 2;
2476 Print at a specific location on the screen.
2478 This function will move the cursor to a given screen location and print the specified string.
2480 If -1 is specified for either the Row or Col the current screen location for BOTH
2483 If either Row or Col is out of range for the current console, then ASSERT.
2484 If Format is NULL, then ASSERT.
2486 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2487 the following additional flags:
2488 %N - Set output attribute to normal
2489 %H - Set output attribute to highlight
2490 %E - Set output attribute to error
2491 %B - Set output attribute to blue color
2492 %V - Set output attribute to green color
2494 Note: The background color is controlled by the shell command cls.
2496 @param[in] Row the row to print at
2497 @param[in] Col the column to print at
2498 @param[in] Format the format string
2500 @return the number of characters printed to the screen
2506 IN INT32 Col OPTIONAL
,
2507 IN INT32 Row OPTIONAL
,
2508 IN CONST CHAR16
*Format
,
2514 VA_START (Marker
, Format
);
2515 Status
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2521 Print at a specific location on the screen.
2523 This function will move the cursor to a given screen location and print the specified string.
2525 If -1 is specified for either the Row or Col the current screen location for BOTH
2528 If either Row or Col is out of range for the current console, then ASSERT.
2529 If Format is NULL, then ASSERT.
2531 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2532 the following additional flags:
2533 %N - Set output attribute to normal
2534 %H - Set output attribute to highlight
2535 %E - Set output attribute to error
2536 %B - Set output attribute to blue color
2537 %V - Set output attribute to green color
2539 Note: The background color is controlled by the shell command cls.
2541 @param[in] Row the row to print at
2542 @param[in] Col the column to print at
2543 @param[in] HiiFormatStringId the format string Id for getting from Hii
2544 @param[in] HiiFormatHandle the format string Handle for getting from Hii
2546 @return the number of characters printed to the screen
2551 IN INT32 Col OPTIONAL
,
2552 IN INT32 Row OPTIONAL
,
2553 IN CONST EFI_STRING_ID HiiFormatStringId
,
2554 IN CONST EFI_HANDLE HiiFormatHandle
,
2559 CHAR16
*HiiFormatString
;
2562 VA_START (Marker
, HiiFormatHandle
);
2563 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
2564 ASSERT(HiiFormatString
!= NULL
);
2566 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2568 FreePool(HiiFormatString
);
2575 Function to determine if a given filename represents a file or a directory.
2577 @param[in] DirName Path to directory to test.
2579 @retval EFI_SUCCESS The Path represents a directory
2580 @retval EFI_NOT_FOUND The Path does not represent a directory
2581 @return other The path failed to open
2586 IN CONST CHAR16
*DirName
2590 EFI_FILE_HANDLE Handle
;
2592 ASSERT(DirName
!= NULL
);
2596 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2597 if (EFI_ERROR(Status
)) {
2601 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2602 ShellCloseFile(&Handle
);
2603 return (EFI_SUCCESS
);
2605 ShellCloseFile(&Handle
);
2606 return (EFI_NOT_FOUND
);
2610 Function to determine if a given filename represents a file.
2612 @param[in] Name Path to file to test.
2614 @retval EFI_SUCCESS The Path represents a file.
2615 @retval EFI_NOT_FOUND The Path does not represent a file.
2616 @retval other The path failed to open.
2621 IN CONST CHAR16
*Name
2625 EFI_FILE_HANDLE Handle
;
2627 ASSERT(Name
!= NULL
);
2631 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
2632 if (EFI_ERROR(Status
)) {
2636 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
2637 ShellCloseFile(&Handle
);
2638 return (EFI_SUCCESS
);
2640 ShellCloseFile(&Handle
);
2641 return (EFI_NOT_FOUND
);
2645 Function to determine if a given filename represents a file.
2647 This will search the CWD and then the Path.
2649 If Name is NULL, then ASSERT.
2651 @param[in] Name Path to file to test.
2653 @retval EFI_SUCCESS The Path represents a file.
2654 @retval EFI_NOT_FOUND The Path does not represent a file.
2655 @retval other The path failed to open.
2660 IN CONST CHAR16
*Name
2665 if (!EFI_ERROR(ShellIsFile(Name
))) {
2669 NewName
= ShellFindFilePath(Name
);
2670 if (NewName
== NULL
) {
2671 return (EFI_NOT_FOUND
);
2673 Status
= ShellIsFile(NewName
);
2678 Function to determine whether a string is decimal or hex representation of a number
2679 and return the number converted from the string.
2681 @param[in] String String representation of a number
2683 @retval all the number
2688 IN CONST CHAR16
*String
2691 CONST CHAR16
*Walker
;
2692 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
2693 if (StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
2694 return (StrHexToUintn(Walker
));
2696 return (StrDecimalToUintn(Walker
));
2700 Safely append with automatic string resizing given length of Destination and
2701 desired length of copy from Source.
2703 append the first D characters of Source to the end of Destination, where D is
2704 the lesser of Count and the StrLen() of Source. If appending those D characters
2705 will fit within Destination (whose Size is given as CurrentSize) and
2706 still leave room for a null terminator, then those characters are appended,
2707 starting at the original terminating null of Destination, and a new terminating
2710 If appending D characters onto Destination will result in a overflow of the size
2711 given in CurrentSize the string will be grown such that the copy can be performed
2712 and CurrentSize will be updated to the new size.
2714 If Source is NULL, there is nothing to append, just return the current buffer in
2717 if Destination is NULL, then ASSERT()
2718 if Destination's current length (including NULL terminator) is already more then
2719 CurrentSize, then ASSERT()
2721 @param[in,out] Destination The String to append onto
2722 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2723 return possibly the new size (still in bytes). if NULL
2724 then allocate whatever is needed.
2725 @param[in] Source The String to append from
2726 @param[in] Count Maximum number of characters to append. if 0 then
2729 @return Destination return the resultant string.
2734 IN OUT CHAR16
**Destination
,
2735 IN OUT UINTN
*CurrentSize
,
2736 IN CONST CHAR16
*Source
,
2740 UINTN DestinationStartSize
;
2746 ASSERT(Destination
!= NULL
);
2749 // If there's nothing to do then just return Destination
2751 if (Source
== NULL
) {
2752 return (*Destination
);
2756 // allow for un-initialized pointers, based on size being 0
2758 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
2759 *Destination
= NULL
;
2763 // allow for NULL pointers address as Destination
2765 if (*Destination
!= NULL
) {
2766 ASSERT(CurrentSize
!= 0);
2767 DestinationStartSize
= StrSize(*Destination
);
2768 ASSERT(DestinationStartSize
<= *CurrentSize
);
2770 DestinationStartSize
= 0;
2771 // ASSERT(*CurrentSize == 0);
2775 // Append all of Source?
2778 Count
= StrLen(Source
);
2782 // Test and grow if required
2784 if (CurrentSize
!= NULL
) {
2785 NewSize
= *CurrentSize
;
2786 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
2787 NewSize
+= 2 * Count
* sizeof(CHAR16
);
2789 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
2790 *CurrentSize
= NewSize
;
2792 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
2796 // Now use standard StrnCat on a big enough buffer
2798 return StrnCat(*Destination
, Source
, Count
);