2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2009, 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.
16 #include <Library/ShellLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/DevicePathLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/FileHandleLib.h>
25 #include <Library/PrintLib.h>
26 #include <Library/UefiLib.h>
27 #include <Library/HiiLib.h>
29 #include <Protocol/EfiShellEnvironment2.h>
30 #include <Protocol/EfiShellInterface.h>
31 #include <Protocol/EfiShell.h>
32 #include <Protocol/EfiShellParameters.h>
33 #include <Protocol/SimpleFileSystem.h>
35 #include "UefiShellLib.h"
37 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
38 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
41 // This is not static since it's extern in the .h file
43 SHELL_PARAM_ITEM EmptyParamList
[] = {
48 // Static file globals for the shell library
50 STATIC EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
51 STATIC EFI_SHELL_INTERFACE
*mEfiShellInterface
;
52 STATIC EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
53 STATIC EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
54 STATIC EFI_HANDLE mEfiShellEnvironment2Handle
;
55 STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
56 STATIC UINTN mTotalParameterCount
;
57 STATIC EFI_FILE_HANDLE StdOut
;
58 STATIC EFI_FILE_HANDLE StdErr
;
61 Check if a Unicode character is a hexadecimal character.
63 This internal function checks if a Unicode character is a
64 decimal character. The valid hexadecimal character is
65 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
68 @param Char The character to check against.
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
76 ShellInternalIsHexaDecimalDigitCharacter (
79 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
83 helper function to find ShellEnvironment2 for constructor
88 IN EFI_HANDLE ImageHandle
97 Status
= gBS
->OpenProtocol(ImageHandle
,
98 &gEfiShellEnvironment2Guid
,
99 (VOID
**)&mEfiShellEnvironment2
,
102 EFI_OPEN_PROTOCOL_GET_PROTOCOL
105 // look for the mEfiShellEnvironment2 protocol at a higher level
107 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
)){
109 // figure out how big of a buffer we need.
111 Status
= gBS
->LocateHandle (ByProtocol
,
112 &gEfiShellEnvironment2Guid
,
113 NULL
, // ignored for ByProtocol
118 // maybe it's not there???
120 if (Status
== EFI_BUFFER_TOO_SMALL
) {
121 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
122 ASSERT(Buffer
!= NULL
);
123 Status
= gBS
->LocateHandle (ByProtocol
,
124 &gEfiShellEnvironment2Guid
,
125 NULL
, // ignored for ByProtocol
130 if (!EFI_ERROR (Status
)) {
132 // now parse the list of returned handles
134 Status
= EFI_NOT_FOUND
;
135 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
136 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
137 &gEfiShellEnvironment2Guid
,
138 (VOID
**)&mEfiShellEnvironment2
,
141 EFI_OPEN_PROTOCOL_GET_PROTOCOL
143 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
) {
144 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
145 Status
= EFI_SUCCESS
;
151 if (Buffer
!= NULL
) {
159 ShellLibConstructorWorker (
160 IN EFI_HANDLE ImageHandle
,
161 IN EFI_SYSTEM_TABLE
*SystemTable
166 // Set the parameter count to an invalid number
168 mTotalParameterCount
= (UINTN
)(-1);
171 // UEFI 2.0 shell interfaces (used preferentially)
173 Status
= gBS
->OpenProtocol(ImageHandle
,
174 &gEfiShellProtocolGuid
,
175 (VOID
**)&mEfiShellProtocol
,
178 EFI_OPEN_PROTOCOL_GET_PROTOCOL
180 if (EFI_ERROR(Status
)) {
181 mEfiShellProtocol
= NULL
;
183 Status
= gBS
->OpenProtocol(ImageHandle
,
184 &gEfiShellParametersProtocolGuid
,
185 (VOID
**)&mEfiShellParametersProtocol
,
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
190 if (EFI_ERROR(Status
)) {
191 mEfiShellParametersProtocol
= NULL
;
194 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
196 // Moved to seperate function due to complexity
198 Status
= ShellFindSE2(ImageHandle
);
200 if (EFI_ERROR(Status
)) {
201 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
202 mEfiShellEnvironment2
= NULL
;
204 Status
= gBS
->OpenProtocol(ImageHandle
,
205 &gEfiShellInterfaceGuid
,
206 (VOID
**)&mEfiShellInterface
,
209 EFI_OPEN_PROTOCOL_GET_PROTOCOL
211 if (EFI_ERROR(Status
)) {
212 mEfiShellInterface
= NULL
;
216 // only success getting 2 of either the old or new, but no 1/2 and 1/2
218 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
219 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
220 if (mEfiShellProtocol
!= NULL
) {
221 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
222 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
223 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
224 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
225 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
226 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
227 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
228 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
229 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
230 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
232 FileFunctionMap
.GetFileInfo
= FileHandleGetInfo
;
233 FileFunctionMap
.SetFileInfo
= FileHandleSetInfo
;
234 FileFunctionMap
.ReadFile
= FileHandleRead
;
235 FileFunctionMap
.WriteFile
= FileHandleWrite
;
236 FileFunctionMap
.CloseFile
= FileHandleClose
;
237 FileFunctionMap
.DeleteFile
= FileHandleDelete
;
238 FileFunctionMap
.GetFilePosition
= FileHandleGetPosition
;
239 FileFunctionMap
.SetFilePosition
= FileHandleSetPosition
;
240 FileFunctionMap
.FlushFile
= FileHandleFlush
;
241 FileFunctionMap
.GetFileSize
= FileHandleGetSize
;
243 return (EFI_SUCCESS
);
245 return (EFI_NOT_FOUND
);
248 Constructor for the Shell library.
250 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
252 @param ImageHandle the image handle of the process
253 @param SystemTable the EFI System Table pointer
255 @retval EFI_SUCCESS the initialization was complete sucessfully
256 @return others an error ocurred during initialization
260 ShellLibConstructor (
261 IN EFI_HANDLE ImageHandle
,
262 IN EFI_SYSTEM_TABLE
*SystemTable
266 mEfiShellEnvironment2
= NULL
;
267 mEfiShellProtocol
= NULL
;
268 mEfiShellParametersProtocol
= NULL
;
269 mEfiShellInterface
= NULL
;
270 mEfiShellEnvironment2Handle
= NULL
;
273 // verify that auto initialize is not set false
275 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
276 return (EFI_SUCCESS
);
279 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
283 Destructory for the library. free any resources.
288 IN EFI_HANDLE ImageHandle
,
289 IN EFI_SYSTEM_TABLE
*SystemTable
291 if (mEfiShellEnvironment2
!= NULL
) {
292 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
293 &gEfiShellEnvironment2Guid
,
296 mEfiShellEnvironment2
= NULL
;
298 if (mEfiShellInterface
!= NULL
) {
299 gBS
->CloseProtocol(ImageHandle
,
300 &gEfiShellInterfaceGuid
,
303 mEfiShellInterface
= NULL
;
305 if (mEfiShellProtocol
!= NULL
) {
306 gBS
->CloseProtocol(ImageHandle
,
307 &gEfiShellProtocolGuid
,
310 mEfiShellProtocol
= NULL
;
312 if (mEfiShellParametersProtocol
!= NULL
) {
313 gBS
->CloseProtocol(ImageHandle
,
314 &gEfiShellParametersProtocolGuid
,
317 mEfiShellParametersProtocol
= NULL
;
319 mEfiShellEnvironment2Handle
= NULL
;
320 return (EFI_SUCCESS
);
324 This function causes the shell library to initialize itself. If the shell library
325 is already initialized it will de-initialize all the current protocol poitners and
326 re-populate them again.
328 When the library is used with PcdShellLibAutoInitialize set to true this function
329 will return EFI_SUCCESS and perform no actions.
331 This function is intended for internal access for shell commands only.
333 @retval EFI_SUCCESS the initialization was complete sucessfully
341 // if auto initialize is not false then skip
343 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
344 return (EFI_SUCCESS
);
348 // deinit the current stuff
350 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
353 // init the new stuff
355 return (ShellLibConstructorWorker(gImageHandle
, gST
));
359 This function will retrieve the information about the file for the handle
360 specified and store it in allocated pool memory.
362 This function allocates a buffer to store the file's information. It is the
363 caller's responsibility to free the buffer
365 @param FileHandle The file handle of the file for which information is
368 @retval NULL information could not be retrieved.
370 @return the information about the file
375 IN EFI_FILE_HANDLE FileHandle
377 return (FileFunctionMap
.GetFileInfo(FileHandle
));
381 This function will set the information about the file for the opened handle
384 @param FileHandle The file handle of the file for which information
387 @param FileInfo The infotmation to set.
389 @retval EFI_SUCCESS The information was set.
390 @retval EFI_UNSUPPORTED The InformationType is not known.
391 @retval EFI_NO_MEDIA The device has no medium.
392 @retval EFI_DEVICE_ERROR The device reported an error.
393 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
394 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
395 @retval EFI_ACCESS_DENIED The file was opened read only.
396 @retval EFI_VOLUME_FULL The volume is full.
401 IN EFI_FILE_HANDLE FileHandle
,
402 IN EFI_FILE_INFO
*FileInfo
404 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
408 This function will open a file or directory referenced by DevicePath.
410 This function opens a file with the open mode according to the file path. The
411 Attributes is valid only for EFI_FILE_MODE_CREATE.
413 @param FilePath on input the device path to the file. On output
414 the remaining device path.
415 @param DeviceHandle pointer to the system device handle.
416 @param FileHandle pointer to the file handle.
417 @param OpenMode the mode to open the file with.
418 @param Attributes the file's file attributes.
420 @retval EFI_SUCCESS The information was set.
421 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
422 @retval EFI_UNSUPPORTED Could not open the file path.
423 @retval EFI_NOT_FOUND The specified file could not be found on the
424 device or the file system could not be found on
426 @retval EFI_NO_MEDIA The device has no medium.
427 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
428 medium is no longer supported.
429 @retval EFI_DEVICE_ERROR The device reported an error.
430 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
431 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
432 @retval EFI_ACCESS_DENIED The file was opened read only.
433 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
435 @retval EFI_VOLUME_FULL The volume is full.
439 ShellOpenFileByDevicePath(
440 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
441 OUT EFI_HANDLE
*DeviceHandle
,
442 OUT EFI_FILE_HANDLE
*FileHandle
,
448 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
449 EFI_FILE_HANDLE LastHandle
;
452 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
454 ASSERT(FilePath
!= NULL
);
455 ASSERT(FileHandle
!= NULL
);
456 ASSERT(DeviceHandle
!= NULL
);
458 // which shell interface should we use
460 if (mEfiShellProtocol
!= NULL
) {
462 // use UEFI Shell 2.0 method.
464 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
465 if (FileName
== NULL
) {
466 return (EFI_INVALID_PARAMETER
);
468 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
475 // use old shell method.
477 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
480 if (EFI_ERROR (Status
)) {
483 Status
= gBS
->OpenProtocol(*DeviceHandle
,
484 &gEfiSimpleFileSystemProtocolGuid
,
485 (VOID
**)&EfiSimpleFileSystemProtocol
,
488 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
489 if (EFI_ERROR (Status
)) {
492 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
493 if (EFI_ERROR (Status
)) {
499 // go down directories one node at a time.
501 while (!IsDevicePathEnd (*FilePath
)) {
503 // For file system access each node should be a file path component
505 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
506 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
509 return (EFI_INVALID_PARAMETER
);
512 // Open this file path node
514 LastHandle
= *FileHandle
;
518 // Try to test opening an existing file
520 Status
= LastHandle
->Open (
523 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
524 OpenMode
&~EFI_FILE_MODE_CREATE
,
529 // see if the error was that it needs to be created
531 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
532 Status
= LastHandle
->Open (
535 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
541 // Close the last node
543 LastHandle
->Close (LastHandle
);
545 if (EFI_ERROR(Status
)) {
552 *FilePath
= NextDevicePathNode (*FilePath
);
554 return (EFI_SUCCESS
);
558 This function will open a file or directory referenced by filename.
560 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
561 otherwise, the Filehandle is NULL. The Attributes is valid only for
562 EFI_FILE_MODE_CREATE.
564 if FileNAme is NULL then ASSERT()
566 @param FileName pointer to file name
567 @param FileHandle pointer to the file handle.
568 @param OpenMode the mode to open the file with.
569 @param Attributes the file's file attributes.
571 @retval EFI_SUCCESS The information was set.
572 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
573 @retval EFI_UNSUPPORTED Could not open the file path.
574 @retval EFI_NOT_FOUND The specified file could not be found on the
575 device or the file system could not be found
577 @retval EFI_NO_MEDIA The device has no medium.
578 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
579 medium is no longer supported.
580 @retval EFI_DEVICE_ERROR The device reported an error.
581 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
582 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
583 @retval EFI_ACCESS_DENIED The file was opened read only.
584 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
586 @retval EFI_VOLUME_FULL The volume is full.
591 IN CONST CHAR16
*FileName
,
592 OUT EFI_FILE_HANDLE
*FileHandle
,
596 EFI_HANDLE DeviceHandle
;
597 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
599 EFI_FILE_INFO
*FileInfo
;
602 // ASSERT if FileName is NULL
604 ASSERT(FileName
!= NULL
);
606 if (mEfiShellProtocol
!= NULL
) {
608 // Use UEFI Shell 2.0 method
610 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
613 if (!EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
614 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
615 ASSERT(FileInfo
!= NULL
);
616 FileInfo
->Attribute
= Attributes
;
617 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
623 // Using EFI Shell version
624 // this means convert name to path and call that function
625 // since this will use EFI method again that will open it.
627 ASSERT(mEfiShellEnvironment2
!= NULL
);
628 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
629 if (FileDevicePath
!= NULL
) {
630 return (ShellOpenFileByDevicePath(&FilePath
,
636 return (EFI_DEVICE_ERROR
);
639 This function create a directory
641 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
642 otherwise, the Filehandle is NULL. If the directory already existed, this
643 function opens the existing directory.
645 @param DirectoryName pointer to directory name
646 @param FileHandle pointer to the file handle.
648 @retval EFI_SUCCESS The information was set.
649 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
650 @retval EFI_UNSUPPORTED Could not open the file path.
651 @retval EFI_NOT_FOUND The specified file could not be found on the
652 device or the file system could not be found
654 @retval EFI_NO_MEDIA The device has no medium.
655 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
656 medium is no longer supported.
657 @retval EFI_DEVICE_ERROR The device reported an error.
658 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
659 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
660 @retval EFI_ACCESS_DENIED The file was opened read only.
661 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
663 @retval EFI_VOLUME_FULL The volume is full.
664 @sa ShellOpenFileByName
668 ShellCreateDirectory(
669 IN CONST CHAR16
*DirectoryName
,
670 OUT EFI_FILE_HANDLE
*FileHandle
672 if (mEfiShellProtocol
!= NULL
) {
674 // Use UEFI Shell 2.0 method
676 return (mEfiShellProtocol
->CreateFile(DirectoryName
,
681 return (ShellOpenFileByName(DirectoryName
,
683 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
690 This function reads information from an opened file.
692 If FileHandle is not a directory, the function reads the requested number of
693 bytes from the file at the file's current position and returns them in Buffer.
694 If the read goes beyond the end of the file, the read length is truncated to the
695 end of the file. The file's current position is increased by the number of bytes
696 returned. If FileHandle is a directory, the function reads the directory entry
697 at the file's current position and returns the entry in Buffer. If the Buffer
698 is not large enough to hold the current directory entry, then
699 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
700 BufferSize is set to be the size of the buffer needed to read the entry. On
701 success, the current position is updated to the next directory entry. If there
702 are no more directory entries, the read returns a zero-length buffer.
703 EFI_FILE_INFO is the structure returned as the directory entry.
705 @param FileHandle the opened file handle
706 @param BufferSize on input the size of buffer in bytes. on return
707 the number of bytes written.
708 @param Buffer the buffer to put read data into.
710 @retval EFI_SUCCESS Data was read.
711 @retval EFI_NO_MEDIA The device has no media.
712 @retval EFI_DEVICE_ERROR The device reported an error.
713 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
714 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
721 IN EFI_FILE_HANDLE FileHandle
,
722 IN OUT UINTN
*BufferSize
,
725 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
730 Write data to a file.
732 This function writes the specified number of bytes to the file at the current
733 file position. The current file position is advanced the actual number of bytes
734 written, which is returned in BufferSize. Partial writes only occur when there
735 has been a data error during the write attempt (such as "volume space full").
736 The file is automatically grown to hold the data if required. Direct writes to
737 opened directories are not supported.
739 @param FileHandle The opened file for writing
740 @param BufferSize on input the number of bytes in Buffer. On output
741 the number of bytes written.
742 @param Buffer the buffer containing data to write is stored.
744 @retval EFI_SUCCESS Data was written.
745 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
746 @retval EFI_NO_MEDIA The device has no media.
747 @retval EFI_DEVICE_ERROR The device reported an error.
748 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
749 @retval EFI_WRITE_PROTECTED The device is write-protected.
750 @retval EFI_ACCESS_DENIED The file was open for read only.
751 @retval EFI_VOLUME_FULL The volume is full.
756 IN EFI_FILE_HANDLE FileHandle
,
757 IN OUT UINTN
*BufferSize
,
760 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
764 Close an open file handle.
766 This function closes a specified file handle. All "dirty" cached file data is
767 flushed to the device, and the file is closed. In all cases the handle is
770 @param FileHandle the file handle to close.
772 @retval EFI_SUCCESS the file handle was closed sucessfully.
777 IN EFI_FILE_HANDLE
*FileHandle
779 return (FileFunctionMap
.CloseFile(*FileHandle
));
783 Delete a file and close the handle
785 This function closes and deletes a file. In all cases the file handle is closed.
786 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
787 returned, but the handle is still closed.
789 @param FileHandle the file handle to delete
791 @retval EFI_SUCCESS the file was closed sucessfully
792 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
794 @retval INVALID_PARAMETER One of the parameters has an invalid value.
799 IN EFI_FILE_HANDLE
*FileHandle
801 return (FileFunctionMap
.DeleteFile(*FileHandle
));
805 Set the current position in a file.
807 This function sets the current file position for the handle to the position
808 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
809 absolute positioning is supported, and seeking past the end of the file is
810 allowed (a subsequent write would grow the file). Seeking to position
811 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
812 If FileHandle is a directory, the only position that may be set is zero. This
813 has the effect of starting the read process of the directory entries over.
815 @param FileHandle The file handle on which the position is being set
816 @param Position Byte position from begining of file
818 @retval EFI_SUCCESS Operation completed sucessfully.
819 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
821 @retval INVALID_PARAMETER One of the parameters has an invalid value.
825 ShellSetFilePosition (
826 IN EFI_FILE_HANDLE FileHandle
,
829 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
833 Gets a file's current position
835 This function retrieves the current file position for the file handle. For
836 directories, the current file position has no meaning outside of the file
837 system driver and as such the operation is not supported. An error is returned
838 if FileHandle is a directory.
840 @param FileHandle The open file handle on which to get the position.
841 @param Position Byte position from begining of file.
843 @retval EFI_SUCCESS the operation completed sucessfully.
844 @retval INVALID_PARAMETER One of the parameters has an invalid value.
845 @retval EFI_UNSUPPORTED the request is not valid on directories.
849 ShellGetFilePosition (
850 IN EFI_FILE_HANDLE FileHandle
,
853 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
856 Flushes data on a file
858 This function flushes all modified data associated with a file to a device.
860 @param FileHandle The file handle on which to flush data
862 @retval EFI_SUCCESS The data was flushed.
863 @retval EFI_NO_MEDIA The device has no media.
864 @retval EFI_DEVICE_ERROR The device reported an error.
865 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
866 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
867 @retval EFI_ACCESS_DENIED The file was opened for read only.
872 IN EFI_FILE_HANDLE FileHandle
874 return (FileFunctionMap
.FlushFile(FileHandle
));
878 Retrieves the first file from a directory
880 This function opens a directory and gets the first file's info in the
881 directory. Caller can use ShellFindNextFile() to get other files. When
882 complete the caller is responsible for calling FreePool() on Buffer.
884 @param DirHandle The file handle of the directory to search
885 @param Buffer Pointer to buffer for file's information
887 @retval EFI_SUCCESS Found the first file.
888 @retval EFI_NOT_FOUND Cannot find the directory.
889 @retval EFI_NO_MEDIA The device has no media.
890 @retval EFI_DEVICE_ERROR The device reported an error.
891 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
892 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
898 IN EFI_FILE_HANDLE DirHandle
,
899 OUT EFI_FILE_INFO
**Buffer
902 // pass to file handle lib
904 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
907 Retrieves the next file in a directory.
909 To use this function, caller must call the LibFindFirstFile() to get the
910 first file, and then use this function get other files. This function can be
911 called for several times to get each file's information in the directory. If
912 the call of ShellFindNextFile() got the last file in the directory, the next
913 call of this function has no file to get. *NoFile will be set to TRUE and the
914 Buffer memory will be automatically freed.
916 @param DirHandle the file handle of the directory
917 @param Buffer pointer to buffer for file's information
918 @param NoFile pointer to boolean when last file is found
920 @retval EFI_SUCCESS Found the next file, or reached last file
921 @retval EFI_NO_MEDIA The device has no media.
922 @retval EFI_DEVICE_ERROR The device reported an error.
923 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
928 IN EFI_FILE_HANDLE DirHandle
,
929 OUT EFI_FILE_INFO
*Buffer
,
933 // pass to file handle lib
935 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
938 Retrieve the size of a file.
940 if FileHandle is NULL then ASSERT()
941 if Size is NULL then ASSERT()
943 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
946 @param FileHandle file handle from which size is retrieved
947 @param Size pointer to size
949 @retval EFI_SUCCESS operation was completed sucessfully
950 @retval EFI_DEVICE_ERROR cannot access the file
955 IN EFI_FILE_HANDLE FileHandle
,
958 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
961 Retrieves the status of the break execution flag
963 this function is useful to check whether the application is being asked to halt by the shell.
965 @retval TRUE the execution break is enabled
966 @retval FALSE the execution break is not enabled
970 ShellGetExecutionBreakFlag(
975 // Check for UEFI Shell 2.0 protocols
977 if (mEfiShellProtocol
!= NULL
) {
980 // We are using UEFI Shell 2.0; see if the event has been triggered
982 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
989 // using EFI Shell; call the function to check
991 ASSERT(mEfiShellEnvironment2
!= NULL
);
992 return (mEfiShellEnvironment2
->GetExecutionBreak());
995 return the value of an environment variable
997 this function gets the value of the environment variable set by the
998 ShellSetEnvironmentVariable function
1000 @param EnvKey The key name of the environment variable.
1002 @retval NULL the named environment variable does not exist.
1003 @return != NULL pointer to the value of the environment variable
1007 ShellGetEnvironmentVariable (
1008 IN CONST CHAR16
*EnvKey
1012 // Check for UEFI Shell 2.0 protocols
1014 if (mEfiShellProtocol
!= NULL
) {
1015 return (mEfiShellProtocol
->GetEnv(EnvKey
));
1019 // ASSERT that we must have EFI shell
1021 ASSERT(mEfiShellEnvironment2
!= NULL
);
1026 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1029 set the value of an environment variable
1031 This function changes the current value of the specified environment variable. If the
1032 environment variable exists and the Value is an empty string, then the environment
1033 variable is deleted. If the environment variable exists and the Value is not an empty
1034 string, then the value of the environment variable is changed. If the environment
1035 variable does not exist and the Value is an empty string, there is no action. If the
1036 environment variable does not exist and the Value is a non-empty string, then the
1037 environment variable is created and assigned the specified value.
1039 This is not supported pre-UEFI Shell 2.0.
1041 @param EnvKey The key name of the environment variable.
1042 @param EnvVal The Value of the environment variable
1043 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1045 @retval EFI_SUCCESS the operation was completed sucessfully
1046 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1050 ShellSetEnvironmentVariable (
1051 IN CONST CHAR16
*EnvKey
,
1052 IN CONST CHAR16
*EnvVal
,
1057 // Check for UEFI Shell 2.0 protocols
1059 if (mEfiShellProtocol
!= NULL
) {
1060 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1064 // This feature does not exist under EFI shell
1066 return (EFI_UNSUPPORTED
);
1069 cause the shell to parse and execute a command line.
1071 This function creates a nested instance of the shell and executes the specified
1072 command (CommandLine) with the specified environment (Environment). Upon return,
1073 the status code returned by the specified command is placed in StatusCode.
1074 If Environment is NULL, then the current environment is used and all changes made
1075 by the commands executed will be reflected in the current environment. If the
1076 Environment is non-NULL, then the changes made will be discarded.
1077 The CommandLine is executed from the current working directory on the current
1080 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1081 Output is only supported for pre-UEFI Shell 2.0
1083 @param ImageHandle Parent image that is starting the operation
1084 @param CommandLine pointer to null terminated command line.
1085 @param Output true to display debug output. false to hide it.
1086 @param EnvironmentVariables optional pointer to array of environment variables
1087 in the form "x=y". if NULL current set is used.
1088 @param Status the status of the run command line.
1090 @retval EFI_SUCCESS the operation completed sucessfully. Status
1091 contains the status code returned.
1092 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1093 @retval EFI_OUT_OF_RESOURCES out of resources
1094 @retval EFI_UNSUPPORTED the operation is not allowed.
1099 IN EFI_HANDLE
*ParentHandle
,
1100 IN CHAR16
*CommandLine OPTIONAL
,
1101 IN BOOLEAN Output OPTIONAL
,
1102 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1103 OUT EFI_STATUS
*Status OPTIONAL
1107 // Check for UEFI Shell 2.0 protocols
1109 if (mEfiShellProtocol
!= NULL
) {
1111 // Call UEFI Shell 2.0 version (not using Output parameter)
1113 return (mEfiShellProtocol
->Execute(ParentHandle
,
1115 EnvironmentVariables
,
1119 // ASSERT that we must have EFI shell
1121 ASSERT(mEfiShellEnvironment2
!= NULL
);
1123 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1124 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1126 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1131 Retreives the current directory path
1133 If the DeviceName is NULL, it returns the current device's current directory
1134 name. If the DeviceName is not NULL, it returns the current directory name
1137 @param DeviceName the name of the drive to get directory on
1139 @retval NULL the directory does not exist
1140 @return != NULL the directory
1144 ShellGetCurrentDir (
1145 IN CHAR16
*DeviceName OPTIONAL
1149 // Check for UEFI Shell 2.0 protocols
1151 if (mEfiShellProtocol
!= NULL
) {
1152 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1155 // ASSERT that we must have EFI shell
1157 ASSERT(mEfiShellEnvironment2
!= NULL
);
1158 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1161 sets (enabled or disabled) the page break mode
1163 when page break mode is enabled the screen will stop scrolling
1164 and wait for operator input before scrolling a subsequent screen.
1166 @param CurrentState TRUE to enable and FALSE to disable
1170 ShellSetPageBreakMode (
1171 IN BOOLEAN CurrentState
1175 // check for enabling
1177 if (CurrentState
!= 0x00) {
1179 // check for UEFI Shell 2.0
1181 if (mEfiShellProtocol
!= NULL
) {
1183 // Enable with UEFI 2.0 Shell
1185 mEfiShellProtocol
->EnablePageBreak();
1189 // ASSERT that must have EFI Shell
1191 ASSERT(mEfiShellEnvironment2
!= NULL
);
1193 // Enable with EFI Shell
1195 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1200 // check for UEFI Shell 2.0
1202 if (mEfiShellProtocol
!= NULL
) {
1204 // Disable with UEFI 2.0 Shell
1206 mEfiShellProtocol
->DisablePageBreak();
1210 // ASSERT that must have EFI Shell
1212 ASSERT(mEfiShellEnvironment2
!= NULL
);
1214 // Disable with EFI Shell
1216 mEfiShellEnvironment2
->DisablePageBreak ();
1223 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1224 /// This allows for the struct to be populated.
1231 EFI_FILE_HANDLE Handle
;
1232 EFI_FILE_INFO
*Info
;
1233 } EFI_SHELL_FILE_INFO_NO_CONST
;
1236 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1238 if OldStyleFileList is NULL then ASSERT()
1240 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1241 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1242 the ShellCloseFileMetaArg function.
1244 @param[in] FileList the EFI shell list type
1245 @param[in,out] ListHead the list to add to
1247 @retval the resultant head of the double linked new format list;
1251 InternalShellConvertFileListType (
1252 IN LIST_ENTRY
*FileList
,
1253 IN OUT LIST_ENTRY
*ListHead
1256 SHELL_FILE_ARG
*OldInfo
;
1258 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1263 ASSERT(FileList
!= NULL
);
1264 ASSERT(ListHead
!= NULL
);
1267 // enumerate through each member of the old list and copy
1269 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1270 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1273 // make sure the old list was valid
1275 ASSERT(OldInfo
!= NULL
);
1276 ASSERT(OldInfo
->Info
!= NULL
);
1277 ASSERT(OldInfo
->FullName
!= NULL
);
1278 ASSERT(OldInfo
->FileName
!= NULL
);
1281 // allocate a new EFI_SHELL_FILE_INFO object
1283 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1286 // copy the simple items
1288 NewInfo
->Handle
= OldInfo
->Handle
;
1289 NewInfo
->Status
= OldInfo
->Status
;
1291 // old shell checks for 0 not NULL
1292 OldInfo
->Handle
= 0;
1295 // allocate new space to copy strings and structure
1297 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1298 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1299 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1302 // make sure all the memory allocations were sucessful
1304 ASSERT(NewInfo
->FullName
!= NULL
);
1305 ASSERT(NewInfo
->FileName
!= NULL
);
1306 ASSERT(NewInfo
->Info
!= NULL
);
1309 // Copt the strings and structure
1311 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1312 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1313 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1316 // add that to the list
1318 InsertTailList(ListHead
, &NewInfo
->Link
);
1323 Opens a group of files based on a path.
1325 This function uses the Arg to open all the matching files. Each matched
1326 file has a SHELL_FILE_ARG structure to record the file information. These
1327 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1328 structures from ListHead to access each file. This function supports wildcards
1329 and will process '?' and '*' as such. the list must be freed with a call to
1330 ShellCloseFileMetaArg().
1332 If you are NOT appending to an existing list *ListHead must be NULL. If
1333 *ListHead is NULL then it must be callee freed.
1335 @param Arg pointer to path string
1336 @param OpenMode mode to open files with
1337 @param ListHead head of linked list of results
1339 @retval EFI_SUCCESS the operation was sucessful and the list head
1340 contains the list of opened files
1341 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1342 *ListHead is set to NULL.
1343 @return != EFI_SUCCESS the operation failed
1345 @sa InternalShellConvertFileListType
1349 ShellOpenFileMetaArg (
1352 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1356 LIST_ENTRY mOldStyleFileList
;
1359 // ASSERT that Arg and ListHead are not NULL
1361 ASSERT(Arg
!= NULL
);
1362 ASSERT(ListHead
!= NULL
);
1365 // Check for UEFI Shell 2.0 protocols
1367 if (mEfiShellProtocol
!= NULL
) {
1368 if (*ListHead
== NULL
) {
1369 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1370 if (*ListHead
== NULL
) {
1371 return (EFI_OUT_OF_RESOURCES
);
1373 InitializeListHead(&((*ListHead
)->Link
));
1375 Status
= mEfiShellProtocol
->OpenFileList(Arg
,
1378 if (EFI_ERROR(Status
)) {
1379 mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1381 Status
= mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1387 // ASSERT that we must have EFI shell
1389 ASSERT(mEfiShellEnvironment2
!= NULL
);
1392 // make sure the list head is initialized
1394 InitializeListHead(&mOldStyleFileList
);
1397 // Get the EFI Shell list of files
1399 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1400 if (EFI_ERROR(Status
)) {
1405 if (*ListHead
== NULL
) {
1406 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1407 if (*ListHead
== NULL
) {
1408 return (EFI_OUT_OF_RESOURCES
);
1413 // Convert that to equivalent of UEFI Shell 2.0 structure
1415 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1418 // Free the EFI Shell version that was converted.
1420 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1425 Free the linked list returned from ShellOpenFileMetaArg
1427 if ListHead is NULL then ASSERT()
1429 @param ListHead the pointer to free
1431 @retval EFI_SUCCESS the operation was sucessful
1435 ShellCloseFileMetaArg (
1436 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1442 // ASSERT that ListHead is not NULL
1444 ASSERT(ListHead
!= NULL
);
1447 // Check for UEFI Shell 2.0 protocols
1449 if (mEfiShellProtocol
!= NULL
) {
1450 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1453 // Since this is EFI Shell version we need to free our internally made copy
1456 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1457 ; IsListEmpty(&(*ListHead
)->Link
) == FALSE
1458 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1459 RemoveEntryList(Node
);
1460 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1461 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1462 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1463 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1464 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1471 Find a file by searching the CWD and then the path.
1473 if FileName is NULL then ASSERT.
1475 if the return value is not NULL then the memory must be caller freed.
1477 @param FileName Filename string.
1479 @retval NULL the file was not found
1480 @return !NULL the full path to the file.
1485 IN CONST CHAR16
*FileName
1489 EFI_FILE_HANDLE Handle
;
1493 CONST CHAR16
*Walker
;
1498 Path
= ShellGetEnvironmentVariable(L
"cwd");
1500 Size
= StrSize(Path
);
1501 Size
+= StrSize(FileName
);
1502 TestPath
= AllocateZeroPool(Size
);
1503 StrCpy(TestPath
, Path
);
1504 StrCat(TestPath
, FileName
);
1505 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1506 if (!EFI_ERROR(Status
)){
1507 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1508 ShellCloseFile(&Handle
);
1514 Path
= ShellGetEnvironmentVariable(L
"path");
1516 Size
= StrSize(Path
);
1517 Size
+= StrSize(FileName
);
1518 TestPath
= AllocateZeroPool(Size
);
1519 Walker
= (CHAR16
*)Path
;
1521 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1522 if (StrStr(TestPath
, L
";") != NULL
) {
1523 *(StrStr(TestPath
, L
";")) = CHAR_NULL
;
1525 StrCat(TestPath
, FileName
);
1526 if (StrStr(Walker
, L
";") != NULL
) {
1527 Walker
= StrStr(Walker
, L
";") + 1;
1531 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1532 if (!EFI_ERROR(Status
)){
1533 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1534 ShellCloseFile(&Handle
);
1537 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1548 UINTN OriginalPosition
;
1549 } SHELL_PARAM_PACKAGE
;
1552 Checks the list of valid arguments and returns TRUE if the item was found. If the
1553 return value is TRUE then the type parameter is set also.
1555 if CheckList is NULL then ASSERT();
1556 if Name is NULL then ASSERT();
1557 if Type is NULL then ASSERT();
1559 @param Type pointer to type of parameter if it was found
1560 @param Name pointer to Name of parameter found
1561 @param CheckList List to check against
1563 @retval TRUE the Parameter was found. Type is valid.
1564 @retval FALSE the Parameter was not found. Type is not valid.
1568 InternalIsOnCheckList (
1569 IN CONST CHAR16
*Name
,
1570 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1573 SHELL_PARAM_ITEM
*TempListItem
;
1576 // ASSERT that all 3 pointer parameters aren't NULL
1578 ASSERT(CheckList
!= NULL
);
1579 ASSERT(Type
!= NULL
);
1580 ASSERT(Name
!= NULL
);
1583 // question mark and page break mode are always supported
1585 if ((StrCmp(Name
, L
"-?") == 0) ||
1586 (StrCmp(Name
, L
"-b") == 0)
1592 // Enumerate through the list
1594 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1596 // If the Type is TypeStart only check the first characters of the passed in param
1597 // If it matches set the type and return TRUE
1599 if (TempListItem
->Type
== TypeStart
&& StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1600 *Type
= TempListItem
->Type
;
1602 } else if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1603 *Type
= TempListItem
->Type
;
1611 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1613 @param Name pointer to Name of parameter found
1615 @retval TRUE the Parameter is a flag.
1616 @retval FALSE the Parameter not a flag
1621 IN CONST CHAR16
*Name
,
1622 IN BOOLEAN AlwaysAllowNumbers
1626 // ASSERT that Name isn't NULL
1628 ASSERT(Name
!= NULL
);
1631 // If we accept numbers then dont return TRUE. (they will be values)
1633 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellInternalIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
== TRUE
) {
1638 // If the Name has a / or - as the first character return TRUE
1640 if ((Name
[0] == L
'/') ||
1641 (Name
[0] == L
'-') ||
1650 Checks the command line arguments passed against the list of valid ones.
1652 If no initialization is required, then return RETURN_SUCCESS.
1654 @param CheckList pointer to list of parameters to check
1655 @param CheckPackage pointer to pointer to list checked values
1656 @param ProblemParam optional pointer to pointer to unicode string for
1657 the paramater that caused failure. If used then the
1658 caller is responsible for freeing the memory.
1659 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1660 @param Argc Count of parameters in Argv
1661 @param Argv pointer to array of parameters
1663 @retval EFI_SUCCESS The operation completed sucessfully.
1664 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1665 @retval EFI_INVALID_PARAMETER A parameter was invalid
1666 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1667 duplicated. the duplicated command line argument
1668 was returned in ProblemParam if provided.
1669 @retval EFI_NOT_FOUND a argument required a value that was missing.
1670 the invalid command line argument was returned in
1671 ProblemParam if provided.
1676 InternalCommandLineParse (
1677 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1678 OUT LIST_ENTRY
**CheckPackage
,
1679 OUT CHAR16
**ProblemParam OPTIONAL
,
1680 IN BOOLEAN AutoPageBreak
,
1681 IN CONST CHAR16
**Argv
,
1683 IN BOOLEAN AlwaysAllowNumbers
1686 ParamType CurrentItemType
;
1687 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1691 CurrentItemPackage
= NULL
;
1692 mTotalParameterCount
= 0;
1697 // If there is only 1 item we dont need to do anything
1700 *CheckPackage
= NULL
;
1701 return (EFI_SUCCESS
);
1707 ASSERT(CheckList
!= NULL
);
1708 ASSERT(Argv
!= NULL
);
1711 // initialize the linked list
1713 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1714 InitializeListHead(*CheckPackage
);
1717 // loop through each of the arguments
1719 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1720 if (Argv
[LoopCounter
] == NULL
) {
1722 // do nothing for NULL argv
1724 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) == TRUE
) {
1726 // We might have leftover if last parameter didnt have optional value
1728 if (GetItemValue
!= 0) {
1730 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1735 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1736 ASSERT(CurrentItemPackage
!= NULL
);
1737 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1738 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1739 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1740 CurrentItemPackage
->Type
= CurrentItemType
;
1741 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1742 CurrentItemPackage
->Value
= NULL
;
1745 // Does this flag require a value
1747 switch (CurrentItemPackage
->Type
) {
1749 // possibly trigger the next loop(s) to populate the value of this item
1755 case TypeDoubleValue
:
1760 GetItemValue
= (UINTN
)(-1);
1765 // this item has no value expected; we are done
1767 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1768 ASSERT(GetItemValue
== 0);
1771 } else if (GetItemValue
!= 0 && InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) == FALSE
) {
1772 ASSERT(CurrentItemPackage
!= NULL
);
1774 // get the item VALUE for a previous flag
1776 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
1777 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1778 if (ValueSize
== 0) {
1779 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1781 StrCat(CurrentItemPackage
->Value
, L
" ");
1782 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1784 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
1786 if (GetItemValue
== 0) {
1787 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1789 } else if (InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) == FALSE
) {
1791 // add this one as a non-flag
1793 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1794 ASSERT(CurrentItemPackage
!= NULL
);
1795 CurrentItemPackage
->Name
= NULL
;
1796 CurrentItemPackage
->Type
= TypePosition
;
1797 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1798 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1799 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1800 CurrentItemPackage
->OriginalPosition
= mTotalParameterCount
++;
1801 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1802 } else if (ProblemParam
) {
1804 // this was a non-recognised flag... error!
1806 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1807 ASSERT(*ProblemParam
!= NULL
);
1808 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1809 ShellCommandLineFreeVarList(*CheckPackage
);
1810 *CheckPackage
= NULL
;
1811 return (EFI_VOLUME_CORRUPTED
);
1813 ShellCommandLineFreeVarList(*CheckPackage
);
1814 *CheckPackage
= NULL
;
1815 return (EFI_VOLUME_CORRUPTED
);
1818 if (GetItemValue
!= 0) {
1820 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1823 // support for AutoPageBreak
1825 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1826 ShellSetPageBreakMode(TRUE
);
1828 return (EFI_SUCCESS
);
1832 Checks the command line arguments passed against the list of valid ones.
1833 Optionally removes NULL values first.
1835 If no initialization is required, then return RETURN_SUCCESS.
1837 @param CheckList pointer to list of parameters to check
1838 @param CheckPackage pointer to pointer to list checked values
1839 @param ProblemParam optional pointer to pointer to unicode string for
1840 the paramater that caused failure.
1841 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1843 @retval EFI_SUCCESS The operation completed sucessfully.
1844 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1845 @retval EFI_INVALID_PARAMETER A parameter was invalid
1846 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1847 duplicated. the duplicated command line argument
1848 was returned in ProblemParam if provided.
1849 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1850 of the command line arguments was returned in
1851 ProblemParam if provided.
1852 @retval EFI_NOT_FOUND a argument required a value that was missing.
1853 the invalid command line argument was returned in
1854 ProblemParam if provided.
1858 ShellCommandLineParseEx (
1859 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1860 OUT LIST_ENTRY
**CheckPackage
,
1861 OUT CHAR16
**ProblemParam OPTIONAL
,
1862 IN BOOLEAN AutoPageBreak
,
1863 IN BOOLEAN AlwaysAllowNumbers
1866 // ASSERT that CheckList and CheckPackage aren't NULL
1868 ASSERT(CheckList
!= NULL
);
1869 ASSERT(CheckPackage
!= NULL
);
1872 // Check for UEFI Shell 2.0 protocols
1874 if (mEfiShellParametersProtocol
!= NULL
) {
1875 return (InternalCommandLineParse(CheckList
,
1879 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
1880 mEfiShellParametersProtocol
->Argc
,
1881 AlwaysAllowNumbers
));
1885 // ASSERT That EFI Shell is not required
1887 ASSERT (mEfiShellInterface
!= NULL
);
1888 return (InternalCommandLineParse(CheckList
,
1892 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
1893 mEfiShellInterface
->Argc
,
1894 AlwaysAllowNumbers
));
1898 Frees shell variable list that was returned from ShellCommandLineParse.
1900 This function will free all the memory that was used for the CheckPackage
1901 list of postprocessed shell arguments.
1903 this function has no return value.
1905 if CheckPackage is NULL, then return
1907 @param CheckPackage the list to de-allocate
1911 ShellCommandLineFreeVarList (
1912 IN LIST_ENTRY
*CheckPackage
1917 // check for CheckPackage == NULL
1919 if (CheckPackage
== NULL
) {
1924 // for each node in the list
1926 for ( Node
= GetFirstNode(CheckPackage
)
1927 ; IsListEmpty(CheckPackage
) == FALSE
1928 ; Node
= GetFirstNode(CheckPackage
)
1931 // Remove it from the list
1933 RemoveEntryList(Node
);
1936 // if it has a name free the name
1938 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1939 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1943 // if it has a value free the value
1945 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
1946 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1950 // free the node structure
1952 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
1955 // free the list head node
1957 FreePool(CheckPackage
);
1960 Checks for presence of a flag parameter
1962 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1964 if CheckPackage is NULL then return FALSE.
1965 if KeyString is NULL then ASSERT()
1967 @param CheckPackage The package of parsed command line arguments
1968 @param KeyString the Key of the command line argument to check for
1970 @retval TRUE the flag is on the command line
1971 @retval FALSE the flag is not on the command line
1975 ShellCommandLineGetFlag (
1976 IN CONST LIST_ENTRY
*CheckPackage
,
1977 IN CHAR16
*KeyString
1982 // ASSERT that both CheckPackage and KeyString aren't NULL
1984 ASSERT(KeyString
!= NULL
);
1987 // return FALSE for no package
1989 if (CheckPackage
== NULL
) {
1994 // enumerate through the list of parametrs
1996 for ( Node
= GetFirstNode(CheckPackage
)
1997 ; !IsNull (CheckPackage
, Node
)
1998 ; Node
= GetNextNode(CheckPackage
, Node
)
2001 // If the Name matches, return TRUE (and there may be NULL name)
2003 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2005 // If Type is TypeStart then only compare the begining of the strings
2007 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2008 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2011 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2019 returns value from command line argument
2021 value parameters are in the form of "-<Key> value" or "/<Key> value"
2023 if CheckPackage is NULL, then return NULL;
2025 @param CheckPackage The package of parsed command line arguments
2026 @param KeyString the Key of the command line argument to check for
2028 @retval NULL the flag is not on the command line
2029 @return !=NULL pointer to unicode string of the value
2033 ShellCommandLineGetValue (
2034 IN CONST LIST_ENTRY
*CheckPackage
,
2035 IN CHAR16
*KeyString
2040 // check for CheckPackage == NULL
2042 if (CheckPackage
== NULL
) {
2047 // enumerate through the list of parametrs
2049 for ( Node
= GetFirstNode(CheckPackage
)
2050 ; !IsNull (CheckPackage
, Node
)
2051 ; Node
= GetNextNode(CheckPackage
, Node
)
2054 // If the Name matches, return the value (name can be NULL)
2056 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2058 // If Type is TypeStart then only compare the begining of the strings
2060 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2061 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2064 // return the string part after the flag
2066 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2067 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2071 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2078 returns raw value from command line argument
2080 raw value parameters are in the form of "value" in a specific position in the list
2082 if CheckPackage is NULL, then return NULL;
2084 @param CheckPackage The package of parsed command line arguments
2085 @param Position the position of the value
2087 @retval NULL the flag is not on the command line
2088 @return !=NULL pointer to unicode string of the value
2092 ShellCommandLineGetRawValue (
2093 IN CONST LIST_ENTRY
*CheckPackage
,
2099 // check for CheckPackage == NULL
2101 if (CheckPackage
== NULL
) {
2106 // enumerate through the list of parametrs
2108 for ( Node
= GetFirstNode(CheckPackage
)
2109 ; !IsNull (CheckPackage
, Node
)
2110 ; Node
= GetNextNode(CheckPackage
, Node
)
2113 // If the position matches, return the value
2115 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2116 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2123 returns the number of command line value parameters that were parsed.
2125 this will not include flags.
2127 @retval (UINTN)-1 No parsing has ocurred
2128 @return other The number of value parameters found
2132 ShellCommandLineGetCount(
2136 return (mTotalParameterCount
);
2140 Determins if a parameter is duplicated.
2142 If Param is not NULL then it will point to a callee allocated string buffer
2143 with the parameter value if a duplicate is found.
2145 If CheckPackage is NULL, then ASSERT.
2147 @param[in] CheckPackage The package of parsed command line arguments.
2148 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2150 @retval EFI_SUCCESS No parameters were duplicated.
2151 @retval EFI_DEVICE_ERROR A duplicate was found.
2155 ShellCommandLineCheckDuplicate (
2156 IN CONST LIST_ENTRY
*CheckPackage
,
2163 ASSERT(CheckPackage
!= NULL
);
2165 for ( Node1
= GetFirstNode(CheckPackage
)
2166 ; !IsNull (CheckPackage
, Node1
)
2167 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2169 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2170 ; !IsNull (CheckPackage
, Node2
)
2171 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2173 if (StrCmp(((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, ((SHELL_PARAM_PACKAGE
*)Node2
)->Name
) == 0) {
2174 if (Param
!= NULL
) {
2176 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2178 return (EFI_DEVICE_ERROR
);
2182 return (EFI_SUCCESS
);
2186 This is a find and replace function. it will return the NewString as a copy of
2187 SourceString with each instance of FindTarget replaced with ReplaceWith.
2189 If the string would grow bigger than NewSize it will halt and return error.
2191 @param[in] SourceString String with source buffer
2192 @param[in,out] NewString String with resultant buffer
2193 @param[in] NewSize Size in bytes of NewString
2194 @param[in] FindTarget String to look for
2195 @param[in[ ReplaceWith String to replace FindTarget with
2197 @retval EFI_INVALID_PARAMETER SourceString was NULL
2198 @retval EFI_INVALID_PARAMETER NewString was NULL
2199 @retval EFI_INVALID_PARAMETER FindTarget was NULL
2200 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
2201 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
2202 @retval EFI_INVALID_PARAMETER SourceString had length < 1
2203 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2204 the new string (truncation occurred)
2205 @retval EFI_SUCCESS the string was sucessfully copied with replacement
2211 IN CHAR16 CONST
*SourceString
,
2212 IN CHAR16
*NewString
,
2214 IN CONST CHAR16
*FindTarget
,
2215 IN CONST CHAR16
*ReplaceWith
2219 if ( (SourceString
== NULL
)
2220 || (NewString
== NULL
)
2221 || (FindTarget
== NULL
)
2222 || (ReplaceWith
== NULL
)
2223 || (StrLen(FindTarget
) < 1)
2224 || (StrLen(SourceString
) < 1)
2226 return (EFI_INVALID_PARAMETER
);
2228 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2229 while (*SourceString
!= CHAR_NULL
) {
2230 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0) {
2231 SourceString
+= StrLen(FindTarget
);
2232 Size
= StrSize(NewString
);
2233 if ((Size
+ (StrLen(ReplaceWith
)*sizeof(CHAR16
))) > NewSize
) {
2234 return (EFI_BUFFER_TOO_SMALL
);
2236 StrCat(NewString
, ReplaceWith
);
2238 Size
= StrSize(NewString
);
2239 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2240 return (EFI_BUFFER_TOO_SMALL
);
2242 StrnCat(NewString
, SourceString
, 1);
2246 return (EFI_SUCCESS
);
2250 Internal worker function to output a string.
2252 This function will output a string to the correct StdOut.
2254 @param[in] String The string to print out.
2256 @retval EFI_SUCCESS The operation was sucessful.
2257 @retval !EFI_SUCCESS The operation failed.
2262 IN CONST CHAR16
*String
2266 Size
= StrSize(String
) - sizeof(CHAR16
);
2267 if (mEfiShellParametersProtocol
!= NULL
) {
2268 return (mEfiShellParametersProtocol
->StdOut
->Write(mEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2270 if (mEfiShellInterface
!= NULL
) {
2271 return ( mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2274 return (EFI_UNSUPPORTED
);
2278 Print at a specific location on the screen.
2280 This function will move the cursor to a given screen location and print the specified string
2282 If -1 is specified for either the Row or Col the current screen location for BOTH
2285 if either Row or Col is out of range for the current console, then ASSERT
2286 if Format is NULL, then ASSERT
2288 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2289 the following additional flags:
2290 %N - Set output attribute to normal
2291 %H - Set output attribute to highlight
2292 %E - Set output attribute to error
2293 %B - Set output attribute to blue color
2294 %V - Set output attribute to green color
2296 Note: The background color is controlled by the shell command cls.
2298 @param[in] Row the row to print at
2299 @param[in] Col the column to print at
2300 @param[in] Format the format string
2301 @param[in] Marker the marker for the variable argument list
2303 @return the number of characters printed to the screen
2308 InternalShellPrintWorker(
2309 IN INT32 Col OPTIONAL
,
2310 IN INT32 Row OPTIONAL
,
2311 IN CONST CHAR16
*Format
,
2316 CHAR16
*PostReplaceFormat
;
2317 CHAR16
*PostReplaceFormat2
;
2320 UINTN NormalAttribute
;
2321 CHAR16
*ResumeLocation
;
2322 CHAR16
*FormatWalker
;
2324 BufferSize
= PcdGet16 (PcdShellLibMaxPrintBufferSize
);
2325 ASSERT(PcdGet16 (PcdShellLibMaxPrintBufferSize
) < PcdGet32 (PcdMaximumUnicodeStringLength
));
2326 PostReplaceFormat
= AllocateZeroPool (BufferSize
);
2327 ASSERT (PostReplaceFormat
!= NULL
);
2328 PostReplaceFormat2
= AllocateZeroPool (BufferSize
);
2329 ASSERT (PostReplaceFormat2
!= NULL
);
2332 // Back and forth each time fixing up 1 of our flags...
2334 Status
= CopyReplace(Format
, PostReplaceFormat
, BufferSize
, L
"%N", L
"%%N");
2335 ASSERT_EFI_ERROR(Status
);
2336 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%E", L
"%%E");
2337 ASSERT_EFI_ERROR(Status
);
2338 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%H", L
"%%H");
2339 ASSERT_EFI_ERROR(Status
);
2340 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%B", L
"%%B");
2341 ASSERT_EFI_ERROR(Status
);
2342 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%V", L
"%%V");
2343 ASSERT_EFI_ERROR(Status
);
2346 // Use the last buffer from replacing to print from...
2348 Return
= UnicodeVSPrint (PostReplaceFormat2
, BufferSize
, PostReplaceFormat
, Marker
);
2350 FreePool(PostReplaceFormat
);
2352 if (Col
!= -1 && Row
!= -1) {
2353 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2354 ASSERT_EFI_ERROR(Status
);
2357 NormalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2358 FormatWalker
= PostReplaceFormat2
;
2359 while (*FormatWalker
!= CHAR_NULL
) {
2361 // Find the next attribute change request
2363 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2364 if (ResumeLocation
!= NULL
) {
2365 *ResumeLocation
= CHAR_NULL
;
2368 // print the current FormatWalker string
2370 Status
= InternalPrintTo(FormatWalker
);
2371 ASSERT_EFI_ERROR(Status
);
2373 // update the attribute
2375 if (ResumeLocation
!= NULL
) {
2376 switch (*(ResumeLocation
+1)) {
2378 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2381 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2384 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2387 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2390 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2394 // Print a simple '%' symbol
2396 Status
= InternalPrintTo(L
"%");
2397 ASSERT_EFI_ERROR(Status
);
2398 ResumeLocation
= ResumeLocation
- 1;
2403 // reset to normal now...
2405 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2410 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2412 FormatWalker
= ResumeLocation
+ 2;
2415 FreePool(PostReplaceFormat2
);
2421 Print at a specific location on the screen.
2423 This function will move the cursor to a given screen location and print the specified string.
2425 If -1 is specified for either the Row or Col the current screen location for BOTH
2428 If either Row or Col is out of range for the current console, then ASSERT.
2429 If Format is NULL, then ASSERT.
2431 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2432 the following additional flags:
2433 %N - Set output attribute to normal
2434 %H - Set output attribute to highlight
2435 %E - Set output attribute to error
2436 %B - Set output attribute to blue color
2437 %V - Set output attribute to green color
2439 Note: The background color is controlled by the shell command cls.
2441 @param[in] Row the row to print at
2442 @param[in] Col the column to print at
2443 @param[in] Format the format string
2445 @return the number of characters printed to the screen
2451 IN INT32 Col OPTIONAL
,
2452 IN INT32 Row OPTIONAL
,
2453 IN CONST CHAR16
*Format
,
2459 VA_START (Marker
, Format
);
2460 Status
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2466 Print at a specific location on the screen.
2468 This function will move the cursor to a given screen location and print the specified string.
2470 If -1 is specified for either the Row or Col the current screen location for BOTH
2473 If either Row or Col is out of range for the current console, then ASSERT.
2474 If Format is NULL, then ASSERT.
2476 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2477 the following additional flags:
2478 %N - Set output attribute to normal
2479 %H - Set output attribute to highlight
2480 %E - Set output attribute to error
2481 %B - Set output attribute to blue color
2482 %V - Set output attribute to green color
2484 Note: The background color is controlled by the shell command cls.
2486 @param[in] Row the row to print at
2487 @param[in] Col the column to print at
2488 @param[in] HiiFormatStringId the format string Id for getting from Hii
2489 @param[in] HiiFormatHandle the format string Handle for getting from Hii
2491 @return the number of characters printed to the screen
2496 IN INT32 Col OPTIONAL
,
2497 IN INT32 Row OPTIONAL
,
2498 IN CONST EFI_STRING_ID HiiFormatStringId
,
2499 IN CONST EFI_HANDLE HiiFormatHandle
,
2504 CHAR16
*HiiFormatString
;
2507 VA_START (Marker
, HiiFormatHandle
);
2508 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
2509 ASSERT(HiiFormatString
!= NULL
);
2511 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2513 FreePool(HiiFormatString
);
2520 Function to determine if a given filename represents a file or a directory.
2522 @param[in] DirName Path to directory to test.
2524 @retval EFI_SUCCESS The Path represents a directory
2525 @retval EFI_NOT_FOUND The Path does not represent a directory
2526 @return other The path failed to open
2531 IN CONST CHAR16
*DirName
2535 EFI_FILE_HANDLE Handle
;
2539 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2540 if (EFI_ERROR(Status
)) {
2544 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2545 ShellCloseFile(&Handle
);
2546 return (EFI_SUCCESS
);
2548 ShellCloseFile(&Handle
);
2549 return (EFI_NOT_FOUND
);
2553 Function to determine if a given filename represents a file.
2555 @param[in] Name Path to file to test.
2557 @retval EFI_SUCCESS The Path represents a file.
2558 @retval EFI_NOT_FOUND The Path does not represent a file.
2559 @retval other The path failed to open.
2564 IN CONST CHAR16
*Name
2568 EFI_FILE_HANDLE Handle
;
2572 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
2573 if (EFI_ERROR(Status
)) {
2577 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
2578 ShellCloseFile(&Handle
);
2579 return (EFI_SUCCESS
);
2581 ShellCloseFile(&Handle
);
2582 return (EFI_NOT_FOUND
);
2586 Function to determine whether a string is decimal or hex representation of a number
2587 and return the number converted from the string.
2589 @param[in] String String representation of a number
2591 @retval all the number
2596 IN CONST CHAR16
*String
2599 CONST CHAR16
*Walker
;
2600 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
= Walker
+ 1);
2601 if (StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
2602 return (StrHexToUintn(Walker
));
2604 return (StrDecimalToUintn(Walker
));
2608 Safely append with automatic string resizing given length of Destination and
2609 desired length of copy from Source.
2611 append the first D characters of Source to the end of Destination, where D is
2612 the lesser of Count and the StrLen() of Source. If appending those D characters
2613 will fit within Destination (whose Size is given as CurrentSize) and
2614 still leave room for a null terminator, then those characters are appended,
2615 starting at the original terminating null of Destination, and a new terminating
2618 If appending D characters onto Destination will result in a overflow of the size
2619 given in CurrentSize the string will be grown such that the copy can be performed
2620 and CurrentSize will be updated to the new size.
2622 If Source is NULL, there is nothing to append, just return the current buffer in
2625 if Destination is NULL, then ASSERT()
2626 if Destination's current length (including NULL terminator) is already more then
2627 CurrentSize, then ASSERT()
2629 @param[in,out] Destination The String to append onto
2630 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2631 return possibly the new size (still in bytes). if NULL
2632 then allocate whatever is needed.
2633 @param[in] Source The String to append from
2634 @param[in] Count Maximum number of characters to append. if 0 then
2637 @return Destination return the resultant string.
2642 IN OUT CHAR16
**Destination
,
2643 IN OUT UINTN
*CurrentSize
,
2644 IN CONST CHAR16
*Source
,
2648 UINTN DestinationStartSize
;
2654 ASSERT(Destination
!= NULL
);
2657 // If there's nothing to do then just return Destination
2659 if (Source
== NULL
) {
2660 return (*Destination
);
2664 // allow for un-initialized pointers, based on size being 0
2666 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
2667 *Destination
= NULL
;
2671 // allow for NULL pointers address as Destination
2673 if (*Destination
!= NULL
) {
2674 ASSERT(CurrentSize
!= 0);
2675 DestinationStartSize
= StrSize(*Destination
);
2676 ASSERT(DestinationStartSize
<= *CurrentSize
);
2678 DestinationStartSize
= 0;
2679 // ASSERT(*CurrentSize == 0);
2683 // Append all of Source?
2686 Count
= StrLen(Source
);
2690 // Test and grow if required
2692 if (CurrentSize
!= NULL
) {
2693 NewSize
= *CurrentSize
;
2694 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
2695 NewSize
+= 2 * Count
* sizeof(CHAR16
);
2697 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
2698 *CurrentSize
= NewSize
;
2700 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
2704 // Now use standard StrnCat on a big enough buffer
2706 return StrnCat(*Destination
, Source
, Count
);