2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLib.h"
16 #include <ShellBase.h>
17 #include <Library/SortLib.h>
19 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
24 SHELL_PARAM_ITEM EmptyParamList
[] = {
27 SHELL_PARAM_ITEM SfoParamList
[] = {
31 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
32 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
33 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
34 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
35 EFI_HANDLE mEfiShellEnvironment2Handle
;
36 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
39 Check if a Unicode character is a hexadecimal character.
41 This internal function checks if a Unicode character is a
42 numeric character. The valid hexadecimal characters are
43 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
45 @param Char The character to check against.
47 @retval TRUE If the Char is a hexadecmial character.
48 @retval FALSE If the Char is not a hexadecmial character.
53 ShellIsHexaDecimalDigitCharacter (
57 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
61 Check if a Unicode character is a decimal character.
63 This internal function checks if a Unicode character is a
64 decimal character. The valid characters are
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 ShellIsDecimalDigitCharacter (
80 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
84 Helper function to find ShellEnvironment2 for constructor.
86 @param[in] ImageHandle A copy of the calling image's handle.
91 IN EFI_HANDLE ImageHandle
101 Status
= gBS
->OpenProtocol(ImageHandle
,
102 &gEfiShellEnvironment2Guid
,
103 (VOID
**)&mEfiShellEnvironment2
,
106 EFI_OPEN_PROTOCOL_GET_PROTOCOL
109 // look for the mEfiShellEnvironment2 protocol at a higher level
111 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
113 // figure out how big of a buffer we need.
115 Status
= gBS
->LocateHandle (ByProtocol
,
116 &gEfiShellEnvironment2Guid
,
117 NULL
, // ignored for ByProtocol
122 // maybe it's not there???
124 if (Status
== EFI_BUFFER_TOO_SMALL
) {
125 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
126 ASSERT(Buffer
!= NULL
);
127 Status
= gBS
->LocateHandle (ByProtocol
,
128 &gEfiShellEnvironment2Guid
,
129 NULL
, // ignored for ByProtocol
134 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
136 // now parse the list of returned handles
138 Status
= EFI_NOT_FOUND
;
139 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
140 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
141 &gEfiShellEnvironment2Guid
,
142 (VOID
**)&mEfiShellEnvironment2
,
145 EFI_OPEN_PROTOCOL_GET_PROTOCOL
147 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
148 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
149 Status
= EFI_SUCCESS
;
155 if (Buffer
!= NULL
) {
162 Function to do most of the work of the constructor. Allows for calling
163 multiple times without complete re-initialization.
165 @param[in] ImageHandle A copy of the ImageHandle.
166 @param[in] SystemTable A pointer to the SystemTable for the application.
168 @retval EFI_SUCCESS The operationw as successful.
172 ShellLibConstructorWorker (
173 IN EFI_HANDLE ImageHandle
,
174 IN EFI_SYSTEM_TABLE
*SystemTable
180 // UEFI 2.0 shell interfaces (used preferentially)
182 Status
= gBS
->OpenProtocol(
184 &gEfiShellProtocolGuid
,
185 (VOID
**)&gEfiShellProtocol
,
188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
190 if (EFI_ERROR(Status
)) {
192 // Search for the shell protocol
194 Status
= gBS
->LocateProtocol(
195 &gEfiShellProtocolGuid
,
197 (VOID
**)&gEfiShellProtocol
199 if (EFI_ERROR(Status
)) {
200 gEfiShellProtocol
= NULL
;
203 Status
= gBS
->OpenProtocol(
205 &gEfiShellParametersProtocolGuid
,
206 (VOID
**)&gEfiShellParametersProtocol
,
209 EFI_OPEN_PROTOCOL_GET_PROTOCOL
211 if (EFI_ERROR(Status
)) {
212 gEfiShellParametersProtocol
= NULL
;
215 if (gEfiShellParametersProtocol
== NULL
|| gEfiShellProtocol
== NULL
) {
217 // Moved to seperate function due to complexity
219 Status
= ShellFindSE2(ImageHandle
);
221 if (EFI_ERROR(Status
)) {
222 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
223 mEfiShellEnvironment2
= NULL
;
225 Status
= gBS
->OpenProtocol(ImageHandle
,
226 &gEfiShellInterfaceGuid
,
227 (VOID
**)&mEfiShellInterface
,
230 EFI_OPEN_PROTOCOL_GET_PROTOCOL
232 if (EFI_ERROR(Status
)) {
233 mEfiShellInterface
= NULL
;
238 // only success getting 2 of either the old or new, but no 1/2 and 1/2
240 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
241 (gEfiShellProtocol
!= NULL
&& gEfiShellParametersProtocol
!= NULL
) ) {
242 if (gEfiShellProtocol
!= NULL
) {
243 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
244 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
245 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
246 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
247 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
248 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
249 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
250 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
251 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
252 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
254 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
255 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
256 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
257 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
258 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
259 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
260 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
261 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
262 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
263 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
265 return (EFI_SUCCESS
);
267 return (EFI_NOT_FOUND
);
270 Constructor for the Shell library.
272 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
274 @param ImageHandle the image handle of the process
275 @param SystemTable the EFI System Table pointer
277 @retval EFI_SUCCESS the initialization was complete sucessfully
278 @return others an error ocurred during initialization
282 ShellLibConstructor (
283 IN EFI_HANDLE ImageHandle
,
284 IN EFI_SYSTEM_TABLE
*SystemTable
287 mEfiShellEnvironment2
= NULL
;
288 gEfiShellProtocol
= NULL
;
289 gEfiShellParametersProtocol
= NULL
;
290 mEfiShellInterface
= NULL
;
291 mEfiShellEnvironment2Handle
= NULL
;
294 // verify that auto initialize is not set false
296 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
297 return (EFI_SUCCESS
);
300 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
304 Destructor for the library. free any resources.
306 @param[in] ImageHandle A copy of the ImageHandle.
307 @param[in] SystemTable A pointer to the SystemTable for the application.
309 @retval EFI_SUCCESS The operation was successful.
310 @return An error from the CloseProtocol function.
315 IN EFI_HANDLE ImageHandle
,
316 IN EFI_SYSTEM_TABLE
*SystemTable
319 if (mEfiShellEnvironment2
!= NULL
) {
320 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
321 &gEfiShellEnvironment2Guid
,
324 mEfiShellEnvironment2
= NULL
;
326 if (mEfiShellInterface
!= NULL
) {
327 gBS
->CloseProtocol(ImageHandle
,
328 &gEfiShellInterfaceGuid
,
331 mEfiShellInterface
= NULL
;
333 if (gEfiShellProtocol
!= NULL
) {
334 gBS
->CloseProtocol(ImageHandle
,
335 &gEfiShellProtocolGuid
,
338 gEfiShellProtocol
= NULL
;
340 if (gEfiShellParametersProtocol
!= NULL
) {
341 gBS
->CloseProtocol(ImageHandle
,
342 &gEfiShellParametersProtocolGuid
,
345 gEfiShellParametersProtocol
= NULL
;
347 mEfiShellEnvironment2Handle
= NULL
;
349 return (EFI_SUCCESS
);
353 This function causes the shell library to initialize itself. If the shell library
354 is already initialized it will de-initialize all the current protocol poitners and
355 re-populate them again.
357 When the library is used with PcdShellLibAutoInitialize set to true this function
358 will return EFI_SUCCESS and perform no actions.
360 This function is intended for internal access for shell commands only.
362 @retval EFI_SUCCESS the initialization was complete sucessfully
371 // if auto initialize is not false then skip
373 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
374 return (EFI_SUCCESS
);
378 // deinit the current stuff
380 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
383 // init the new stuff
385 return (ShellLibConstructorWorker(gImageHandle
, gST
));
389 This function will retrieve the information about the file for the handle
390 specified and store it in allocated pool memory.
392 This function allocates a buffer to store the file's information. It is the
393 caller's responsibility to free the buffer
395 @param FileHandle The file handle of the file for which information is
398 @retval NULL information could not be retrieved.
400 @return the information about the file
405 IN SHELL_FILE_HANDLE FileHandle
408 return (FileFunctionMap
.GetFileInfo(FileHandle
));
412 This function sets the information about the file for the opened handle
415 @param[in] FileHandle The file handle of the file for which information
418 @param[in] FileInfo The information to set.
420 @retval EFI_SUCCESS The information was set.
421 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
422 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
423 @retval EFI_NO_MEDIA The device has no medium.
424 @retval EFI_DEVICE_ERROR The device reported an error.
425 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
426 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
427 @retval EFI_ACCESS_DENIED The file was opened read only.
428 @retval EFI_VOLUME_FULL The volume is full.
433 IN SHELL_FILE_HANDLE FileHandle
,
434 IN EFI_FILE_INFO
*FileInfo
437 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
441 This function will open a file or directory referenced by DevicePath.
443 This function opens a file with the open mode according to the file path. The
444 Attributes is valid only for EFI_FILE_MODE_CREATE.
446 @param FilePath on input the device path to the file. On output
447 the remaining device path.
448 @param DeviceHandle pointer to the system device handle.
449 @param FileHandle pointer to the file handle.
450 @param OpenMode the mode to open the file with.
451 @param Attributes the file's file attributes.
453 @retval EFI_SUCCESS The information was set.
454 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
455 @retval EFI_UNSUPPORTED Could not open the file path.
456 @retval EFI_NOT_FOUND The specified file could not be found on the
457 device or the file system could not be found on
459 @retval EFI_NO_MEDIA The device has no medium.
460 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
461 medium is no longer supported.
462 @retval EFI_DEVICE_ERROR The device reported an error.
463 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
464 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
465 @retval EFI_ACCESS_DENIED The file was opened read only.
466 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
468 @retval EFI_VOLUME_FULL The volume is full.
472 ShellOpenFileByDevicePath(
473 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
474 OUT EFI_HANDLE
*DeviceHandle
,
475 OUT SHELL_FILE_HANDLE
*FileHandle
,
482 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
483 EFI_FILE_PROTOCOL
*Handle1
;
484 EFI_FILE_PROTOCOL
*Handle2
;
486 if (FilePath
== NULL
|| FileHandle
== NULL
|| DeviceHandle
== NULL
) {
487 return (EFI_INVALID_PARAMETER
);
491 // which shell interface should we use
493 if (gEfiShellProtocol
!= NULL
) {
495 // use UEFI Shell 2.0 method.
497 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
498 if (FileName
== NULL
) {
499 return (EFI_INVALID_PARAMETER
);
501 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
508 // use old shell method.
510 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
513 if (EFI_ERROR (Status
)) {
516 Status
= gBS
->OpenProtocol(*DeviceHandle
,
517 &gEfiSimpleFileSystemProtocolGuid
,
518 (VOID
**)&EfiSimpleFileSystemProtocol
,
521 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
522 if (EFI_ERROR (Status
)) {
525 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
526 if (EFI_ERROR (Status
)) {
532 // go down directories one node at a time.
534 while (!IsDevicePathEnd (*FilePath
)) {
536 // For file system access each node should be a file path component
538 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
539 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
542 return (EFI_INVALID_PARAMETER
);
545 // Open this file path node
551 // Try to test opening an existing file
553 Status
= Handle2
->Open (
556 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
557 OpenMode
&~EFI_FILE_MODE_CREATE
,
562 // see if the error was that it needs to be created
564 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
565 Status
= Handle2
->Open (
568 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
574 // Close the last node
576 Handle2
->Close (Handle2
);
578 if (EFI_ERROR(Status
)) {
585 *FilePath
= NextDevicePathNode (*FilePath
);
589 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
591 *FileHandle
= (VOID
*)Handle1
;
592 return (EFI_SUCCESS
);
596 This function will open a file or directory referenced by filename.
598 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
599 otherwise, the Filehandle is NULL. The Attributes is valid only for
600 EFI_FILE_MODE_CREATE.
602 if FileName is NULL then ASSERT()
604 @param FileName pointer to file name
605 @param FileHandle pointer to the file handle.
606 @param OpenMode the mode to open the file with.
607 @param Attributes the file's file attributes.
609 @retval EFI_SUCCESS The information was set.
610 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
611 @retval EFI_UNSUPPORTED Could not open the file path.
612 @retval EFI_NOT_FOUND The specified file could not be found on the
613 device or the file system could not be found
615 @retval EFI_NO_MEDIA The device has no medium.
616 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
617 medium is no longer supported.
618 @retval EFI_DEVICE_ERROR The device reported an error.
619 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
620 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
621 @retval EFI_ACCESS_DENIED The file was opened read only.
622 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
624 @retval EFI_VOLUME_FULL The volume is full.
629 IN CONST CHAR16
*FileName
,
630 OUT SHELL_FILE_HANDLE
*FileHandle
,
635 EFI_HANDLE DeviceHandle
;
636 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
638 EFI_FILE_INFO
*FileInfo
;
641 // ASSERT if FileName is NULL
643 ASSERT(FileName
!= NULL
);
645 if (FileName
== NULL
) {
646 return (EFI_INVALID_PARAMETER
);
649 if (gEfiShellProtocol
!= NULL
) {
650 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
&& (Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
651 return ShellCreateDirectory(FileName
, FileHandle
);
654 // Use UEFI Shell 2.0 method
656 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
659 if (StrCmp(FileName
, L
"NUL") != 0 && !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
660 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
661 ASSERT(FileInfo
!= NULL
);
662 FileInfo
->Attribute
= Attributes
;
663 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
669 // Using EFI Shell version
670 // this means convert name to path and call that function
671 // since this will use EFI method again that will open it.
673 ASSERT(mEfiShellEnvironment2
!= NULL
);
674 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
675 if (FilePath
!= NULL
) {
676 return (ShellOpenFileByDevicePath(&FilePath
,
682 return (EFI_DEVICE_ERROR
);
685 This function create a directory
687 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
688 otherwise, the Filehandle is NULL. If the directory already existed, this
689 function opens the existing directory.
691 @param DirectoryName pointer to directory name
692 @param FileHandle pointer to the file handle.
694 @retval EFI_SUCCESS The information was set.
695 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
696 @retval EFI_UNSUPPORTED Could not open the file path.
697 @retval EFI_NOT_FOUND The specified file could not be found on the
698 device or the file system could not be found
700 @retval EFI_NO_MEDIA The device has no medium.
701 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
702 medium is no longer supported.
703 @retval EFI_DEVICE_ERROR The device reported an error.
704 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
705 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
706 @retval EFI_ACCESS_DENIED The file was opened read only.
707 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
709 @retval EFI_VOLUME_FULL The volume is full.
710 @sa ShellOpenFileByName
714 ShellCreateDirectory(
715 IN CONST CHAR16
*DirectoryName
,
716 OUT SHELL_FILE_HANDLE
*FileHandle
719 if (gEfiShellProtocol
!= NULL
) {
721 // Use UEFI Shell 2.0 method
723 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
728 return (ShellOpenFileByName(DirectoryName
,
730 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
737 This function reads information from an opened file.
739 If FileHandle is not a directory, the function reads the requested number of
740 bytes from the file at the file's current position and returns them in Buffer.
741 If the read goes beyond the end of the file, the read length is truncated to the
742 end of the file. The file's current position is increased by the number of bytes
743 returned. If FileHandle is a directory, the function reads the directory entry
744 at the file's current position and returns the entry in Buffer. If the Buffer
745 is not large enough to hold the current directory entry, then
746 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
747 BufferSize is set to be the size of the buffer needed to read the entry. On
748 success, the current position is updated to the next directory entry. If there
749 are no more directory entries, the read returns a zero-length buffer.
750 EFI_FILE_INFO is the structure returned as the directory entry.
752 @param FileHandle the opened file handle
753 @param BufferSize on input the size of buffer in bytes. on return
754 the number of bytes written.
755 @param Buffer the buffer to put read data into.
757 @retval EFI_SUCCESS Data was read.
758 @retval EFI_NO_MEDIA The device has no media.
759 @retval EFI_DEVICE_ERROR The device reported an error.
760 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
761 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
768 IN SHELL_FILE_HANDLE FileHandle
,
769 IN OUT UINTN
*BufferSize
,
773 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
778 Write data to a file.
780 This function writes the specified number of bytes to the file at the current
781 file position. The current file position is advanced the actual number of bytes
782 written, which is returned in BufferSize. Partial writes only occur when there
783 has been a data error during the write attempt (such as "volume space full").
784 The file is automatically grown to hold the data if required. Direct writes to
785 opened directories are not supported.
787 @param FileHandle The opened file for writing
788 @param BufferSize on input the number of bytes in Buffer. On output
789 the number of bytes written.
790 @param Buffer the buffer containing data to write is stored.
792 @retval EFI_SUCCESS Data was written.
793 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
794 @retval EFI_NO_MEDIA The device has no media.
795 @retval EFI_DEVICE_ERROR The device reported an error.
796 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
797 @retval EFI_WRITE_PROTECTED The device is write-protected.
798 @retval EFI_ACCESS_DENIED The file was open for read only.
799 @retval EFI_VOLUME_FULL The volume is full.
804 IN SHELL_FILE_HANDLE FileHandle
,
805 IN OUT UINTN
*BufferSize
,
809 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
813 Close an open file handle.
815 This function closes a specified file handle. All "dirty" cached file data is
816 flushed to the device, and the file is closed. In all cases the handle is
819 @param FileHandle the file handle to close.
821 @retval EFI_SUCCESS the file handle was closed sucessfully.
826 IN SHELL_FILE_HANDLE
*FileHandle
829 return (FileFunctionMap
.CloseFile(*FileHandle
));
833 Delete a file and close the handle
835 This function closes and deletes a file. In all cases the file handle is closed.
836 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
837 returned, but the handle is still closed.
839 @param FileHandle the file handle to delete
841 @retval EFI_SUCCESS the file was closed sucessfully
842 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
844 @retval INVALID_PARAMETER One of the parameters has an invalid value.
849 IN SHELL_FILE_HANDLE
*FileHandle
852 return (FileFunctionMap
.DeleteFile(*FileHandle
));
856 Set the current position in a file.
858 This function sets the current file position for the handle to the position
859 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
860 absolute positioning is supported, and seeking past the end of the file is
861 allowed (a subsequent write would grow the file). Seeking to position
862 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
863 If FileHandle is a directory, the only position that may be set is zero. This
864 has the effect of starting the read process of the directory entries over.
866 @param FileHandle The file handle on which the position is being set
867 @param Position Byte position from begining of file
869 @retval EFI_SUCCESS Operation completed sucessfully.
870 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
872 @retval INVALID_PARAMETER One of the parameters has an invalid value.
876 ShellSetFilePosition (
877 IN SHELL_FILE_HANDLE FileHandle
,
881 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
885 Gets a file's current position
887 This function retrieves the current file position for the file handle. For
888 directories, the current file position has no meaning outside of the file
889 system driver and as such the operation is not supported. An error is returned
890 if FileHandle is a directory.
892 @param FileHandle The open file handle on which to get the position.
893 @param Position Byte position from begining of file.
895 @retval EFI_SUCCESS the operation completed sucessfully.
896 @retval INVALID_PARAMETER One of the parameters has an invalid value.
897 @retval EFI_UNSUPPORTED the request is not valid on directories.
901 ShellGetFilePosition (
902 IN SHELL_FILE_HANDLE FileHandle
,
906 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
909 Flushes data on a file
911 This function flushes all modified data associated with a file to a device.
913 @param FileHandle The file handle on which to flush data
915 @retval EFI_SUCCESS The data was flushed.
916 @retval EFI_NO_MEDIA The device has no media.
917 @retval EFI_DEVICE_ERROR The device reported an error.
918 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
919 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
920 @retval EFI_ACCESS_DENIED The file was opened for read only.
925 IN SHELL_FILE_HANDLE FileHandle
928 return (FileFunctionMap
.FlushFile(FileHandle
));
932 Retrieves the first file from a directory
934 This function opens a directory and gets the first file's info in the
935 directory. Caller can use ShellFindNextFile() to get other files. When
936 complete the caller is responsible for calling FreePool() on Buffer.
938 @param DirHandle The file handle of the directory to search
939 @param Buffer Pointer to buffer for file's information
941 @retval EFI_SUCCESS Found the first file.
942 @retval EFI_NOT_FOUND Cannot find the directory.
943 @retval EFI_NO_MEDIA The device has no media.
944 @retval EFI_DEVICE_ERROR The device reported an error.
945 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
946 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
952 IN SHELL_FILE_HANDLE DirHandle
,
953 OUT EFI_FILE_INFO
**Buffer
957 // pass to file handle lib
959 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
962 Retrieves the next file in a directory.
964 To use this function, caller must call the ShellFindFirstFile() to get the
965 first file, and then use this function get other files. This function can be
966 called for several times to get each file's information in the directory. If
967 the call of ShellFindNextFile() got the last file in the directory, the next
968 call of this function has no file to get. *NoFile will be set to TRUE and the
969 Buffer memory will be automatically freed.
971 @param DirHandle the file handle of the directory
972 @param Buffer pointer to buffer for file's information
973 @param NoFile pointer to boolean when last file is found
975 @retval EFI_SUCCESS Found the next file, or reached last file
976 @retval EFI_NO_MEDIA The device has no media.
977 @retval EFI_DEVICE_ERROR The device reported an error.
978 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
983 IN SHELL_FILE_HANDLE DirHandle
,
984 OUT EFI_FILE_INFO
*Buffer
,
989 // pass to file handle lib
991 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
994 Retrieve the size of a file.
996 if FileHandle is NULL then ASSERT()
997 if Size is NULL then ASSERT()
999 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1002 @param FileHandle file handle from which size is retrieved
1003 @param Size pointer to size
1005 @retval EFI_SUCCESS operation was completed sucessfully
1006 @retval EFI_DEVICE_ERROR cannot access the file
1011 IN SHELL_FILE_HANDLE FileHandle
,
1015 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1018 Retrieves the status of the break execution flag
1020 this function is useful to check whether the application is being asked to halt by the shell.
1022 @retval TRUE the execution break is enabled
1023 @retval FALSE the execution break is not enabled
1027 ShellGetExecutionBreakFlag(
1032 // Check for UEFI Shell 2.0 protocols
1034 if (gEfiShellProtocol
!= NULL
) {
1037 // We are using UEFI Shell 2.0; see if the event has been triggered
1039 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1046 // using EFI Shell; call the function to check
1048 if (mEfiShellEnvironment2
!= NULL
) {
1049 return (mEfiShellEnvironment2
->GetExecutionBreak());
1055 return the value of an environment variable
1057 this function gets the value of the environment variable set by the
1058 ShellSetEnvironmentVariable function
1060 @param EnvKey The key name of the environment variable.
1062 @retval NULL the named environment variable does not exist.
1063 @return != NULL pointer to the value of the environment variable
1067 ShellGetEnvironmentVariable (
1068 IN CONST CHAR16
*EnvKey
1072 // Check for UEFI Shell 2.0 protocols
1074 if (gEfiShellProtocol
!= NULL
) {
1075 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1079 // Check for EFI shell
1081 if (mEfiShellEnvironment2
!= NULL
) {
1082 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1088 set the value of an environment variable
1090 This function changes the current value of the specified environment variable. If the
1091 environment variable exists and the Value is an empty string, then the environment
1092 variable is deleted. If the environment variable exists and the Value is not an empty
1093 string, then the value of the environment variable is changed. If the environment
1094 variable does not exist and the Value is an empty string, there is no action. If the
1095 environment variable does not exist and the Value is a non-empty string, then the
1096 environment variable is created and assigned the specified value.
1098 This is not supported pre-UEFI Shell 2.0.
1100 @param EnvKey The key name of the environment variable.
1101 @param EnvVal The Value of the environment variable
1102 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1104 @retval EFI_SUCCESS the operation was completed sucessfully
1105 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1109 ShellSetEnvironmentVariable (
1110 IN CONST CHAR16
*EnvKey
,
1111 IN CONST CHAR16
*EnvVal
,
1116 // Check for UEFI Shell 2.0 protocols
1118 if (gEfiShellProtocol
!= NULL
) {
1119 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1123 // This feature does not exist under EFI shell
1125 return (EFI_UNSUPPORTED
);
1129 Cause the shell to parse and execute a command line.
1131 This function creates a nested instance of the shell and executes the specified
1132 command (CommandLine) with the specified environment (Environment). Upon return,
1133 the status code returned by the specified command is placed in StatusCode.
1134 If Environment is NULL, then the current environment is used and all changes made
1135 by the commands executed will be reflected in the current environment. If the
1136 Environment is non-NULL, then the changes made will be discarded.
1137 The CommandLine is executed from the current working directory on the current
1140 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1141 environment. The values pointed to by the parameters will be unchanged by the
1142 ShellExecute() function. The Output parameter has no effect in a
1143 UEFI Shell 2.0 environment.
1145 @param[in] ParentHandle The parent image starting the operation.
1146 @param[in] CommandLine The pointer to a NULL terminated command line.
1147 @param[in] Output True to display debug output. False to hide it.
1148 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1149 in the form "x=y". If NULL, the current set is used.
1150 @param[out] Status The status of the run command line.
1152 @retval EFI_SUCCESS The operation completed sucessfully. Status
1153 contains the status code returned.
1154 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1155 @retval EFI_OUT_OF_RESOURCES Out of resources.
1156 @retval EFI_UNSUPPORTED The operation is not allowed.
1161 IN EFI_HANDLE
*ParentHandle
,
1162 IN CHAR16
*CommandLine OPTIONAL
,
1163 IN BOOLEAN Output OPTIONAL
,
1164 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1165 OUT EFI_STATUS
*Status OPTIONAL
1169 // Check for UEFI Shell 2.0 protocols
1171 if (gEfiShellProtocol
!= NULL
) {
1173 // Call UEFI Shell 2.0 version (not using Output parameter)
1175 return (gEfiShellProtocol
->Execute(ParentHandle
,
1177 EnvironmentVariables
,
1182 // Check for EFI shell
1184 if (mEfiShellEnvironment2
!= NULL
) {
1186 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1187 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1189 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1194 return (EFI_UNSUPPORTED
);
1197 Retreives the current directory path
1199 If the DeviceName is NULL, it returns the current device's current directory
1200 name. If the DeviceName is not NULL, it returns the current directory name
1203 @param DeviceName the name of the drive to get directory on
1205 @retval NULL the directory does not exist
1206 @return != NULL the directory
1210 ShellGetCurrentDir (
1211 IN CHAR16
* CONST DeviceName OPTIONAL
1215 // Check for UEFI Shell 2.0 protocols
1217 if (gEfiShellProtocol
!= NULL
) {
1218 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1222 // Check for EFI shell
1224 if (mEfiShellEnvironment2
!= NULL
) {
1225 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1231 sets (enabled or disabled) the page break mode
1233 when page break mode is enabled the screen will stop scrolling
1234 and wait for operator input before scrolling a subsequent screen.
1236 @param CurrentState TRUE to enable and FALSE to disable
1240 ShellSetPageBreakMode (
1241 IN BOOLEAN CurrentState
1245 // check for enabling
1247 if (CurrentState
!= 0x00) {
1249 // check for UEFI Shell 2.0
1251 if (gEfiShellProtocol
!= NULL
) {
1253 // Enable with UEFI 2.0 Shell
1255 gEfiShellProtocol
->EnablePageBreak();
1259 // Check for EFI shell
1261 if (mEfiShellEnvironment2
!= NULL
) {
1263 // Enable with EFI Shell
1265 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1271 // check for UEFI Shell 2.0
1273 if (gEfiShellProtocol
!= NULL
) {
1275 // Disable with UEFI 2.0 Shell
1277 gEfiShellProtocol
->DisablePageBreak();
1281 // Check for EFI shell
1283 if (mEfiShellEnvironment2
!= NULL
) {
1285 // Disable with EFI Shell
1287 mEfiShellEnvironment2
->DisablePageBreak ();
1295 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1296 /// This allows for the struct to be populated.
1303 SHELL_FILE_HANDLE Handle
;
1304 EFI_FILE_INFO
*Info
;
1305 } EFI_SHELL_FILE_INFO_NO_CONST
;
1308 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1310 if OldStyleFileList is NULL then ASSERT()
1312 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1313 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1314 the ShellCloseFileMetaArg function.
1316 @param[in] FileList the EFI shell list type
1317 @param[in, out] ListHead the list to add to
1319 @retval the resultant head of the double linked new format list;
1323 InternalShellConvertFileListType (
1324 IN LIST_ENTRY
*FileList
,
1325 IN OUT LIST_ENTRY
*ListHead
1328 SHELL_FILE_ARG
*OldInfo
;
1330 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1335 ASSERT(FileList
!= NULL
);
1336 ASSERT(ListHead
!= NULL
);
1339 // enumerate through each member of the old list and copy
1341 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1342 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1343 ASSERT(OldInfo
!= NULL
);
1346 // Skip ones that failed to open...
1348 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1353 // make sure the old list was valid
1355 ASSERT(OldInfo
->Info
!= NULL
);
1356 ASSERT(OldInfo
->FullName
!= NULL
);
1357 ASSERT(OldInfo
->FileName
!= NULL
);
1360 // allocate a new EFI_SHELL_FILE_INFO object
1362 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1363 ASSERT(NewInfo
!= NULL
);
1364 if (NewInfo
== NULL
) {
1369 // copy the simple items
1371 NewInfo
->Handle
= OldInfo
->Handle
;
1372 NewInfo
->Status
= OldInfo
->Status
;
1374 // old shell checks for 0 not NULL
1375 OldInfo
->Handle
= 0;
1378 // allocate new space to copy strings and structure
1380 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1381 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1382 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1385 // make sure all the memory allocations were sucessful
1387 ASSERT(NewInfo
->FullName
!= NULL
);
1388 ASSERT(NewInfo
->FileName
!= NULL
);
1389 ASSERT(NewInfo
->Info
!= NULL
);
1392 // Copt the strings and structure
1394 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1395 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1396 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1399 // add that to the list
1401 InsertTailList(ListHead
, &NewInfo
->Link
);
1406 Opens a group of files based on a path.
1408 This function uses the Arg to open all the matching files. Each matched
1409 file has a SHELL_FILE_ARG structure to record the file information. These
1410 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1411 structures from ListHead to access each file. This function supports wildcards
1412 and will process '?' and '*' as such. the list must be freed with a call to
1413 ShellCloseFileMetaArg().
1415 If you are NOT appending to an existing list *ListHead must be NULL. If
1416 *ListHead is NULL then it must be callee freed.
1418 @param Arg pointer to path string
1419 @param OpenMode mode to open files with
1420 @param ListHead head of linked list of results
1422 @retval EFI_SUCCESS the operation was sucessful and the list head
1423 contains the list of opened files
1424 @return != EFI_SUCCESS the operation failed
1426 @sa InternalShellConvertFileListType
1430 ShellOpenFileMetaArg (
1433 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1437 LIST_ENTRY mOldStyleFileList
;
1440 // ASSERT that Arg and ListHead are not NULL
1442 ASSERT(Arg
!= NULL
);
1443 ASSERT(ListHead
!= NULL
);
1446 // Check for UEFI Shell 2.0 protocols
1448 if (gEfiShellProtocol
!= NULL
) {
1449 if (*ListHead
== NULL
) {
1450 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1451 if (*ListHead
== NULL
) {
1452 return (EFI_OUT_OF_RESOURCES
);
1454 InitializeListHead(&((*ListHead
)->Link
));
1456 Status
= gEfiShellProtocol
->OpenFileList(Arg
,
1459 if (EFI_ERROR(Status
)) {
1460 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1462 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1464 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1465 FreePool(*ListHead
);
1467 return (EFI_NOT_FOUND
);
1473 // Check for EFI shell
1475 if (mEfiShellEnvironment2
!= NULL
) {
1477 // make sure the list head is initialized
1479 InitializeListHead(&mOldStyleFileList
);
1482 // Get the EFI Shell list of files
1484 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1485 if (EFI_ERROR(Status
)) {
1490 if (*ListHead
== NULL
) {
1491 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1492 if (*ListHead
== NULL
) {
1493 return (EFI_OUT_OF_RESOURCES
);
1495 InitializeListHead(&((*ListHead
)->Link
));
1499 // Convert that to equivalent of UEFI Shell 2.0 structure
1501 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1504 // Free the EFI Shell version that was converted.
1506 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1508 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1509 FreePool(*ListHead
);
1511 Status
= EFI_NOT_FOUND
;
1516 return (EFI_UNSUPPORTED
);
1519 Free the linked list returned from ShellOpenFileMetaArg.
1521 if ListHead is NULL then ASSERT().
1523 @param ListHead the pointer to free.
1525 @retval EFI_SUCCESS the operation was sucessful.
1529 ShellCloseFileMetaArg (
1530 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1536 // ASSERT that ListHead is not NULL
1538 ASSERT(ListHead
!= NULL
);
1541 // Check for UEFI Shell 2.0 protocols
1543 if (gEfiShellProtocol
!= NULL
) {
1544 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1545 } else if (mEfiShellEnvironment2
!= NULL
) {
1547 // Since this is EFI Shell version we need to free our internally made copy
1550 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1551 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1552 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1553 RemoveEntryList(Node
);
1554 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1555 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1556 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1557 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1558 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1563 return (EFI_UNSUPPORTED
);
1567 Find a file by searching the CWD and then the path.
1569 If FileName is NULL then ASSERT.
1571 If the return value is not NULL then the memory must be caller freed.
1573 @param FileName Filename string.
1575 @retval NULL the file was not found
1576 @return !NULL the full path to the file.
1581 IN CONST CHAR16
*FileName
1585 SHELL_FILE_HANDLE Handle
;
1589 CONST CHAR16
*Walker
;
1596 // First make sure its not an absolute path.
1598 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1599 if (!EFI_ERROR(Status
)){
1600 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1601 ASSERT(RetVal
== NULL
);
1602 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1603 ShellCloseFile(&Handle
);
1606 ShellCloseFile(&Handle
);
1610 Path
= ShellGetEnvironmentVariable(L
"cwd");
1612 Size
= StrSize(Path
);
1613 Size
+= StrSize(FileName
);
1614 TestPath
= AllocateZeroPool(Size
);
1615 ASSERT(TestPath
!= NULL
);
1616 if (TestPath
== NULL
) {
1619 StrCpy(TestPath
, Path
);
1620 StrCat(TestPath
, FileName
);
1621 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1622 if (!EFI_ERROR(Status
)){
1623 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1624 ASSERT(RetVal
== NULL
);
1625 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1626 ShellCloseFile(&Handle
);
1630 ShellCloseFile(&Handle
);
1635 Path
= ShellGetEnvironmentVariable(L
"path");
1637 Size
= StrSize(Path
)+sizeof(CHAR16
);
1638 Size
+= StrSize(FileName
);
1639 TestPath
= AllocateZeroPool(Size
);
1640 if (TestPath
== NULL
) {
1643 Walker
= (CHAR16
*)Path
;
1645 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1646 if (TestPath
!= NULL
) {
1647 TempChar
= StrStr(TestPath
, L
";");
1648 if (TempChar
!= NULL
) {
1649 *TempChar
= CHAR_NULL
;
1651 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1652 StrCat(TestPath
, L
"\\");
1654 if (FileName
[0] == L
'\\') {
1657 StrCat(TestPath
, FileName
);
1658 if (StrStr(Walker
, L
";") != NULL
) {
1659 Walker
= StrStr(Walker
, L
";") + 1;
1663 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1664 if (!EFI_ERROR(Status
)){
1665 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1666 ASSERT(RetVal
== NULL
);
1667 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1668 ShellCloseFile(&Handle
);
1671 ShellCloseFile(&Handle
);
1675 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1682 Find a file by searching the CWD and then the path with a variable set of file
1683 extensions. If the file is not found it will append each extension in the list
1684 in the order provided and return the first one that is successful.
1686 If FileName is NULL, then ASSERT.
1687 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1689 If the return value is not NULL then the memory must be caller freed.
1691 @param[in] FileName Filename string.
1692 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1694 @retval NULL The file was not found.
1695 @retval !NULL The path to the file.
1699 ShellFindFilePathEx (
1700 IN CONST CHAR16
*FileName
,
1701 IN CONST CHAR16
*FileExtension
1706 CONST CHAR16
*ExtensionWalker
;
1711 ASSERT(FileName
!= NULL
);
1712 if (FileExtension
== NULL
) {
1713 return (ShellFindFilePath(FileName
));
1715 RetVal
= ShellFindFilePath(FileName
);
1716 if (RetVal
!= NULL
) {
1719 Size
= StrSize(FileName
);
1720 Size
+= StrSize(FileExtension
);
1721 TestPath
= AllocateZeroPool(Size
);
1722 ASSERT(TestPath
!= NULL
);
1723 if (TestPath
== NULL
) {
1726 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1727 StrCpy(TestPath
, FileName
);
1728 if (ExtensionWalker
!= NULL
) {
1729 StrCat(TestPath
, ExtensionWalker
);
1731 TempChar
= StrStr(TestPath
, L
";");
1732 if (TempChar
!= NULL
) {
1733 *TempChar
= CHAR_NULL
;
1735 RetVal
= ShellFindFilePath(TestPath
);
1736 if (RetVal
!= NULL
) {
1739 ASSERT(ExtensionWalker
!= NULL
);
1740 TempChar2
= StrStr(ExtensionWalker
, L
";");
1749 SHELL_PARAM_TYPE Type
;
1751 UINTN OriginalPosition
;
1752 } SHELL_PARAM_PACKAGE
;
1755 Checks the list of valid arguments and returns TRUE if the item was found. If the
1756 return value is TRUE then the type parameter is set also.
1758 if CheckList is NULL then ASSERT();
1759 if Name is NULL then ASSERT();
1760 if Type is NULL then ASSERT();
1762 @param Name pointer to Name of parameter found
1763 @param CheckList List to check against
1764 @param Type pointer to type of parameter if it was found
1766 @retval TRUE the Parameter was found. Type is valid.
1767 @retval FALSE the Parameter was not found. Type is not valid.
1771 InternalIsOnCheckList (
1772 IN CONST CHAR16
*Name
,
1773 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1774 OUT SHELL_PARAM_TYPE
*Type
1777 SHELL_PARAM_ITEM
*TempListItem
;
1781 // ASSERT that all 3 pointer parameters aren't NULL
1783 ASSERT(CheckList
!= NULL
);
1784 ASSERT(Type
!= NULL
);
1785 ASSERT(Name
!= NULL
);
1788 // question mark and page break mode are always supported
1790 if ((StrCmp(Name
, L
"-?") == 0) ||
1791 (StrCmp(Name
, L
"-b") == 0)
1798 // Enumerate through the list
1800 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1802 // If the Type is TypeStart only check the first characters of the passed in param
1803 // If it matches set the type and return TRUE
1805 if (TempListItem
->Type
== TypeStart
) {
1806 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1807 *Type
= TempListItem
->Type
;
1811 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1812 if (TempString
!= NULL
) {
1813 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1814 *Type
= TempListItem
->Type
;
1815 FreePool(TempString
);
1818 FreePool(TempString
);
1820 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1821 *Type
= TempListItem
->Type
;
1829 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1831 @param[in] Name pointer to Name of parameter found
1832 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1834 @retval TRUE the Parameter is a flag.
1835 @retval FALSE the Parameter not a flag.
1840 IN CONST CHAR16
*Name
,
1841 IN BOOLEAN AlwaysAllowNumbers
1845 // ASSERT that Name isn't NULL
1847 ASSERT(Name
!= NULL
);
1850 // If we accept numbers then dont return TRUE. (they will be values)
1852 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
) {
1857 // If the Name has a /, +, or - as the first character return TRUE
1859 if ((Name
[0] == L
'/') ||
1860 (Name
[0] == L
'-') ||
1869 Checks the command line arguments passed against the list of valid ones.
1871 If no initialization is required, then return RETURN_SUCCESS.
1873 @param[in] CheckList pointer to list of parameters to check
1874 @param[out] CheckPackage pointer to pointer to list checked values
1875 @param[out] ProblemParam optional pointer to pointer to unicode string for
1876 the paramater that caused failure. If used then the
1877 caller is responsible for freeing the memory.
1878 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1879 @param[in] Argv pointer to array of parameters
1880 @param[in] Argc Count of parameters in Argv
1881 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
1883 @retval EFI_SUCCESS The operation completed sucessfully.
1884 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1885 @retval EFI_INVALID_PARAMETER A parameter was invalid
1886 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1887 duplicated. the duplicated command line argument
1888 was returned in ProblemParam if provided.
1889 @retval EFI_NOT_FOUND a argument required a value that was missing.
1890 the invalid command line argument was returned in
1891 ProblemParam if provided.
1895 InternalCommandLineParse (
1896 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1897 OUT LIST_ENTRY
**CheckPackage
,
1898 OUT CHAR16
**ProblemParam OPTIONAL
,
1899 IN BOOLEAN AutoPageBreak
,
1900 IN CONST CHAR16
**Argv
,
1902 IN BOOLEAN AlwaysAllowNumbers
1906 SHELL_PARAM_TYPE CurrentItemType
;
1907 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1911 CONST CHAR16
*TempPointer
;
1913 CurrentItemPackage
= NULL
;
1919 // If there is only 1 item we dont need to do anything
1922 *CheckPackage
= NULL
;
1923 return (EFI_SUCCESS
);
1929 ASSERT(CheckList
!= NULL
);
1930 ASSERT(Argv
!= NULL
);
1933 // initialize the linked list
1935 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1936 if (*CheckPackage
== NULL
) {
1937 return EFI_OUT_OF_RESOURCES
;
1939 InitializeListHead(*CheckPackage
);
1942 // loop through each of the arguments
1944 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1945 if (Argv
[LoopCounter
] == NULL
) {
1947 // do nothing for NULL argv
1949 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
1951 // We might have leftover if last parameter didnt have optional value
1953 if (GetItemValue
!= 0) {
1955 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1960 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
1961 ASSERT(CurrentItemPackage
!= NULL
);
1962 CurrentItemPackage
->Name
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1963 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1964 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1965 CurrentItemPackage
->Type
= CurrentItemType
;
1966 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1967 CurrentItemPackage
->Value
= NULL
;
1970 // Does this flag require a value
1972 switch (CurrentItemPackage
->Type
) {
1974 // possibly trigger the next loop(s) to populate the value of this item
1980 case TypeDoubleValue
:
1985 GetItemValue
= (UINTN
)(-1);
1990 // this item has no value expected; we are done
1992 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1993 ASSERT(GetItemValue
== 0);
1996 } else if (GetItemValue
!= 0 && !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
)) {
1997 ASSERT(CurrentItemPackage
!= NULL
);
1999 // get the item VALUE for a previous flag
2001 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
2002 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2003 if (ValueSize
== 0) {
2004 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2006 StrCat(CurrentItemPackage
->Value
, L
" ");
2007 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2009 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2011 if (GetItemValue
== 0) {
2012 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2014 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) ){ //|| ProblemParam == NULL) {
2016 // add this one as a non-flag
2019 TempPointer
= Argv
[LoopCounter
];
2020 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2021 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2022 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2026 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2027 ASSERT(CurrentItemPackage
!= NULL
);
2028 CurrentItemPackage
->Name
= NULL
;
2029 CurrentItemPackage
->Type
= TypePosition
;
2030 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(TempPointer
));
2031 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2032 StrCpy(CurrentItemPackage
->Value
, TempPointer
);
2033 CurrentItemPackage
->OriginalPosition
= Count
++;
2034 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2037 // this was a non-recognised flag... error!
2039 if (ProblemParam
!= NULL
) {
2040 *ProblemParam
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
2041 ASSERT(*ProblemParam
!= NULL
);
2042 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
2044 ShellCommandLineFreeVarList(*CheckPackage
);
2045 *CheckPackage
= NULL
;
2046 return (EFI_VOLUME_CORRUPTED
);
2049 if (GetItemValue
!= 0) {
2051 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2054 // support for AutoPageBreak
2056 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2057 ShellSetPageBreakMode(TRUE
);
2059 return (EFI_SUCCESS
);
2063 Checks the command line arguments passed against the list of valid ones.
2064 Optionally removes NULL values first.
2066 If no initialization is required, then return RETURN_SUCCESS.
2068 @param[in] CheckList The pointer to list of parameters to check.
2069 @param[out] CheckPackage The package of checked values.
2070 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2071 the paramater that caused failure.
2072 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2073 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2075 @retval EFI_SUCCESS The operation completed sucessfully.
2076 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2077 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2078 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2079 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2080 of the command line arguments was returned in
2081 ProblemParam if provided.
2082 @retval EFI_NOT_FOUND A argument required a value that was missing.
2083 The invalid command line argument was returned in
2084 ProblemParam if provided.
2088 ShellCommandLineParseEx (
2089 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2090 OUT LIST_ENTRY
**CheckPackage
,
2091 OUT CHAR16
**ProblemParam OPTIONAL
,
2092 IN BOOLEAN AutoPageBreak
,
2093 IN BOOLEAN AlwaysAllowNumbers
2097 // ASSERT that CheckList and CheckPackage aren't NULL
2099 ASSERT(CheckList
!= NULL
);
2100 ASSERT(CheckPackage
!= NULL
);
2103 // Check for UEFI Shell 2.0 protocols
2105 if (gEfiShellParametersProtocol
!= NULL
) {
2106 return (InternalCommandLineParse(CheckList
,
2110 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2111 gEfiShellParametersProtocol
->Argc
,
2112 AlwaysAllowNumbers
));
2116 // ASSERT That EFI Shell is not required
2118 ASSERT (mEfiShellInterface
!= NULL
);
2119 return (InternalCommandLineParse(CheckList
,
2123 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2124 mEfiShellInterface
->Argc
,
2125 AlwaysAllowNumbers
));
2129 Frees shell variable list that was returned from ShellCommandLineParse.
2131 This function will free all the memory that was used for the CheckPackage
2132 list of postprocessed shell arguments.
2134 this function has no return value.
2136 if CheckPackage is NULL, then return
2138 @param CheckPackage the list to de-allocate
2142 ShellCommandLineFreeVarList (
2143 IN LIST_ENTRY
*CheckPackage
2149 // check for CheckPackage == NULL
2151 if (CheckPackage
== NULL
) {
2156 // for each node in the list
2158 for ( Node
= GetFirstNode(CheckPackage
)
2159 ; !IsListEmpty(CheckPackage
)
2160 ; Node
= GetFirstNode(CheckPackage
)
2163 // Remove it from the list
2165 RemoveEntryList(Node
);
2168 // if it has a name free the name
2170 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2171 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2175 // if it has a value free the value
2177 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2178 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2182 // free the node structure
2184 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2187 // free the list head node
2189 FreePool(CheckPackage
);
2192 Checks for presence of a flag parameter
2194 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2196 if CheckPackage is NULL then return FALSE.
2197 if KeyString is NULL then ASSERT()
2199 @param CheckPackage The package of parsed command line arguments
2200 @param KeyString the Key of the command line argument to check for
2202 @retval TRUE the flag is on the command line
2203 @retval FALSE the flag is not on the command line
2207 ShellCommandLineGetFlag (
2208 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2209 IN CONST CHAR16
* CONST KeyString
2216 // ASSERT that both CheckPackage and KeyString aren't NULL
2218 ASSERT(KeyString
!= NULL
);
2221 // return FALSE for no package
2223 if (CheckPackage
== NULL
) {
2228 // enumerate through the list of parametrs
2230 for ( Node
= GetFirstNode(CheckPackage
)
2231 ; !IsNull (CheckPackage
, Node
)
2232 ; Node
= GetNextNode(CheckPackage
, Node
)
2235 // If the Name matches, return TRUE (and there may be NULL name)
2237 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2239 // If Type is TypeStart then only compare the begining of the strings
2241 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2242 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2246 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2247 if (TempString
!= NULL
) {
2248 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2249 FreePool(TempString
);
2252 FreePool(TempString
);
2254 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2262 Returns value from command line argument.
2264 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2266 If CheckPackage is NULL, then return NULL.
2268 @param[in] CheckPackage The package of parsed command line arguments.
2269 @param[in] KeyString The Key of the command line argument to check for.
2271 @retval NULL The flag is not on the command line.
2272 @retval !=NULL The pointer to unicode string of the value.
2276 ShellCommandLineGetValue (
2277 IN CONST LIST_ENTRY
*CheckPackage
,
2278 IN CHAR16
*KeyString
2285 // check for CheckPackage == NULL
2287 if (CheckPackage
== NULL
) {
2292 // enumerate through the list of parametrs
2294 for ( Node
= GetFirstNode(CheckPackage
)
2295 ; !IsNull (CheckPackage
, Node
)
2296 ; Node
= GetNextNode(CheckPackage
, Node
)
2299 // If the Name matches, return TRUE (and there may be NULL name)
2301 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2303 // If Type is TypeStart then only compare the begining of the strings
2305 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2306 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2307 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2310 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2311 if (TempString
!= NULL
) {
2312 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2313 FreePool(TempString
);
2314 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2316 FreePool(TempString
);
2318 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2319 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2327 Returns raw value from command line argument.
2329 Raw value parameters are in the form of "value" in a specific position in the list.
2331 If CheckPackage is NULL, then return NULL.
2333 @param[in] CheckPackage The package of parsed command line arguments.
2334 @param[in] Position The position of the value.
2336 @retval NULL The flag is not on the command line.
2337 @retval !=NULL The pointer to unicode string of the value.
2341 ShellCommandLineGetRawValue (
2342 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2349 // check for CheckPackage == NULL
2351 if (CheckPackage
== NULL
) {
2356 // enumerate through the list of parametrs
2358 for ( Node
= GetFirstNode(CheckPackage
)
2359 ; !IsNull (CheckPackage
, Node
)
2360 ; Node
= GetNextNode(CheckPackage
, Node
)
2363 // If the position matches, return the value
2365 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2366 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2373 returns the number of command line value parameters that were parsed.
2375 this will not include flags.
2377 @param[in] CheckPackage The package of parsed command line arguments.
2379 @retval (UINTN)-1 No parsing has ocurred
2380 @return other The number of value parameters found
2384 ShellCommandLineGetCount(
2385 IN CONST LIST_ENTRY
*CheckPackage
2391 if (CheckPackage
== NULL
) {
2394 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2395 ; !IsNull (CheckPackage
, Node1
)
2396 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2398 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2406 Determins if a parameter is duplicated.
2408 If Param is not NULL then it will point to a callee allocated string buffer
2409 with the parameter value if a duplicate is found.
2411 If CheckPackage is NULL, then ASSERT.
2413 @param[in] CheckPackage The package of parsed command line arguments.
2414 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2416 @retval EFI_SUCCESS No parameters were duplicated.
2417 @retval EFI_DEVICE_ERROR A duplicate was found.
2421 ShellCommandLineCheckDuplicate (
2422 IN CONST LIST_ENTRY
*CheckPackage
,
2429 ASSERT(CheckPackage
!= NULL
);
2431 for ( Node1
= GetFirstNode(CheckPackage
)
2432 ; !IsNull (CheckPackage
, Node1
)
2433 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2435 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2436 ; !IsNull (CheckPackage
, Node2
)
2437 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2439 if ((((SHELL_PARAM_PACKAGE
*)Node1
)->Name
!= NULL
) && (((SHELL_PARAM_PACKAGE
*)Node2
)->Name
!= NULL
) && StrCmp(((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, ((SHELL_PARAM_PACKAGE
*)Node2
)->Name
) == 0) {
2440 if (Param
!= NULL
) {
2442 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2444 return (EFI_DEVICE_ERROR
);
2448 return (EFI_SUCCESS
);
2452 This is a find and replace function. Upon successful return the NewString is a copy of
2453 SourceString with each instance of FindTarget replaced with ReplaceWith.
2455 If SourceString and NewString overlap the behavior is undefined.
2457 If the string would grow bigger than NewSize it will halt and return error.
2459 @param[in] SourceString The string with source buffer.
2460 @param[in, out] NewString The string with resultant buffer.
2461 @param[in] NewSize The size in bytes of NewString.
2462 @param[in] FindTarget The string to look for.
2463 @param[in] ReplaceWith The string to replace FindTarget with.
2464 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2465 immediately before it.
2466 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2468 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2469 @retval EFI_INVALID_PARAMETER NewString was NULL.
2470 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2471 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2472 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2473 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2474 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2475 the new string (truncation occurred).
2476 @retval EFI_SUCCESS The string was successfully copied with replacement.
2480 ShellCopySearchAndReplace(
2481 IN CHAR16 CONST
*SourceString
,
2482 IN OUT CHAR16
*NewString
,
2484 IN CONST CHAR16
*FindTarget
,
2485 IN CONST CHAR16
*ReplaceWith
,
2486 IN CONST BOOLEAN SkipPreCarrot
,
2487 IN CONST BOOLEAN ParameterReplacing
2493 if ( (SourceString
== NULL
)
2494 || (NewString
== NULL
)
2495 || (FindTarget
== NULL
)
2496 || (ReplaceWith
== NULL
)
2497 || (StrLen(FindTarget
) < 1)
2498 || (StrLen(SourceString
) < 1)
2500 return (EFI_INVALID_PARAMETER
);
2503 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2504 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2506 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2507 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2509 if (Replace
== NULL
) {
2510 return (EFI_OUT_OF_RESOURCES
);
2512 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2513 while (*SourceString
!= CHAR_NULL
) {
2515 // if we find the FindTarget and either Skip == FALSE or Skip and we
2516 // dont have a carrot do a replace...
2518 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2519 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2521 SourceString
+= StrLen(FindTarget
);
2522 Size
= StrSize(NewString
);
2523 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2525 return (EFI_BUFFER_TOO_SMALL
);
2527 StrCat(NewString
, Replace
);
2529 Size
= StrSize(NewString
);
2530 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2532 return (EFI_BUFFER_TOO_SMALL
);
2534 StrnCat(NewString
, SourceString
, 1);
2539 return (EFI_SUCCESS
);
2543 Internal worker function to output a string.
2545 This function will output a string to the correct StdOut.
2547 @param[in] String The string to print out.
2549 @retval EFI_SUCCESS The operation was sucessful.
2550 @retval !EFI_SUCCESS The operation failed.
2555 IN CONST CHAR16
*String
2559 Size
= StrSize(String
) - sizeof(CHAR16
);
2561 return (EFI_SUCCESS
);
2563 if (gEfiShellParametersProtocol
!= NULL
) {
2564 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2566 if (mEfiShellInterface
!= NULL
) {
2568 // Divide in half for old shell. Must be string length not size.
2571 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2574 return (EFI_UNSUPPORTED
);
2578 Print at a specific location on the screen.
2580 This function will move the cursor to a given screen location and print the specified string
2582 If -1 is specified for either the Row or Col the current screen location for BOTH
2585 if either Row or Col is out of range for the current console, then ASSERT
2586 if Format is NULL, then ASSERT
2588 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2589 the following additional flags:
2590 %N - Set output attribute to normal
2591 %H - Set output attribute to highlight
2592 %E - Set output attribute to error
2593 %B - Set output attribute to blue color
2594 %V - Set output attribute to green color
2596 Note: The background color is controlled by the shell command cls.
2598 @param[in] Col the column to print at
2599 @param[in] Row the row to print at
2600 @param[in] Format the format string
2601 @param[in] Marker the marker for the variable argument list
2603 @return EFI_SUCCESS The operation was successful.
2604 @return EFI_DEVICE_ERROR The console device reported an error.
2608 InternalShellPrintWorker(
2609 IN INT32 Col OPTIONAL
,
2610 IN INT32 Row OPTIONAL
,
2611 IN CONST CHAR16
*Format
,
2616 CHAR16
*ResumeLocation
;
2617 CHAR16
*FormatWalker
;
2618 UINTN OriginalAttribute
;
2619 CHAR16
*mPostReplaceFormat
;
2620 CHAR16
*mPostReplaceFormat2
;
2622 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2623 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2625 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2626 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2627 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2628 return (EFI_OUT_OF_RESOURCES
);
2631 Status
= EFI_SUCCESS
;
2632 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2635 // Back and forth each time fixing up 1 of our flags...
2637 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2638 ASSERT_EFI_ERROR(Status
);
2639 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2640 ASSERT_EFI_ERROR(Status
);
2641 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2642 ASSERT_EFI_ERROR(Status
);
2643 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2644 ASSERT_EFI_ERROR(Status
);
2645 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2646 ASSERT_EFI_ERROR(Status
);
2649 // Use the last buffer from replacing to print from...
2651 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2653 if (Col
!= -1 && Row
!= -1) {
2654 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2657 FormatWalker
= mPostReplaceFormat2
;
2658 while (*FormatWalker
!= CHAR_NULL
) {
2660 // Find the next attribute change request
2662 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2663 if (ResumeLocation
!= NULL
) {
2664 *ResumeLocation
= CHAR_NULL
;
2667 // print the current FormatWalker string
2669 if (StrLen(FormatWalker
)>0) {
2670 Status
= InternalPrintTo(FormatWalker
);
2671 if (EFI_ERROR(Status
)) {
2677 // update the attribute
2679 if (ResumeLocation
!= NULL
) {
2680 if (*(ResumeLocation
-1) == L
'^') {
2682 // Print a simple '%' symbol
2684 Status
= InternalPrintTo(L
"%");
2685 ResumeLocation
= ResumeLocation
- 1;
2687 switch (*(ResumeLocation
+1)) {
2689 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2692 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2695 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2698 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2701 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2705 // Print a simple '%' symbol
2707 Status
= InternalPrintTo(L
"%");
2708 if (EFI_ERROR(Status
)) {
2711 ResumeLocation
= ResumeLocation
- 1;
2717 // reset to normal now...
2723 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2725 FormatWalker
= ResumeLocation
+ 2;
2728 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2730 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2731 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2736 Print at a specific location on the screen.
2738 This function will move the cursor to a given screen location and print the specified string.
2740 If -1 is specified for either the Row or Col the current screen location for BOTH
2743 If either Row or Col is out of range for the current console, then ASSERT.
2744 If Format is NULL, then ASSERT.
2746 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2747 the following additional flags:
2748 %N - Set output attribute to normal
2749 %H - Set output attribute to highlight
2750 %E - Set output attribute to error
2751 %B - Set output attribute to blue color
2752 %V - Set output attribute to green color
2754 Note: The background color is controlled by the shell command cls.
2756 @param[in] Col the column to print at
2757 @param[in] Row the row to print at
2758 @param[in] Format the format string
2759 @param[in] ... The variable argument list.
2761 @return EFI_SUCCESS The printing was successful.
2762 @return EFI_DEVICE_ERROR The console device reported an error.
2767 IN INT32 Col OPTIONAL
,
2768 IN INT32 Row OPTIONAL
,
2769 IN CONST CHAR16
*Format
,
2775 if (Format
== NULL
) {
2776 return (EFI_INVALID_PARAMETER
);
2778 VA_START (Marker
, Format
);
2779 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2785 Print at a specific location on the screen.
2787 This function will move the cursor to a given screen location and print the specified string.
2789 If -1 is specified for either the Row or Col the current screen location for BOTH
2792 If either Row or Col is out of range for the current console, then ASSERT.
2793 If Format is NULL, then ASSERT.
2795 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2796 the following additional flags:
2797 %N - Set output attribute to normal.
2798 %H - Set output attribute to highlight.
2799 %E - Set output attribute to error.
2800 %B - Set output attribute to blue color.
2801 %V - Set output attribute to green color.
2803 Note: The background color is controlled by the shell command cls.
2805 @param[in] Col The column to print at.
2806 @param[in] Row The row to print at.
2807 @param[in] Language The language of the string to retrieve. If this parameter
2808 is NULL, then the current platform language is used.
2809 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2810 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2811 @param[in] ... The variable argument list.
2813 @return EFI_SUCCESS The printing was successful.
2814 @return EFI_DEVICE_ERROR The console device reported an error.
2819 IN INT32 Col OPTIONAL
,
2820 IN INT32 Row OPTIONAL
,
2821 IN CONST CHAR8
*Language OPTIONAL
,
2822 IN CONST EFI_STRING_ID HiiFormatStringId
,
2823 IN CONST EFI_HANDLE HiiFormatHandle
,
2828 CHAR16
*HiiFormatString
;
2831 VA_START (Marker
, HiiFormatHandle
);
2832 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2833 ASSERT(HiiFormatString
!= NULL
);
2835 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2837 SHELL_FREE_NON_NULL(HiiFormatString
);
2844 Function to determine if a given filename represents a file or a directory.
2846 @param[in] DirName Path to directory to test.
2848 @retval EFI_SUCCESS The Path represents a directory
2849 @retval EFI_NOT_FOUND The Path does not represent a directory
2850 @return other The path failed to open
2855 IN CONST CHAR16
*DirName
2859 SHELL_FILE_HANDLE Handle
;
2860 CHAR16
*TempLocation
;
2861 CHAR16
*TempLocation2
;
2863 ASSERT(DirName
!= NULL
);
2866 TempLocation
= NULL
;
2868 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2869 if (EFI_ERROR(Status
)) {
2871 // try good logic first.
2873 if (gEfiShellProtocol
!= NULL
) {
2874 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
2875 TempLocation2
= StrStr(TempLocation
, L
":");
2876 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
2877 *(TempLocation2
+1) = CHAR_NULL
;
2879 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
2880 FreePool(TempLocation
);
2881 return (EFI_SUCCESS
);
2883 FreePool(TempLocation
);
2886 // probably a map name?!?!!?
2888 TempLocation
= StrStr(DirName
, L
"\\");
2889 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
2890 return (EFI_SUCCESS
);
2896 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2897 ShellCloseFile(&Handle
);
2898 return (EFI_SUCCESS
);
2900 ShellCloseFile(&Handle
);
2901 return (EFI_NOT_FOUND
);
2905 Function to determine if a given filename represents a file.
2907 @param[in] Name Path to file to test.
2909 @retval EFI_SUCCESS The Path represents a file.
2910 @retval EFI_NOT_FOUND The Path does not represent a file.
2911 @retval other The path failed to open.
2916 IN CONST CHAR16
*Name
2920 SHELL_FILE_HANDLE Handle
;
2922 ASSERT(Name
!= NULL
);
2926 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
2927 if (EFI_ERROR(Status
)) {
2931 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
2932 ShellCloseFile(&Handle
);
2933 return (EFI_SUCCESS
);
2935 ShellCloseFile(&Handle
);
2936 return (EFI_NOT_FOUND
);
2940 Function to determine if a given filename represents a file.
2942 This will search the CWD and then the Path.
2944 If Name is NULL, then ASSERT.
2946 @param[in] Name Path to file to test.
2948 @retval EFI_SUCCESS The Path represents a file.
2949 @retval EFI_NOT_FOUND The Path does not represent a file.
2950 @retval other The path failed to open.
2955 IN CONST CHAR16
*Name
2961 if (!EFI_ERROR(ShellIsFile(Name
))) {
2962 return (EFI_SUCCESS
);
2965 NewName
= ShellFindFilePath(Name
);
2966 if (NewName
== NULL
) {
2967 return (EFI_NOT_FOUND
);
2969 Status
= ShellIsFile(NewName
);
2975 Function to determine whether a string is decimal or hex representation of a number
2976 and return the number converted from the string.
2978 @param[in] String String representation of a number
2981 @retval (UINTN)(-1) An error ocurred.
2986 IN CONST CHAR16
*String
2994 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
)) {
2998 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
2999 return ((UINTN
)RetVal
);
3001 return ((UINTN
)(-1));
3005 Safely append with automatic string resizing given length of Destination and
3006 desired length of copy from Source.
3008 append the first D characters of Source to the end of Destination, where D is
3009 the lesser of Count and the StrLen() of Source. If appending those D characters
3010 will fit within Destination (whose Size is given as CurrentSize) and
3011 still leave room for a NULL terminator, then those characters are appended,
3012 starting at the original terminating NULL of Destination, and a new terminating
3015 If appending D characters onto Destination will result in a overflow of the size
3016 given in CurrentSize the string will be grown such that the copy can be performed
3017 and CurrentSize will be updated to the new size.
3019 If Source is NULL, there is nothing to append, just return the current buffer in
3022 if Destination is NULL, then ASSERT()
3023 if Destination's current length (including NULL terminator) is already more then
3024 CurrentSize, then ASSERT()
3026 @param[in, out] Destination The String to append onto
3027 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3028 return possibly the new size (still in bytes). if NULL
3029 then allocate whatever is needed.
3030 @param[in] Source The String to append from
3031 @param[in] Count Maximum number of characters to append. if 0 then
3034 @return Destination return the resultant string.
3039 IN OUT CHAR16
**Destination
,
3040 IN OUT UINTN
*CurrentSize
,
3041 IN CONST CHAR16
*Source
,
3045 UINTN DestinationStartSize
;
3051 ASSERT(Destination
!= NULL
);
3054 // If there's nothing to do then just return Destination
3056 if (Source
== NULL
) {
3057 return (*Destination
);
3061 // allow for un-initialized pointers, based on size being 0
3063 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3064 *Destination
= NULL
;
3068 // allow for NULL pointers address as Destination
3070 if (*Destination
!= NULL
) {
3071 ASSERT(CurrentSize
!= 0);
3072 DestinationStartSize
= StrSize(*Destination
);
3073 ASSERT(DestinationStartSize
<= *CurrentSize
);
3075 DestinationStartSize
= 0;
3076 // ASSERT(*CurrentSize == 0);
3080 // Append all of Source?
3083 Count
= StrLen(Source
);
3087 // Test and grow if required
3089 if (CurrentSize
!= NULL
) {
3090 NewSize
= *CurrentSize
;
3091 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3092 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3094 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3095 ASSERT(*Destination
!= NULL
);
3096 *CurrentSize
= NewSize
;
3098 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
3099 ASSERT(*Destination
!= NULL
);
3103 // Now use standard StrnCat on a big enough buffer
3105 if (*Destination
== NULL
) {
3108 return StrnCat(*Destination
, Source
, Count
);
3112 Prompt the user and return the resultant answer to the requestor.
3114 This function will display the requested question on the shell prompt and then
3115 wait for an apropriate answer to be input from the console.
3117 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3118 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3120 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3123 In either case *Response must be callee freed if Response was not NULL;
3125 @param Type What type of question is asked. This is used to filter the input
3126 to prevent invalid answers to question.
3127 @param Prompt Pointer to string prompt to use to request input.
3128 @param Response Pointer to Response which will be populated upon return.
3130 @retval EFI_SUCCESS The operation was sucessful.
3131 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3132 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3133 @return other The operation failed.
3137 ShellPromptForResponse (
3138 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3139 IN CHAR16
*Prompt OPTIONAL
,
3140 IN OUT VOID
**Response OPTIONAL
3146 SHELL_PROMPT_RESPONSE
*Resp
;
3150 Status
= EFI_UNSUPPORTED
;
3154 if (Type
!= ShellPromptResponseTypeFreeform
) {
3155 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3157 return (EFI_OUT_OF_RESOURCES
);
3162 case ShellPromptResponseTypeQuitContinue
:
3163 if (Prompt
!= NULL
) {
3164 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3167 // wait for valid response
3169 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3170 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3171 ASSERT_EFI_ERROR(Status
);
3172 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3173 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3174 *Resp
= ShellPromptResponseQuit
;
3176 *Resp
= ShellPromptResponseContinue
;
3179 case ShellPromptResponseTypeYesNoCancel
:
3180 if (Prompt
!= NULL
) {
3181 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3184 // wait for valid response
3186 *Resp
= ShellPromptResponseMax
;
3187 while (*Resp
== ShellPromptResponseMax
) {
3188 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3189 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3190 ASSERT_EFI_ERROR(Status
);
3191 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3192 switch (Key
.UnicodeChar
) {
3195 *Resp
= ShellPromptResponseYes
;
3199 *Resp
= ShellPromptResponseNo
;
3203 *Resp
= ShellPromptResponseCancel
;
3207 break; case ShellPromptResponseTypeYesNoAllCancel
:
3208 if (Prompt
!= NULL
) {
3209 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3212 // wait for valid response
3214 *Resp
= ShellPromptResponseMax
;
3215 while (*Resp
== ShellPromptResponseMax
) {
3216 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3217 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3218 ASSERT_EFI_ERROR(Status
);
3219 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3220 switch (Key
.UnicodeChar
) {
3223 *Resp
= ShellPromptResponseYes
;
3227 *Resp
= ShellPromptResponseNo
;
3231 *Resp
= ShellPromptResponseAll
;
3235 *Resp
= ShellPromptResponseCancel
;
3240 case ShellPromptResponseTypeEnterContinue
:
3241 case ShellPromptResponseTypeAnyKeyContinue
:
3242 if (Prompt
!= NULL
) {
3243 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3246 // wait for valid response
3248 *Resp
= ShellPromptResponseMax
;
3249 while (*Resp
== ShellPromptResponseMax
) {
3250 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3251 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3252 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3253 ASSERT_EFI_ERROR(Status
);
3254 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3255 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3256 *Resp
= ShellPromptResponseContinue
;
3260 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3261 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3262 ASSERT_EFI_ERROR(Status
);
3263 *Resp
= ShellPromptResponseContinue
;
3268 case ShellPromptResponseTypeYesNo
:
3269 if (Prompt
!= NULL
) {
3270 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3273 // wait for valid response
3275 *Resp
= ShellPromptResponseMax
;
3276 while (*Resp
== ShellPromptResponseMax
) {
3277 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3278 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3279 ASSERT_EFI_ERROR(Status
);
3280 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3281 switch (Key
.UnicodeChar
) {
3284 *Resp
= ShellPromptResponseYes
;
3288 *Resp
= ShellPromptResponseNo
;
3293 case ShellPromptResponseTypeFreeform
:
3294 if (Prompt
!= NULL
) {
3295 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3298 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3299 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3300 ASSERT_EFI_ERROR(Status
);
3301 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3302 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3305 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3306 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3310 // This is the location to add new prompt types.
3316 if (Response
!= NULL
) {
3319 } else if (Buffer
!= NULL
) {
3326 if (Buffer
!= NULL
) {
3331 ShellPrintEx(-1, -1, L
"\r\n");
3336 Prompt the user and return the resultant answer to the requestor.
3338 This function is the same as ShellPromptForResponse, except that the prompt is
3339 automatically pulled from HII.
3341 @param Type What type of question is asked. This is used to filter the input
3342 to prevent invalid answers to question.
3343 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3344 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3345 @param Response Pointer to Response which will be populated upon return.
3347 @retval EFI_SUCCESS the operation was sucessful.
3348 @return other the operation failed.
3350 @sa ShellPromptForResponse
3354 ShellPromptForResponseHii (
3355 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3356 IN CONST EFI_STRING_ID HiiFormatStringId
,
3357 IN CONST EFI_HANDLE HiiFormatHandle
,
3358 IN OUT VOID
**Response
3364 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3365 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3371 Function to determin if an entire string is a valid number.
3373 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3375 @param[in] String The string to evaluate.
3376 @param[in] ForceHex TRUE - always assume hex.
3377 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3379 @retval TRUE It is all numeric (dec/hex) characters.
3380 @retval FALSE There is a non-numeric character.
3384 InternalShellIsHexOrDecimalNumber (
3385 IN CONST CHAR16
*String
,
3386 IN CONST BOOLEAN ForceHex
,
3387 IN CONST BOOLEAN StopAtSpace
3393 // chop off a single negative sign
3395 if (String
!= NULL
&& *String
== L
'-') {
3399 if (String
== NULL
) {
3404 // chop leading zeroes
3406 while(String
!= NULL
&& *String
== L
'0'){
3410 // allow '0x' or '0X', but not 'x' or 'X'
3412 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3413 if (*(String
-1) != L
'0') {
3415 // we got an x without a preceeding 0
3421 } else if (ForceHex
) {
3428 // loop through the remaining characters and use the lib function
3430 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3432 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3436 if (!ShellIsDecimalDigitCharacter(*String
)) {
3446 Function to determine if a given filename exists.
3448 @param[in] Name Path to test.
3450 @retval EFI_SUCCESS The Path represents a file.
3451 @retval EFI_NOT_FOUND The Path does not represent a file.
3452 @retval other The path failed to open.
3457 IN CONST CHAR16
*Name
3461 EFI_SHELL_FILE_INFO
*List
;
3463 ASSERT(Name
!= NULL
);
3466 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3467 if (EFI_ERROR(Status
)) {
3471 ShellCloseFileMetaArg(&List
);
3473 return (EFI_SUCCESS
);
3477 Convert a Unicode character to upper case only if
3478 it maps to a valid small-case ASCII character.
3480 This internal function only deal with Unicode character
3481 which maps to a valid small-case ASCII character, i.e.
3482 L'a' to L'z'. For other Unicode character, the input character
3483 is returned directly.
3485 @param Char The character to convert.
3487 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3488 @retval Unchanged Otherwise.
3493 InternalShellCharToUpper (
3497 if (Char
>= L
'a' && Char
<= L
'z') {
3498 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3505 Convert a Unicode character to numerical value.
3507 This internal function only deal with Unicode character
3508 which maps to a valid hexadecimal ASII character, i.e.
3509 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3510 Unicode character, the value returned does not make sense.
3512 @param Char The character to convert.
3514 @return The numerical value converted.
3519 InternalShellHexCharToUintn (
3523 if (ShellIsDecimalDigitCharacter (Char
)) {
3527 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3531 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3533 This function returns a value of type UINTN by interpreting the contents
3534 of the Unicode string specified by String as a hexadecimal number.
3535 The format of the input Unicode string String is:
3537 [spaces][zeros][x][hexadecimal digits].
3539 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3540 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3541 If "x" appears in the input string, it must be prefixed with at least one 0.
3542 The function will ignore the pad space, which includes spaces or tab characters,
3543 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3544 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3545 first valid hexadecimal digit. Then, the function stops at the first character that is
3546 a not a valid hexadecimal character or NULL, whichever one comes first.
3548 If String has only pad spaces, then zero is returned.
3549 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3550 then zero is returned.
3552 @param[in] String A pointer to a Null-terminated Unicode string.
3553 @param[out] Value Upon a successful return the value of the conversion.
3554 @param[in] StopAtSpace FALSE to skip spaces.
3556 @retval EFI_SUCCESS The conversion was successful.
3557 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3558 @retval EFI_DEVICE_ERROR An overflow occured.
3562 InternalShellStrHexToUint64 (
3563 IN CONST CHAR16
*String
,
3565 IN CONST BOOLEAN StopAtSpace
3570 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3571 return (EFI_INVALID_PARAMETER
);
3575 // Ignore the pad spaces (space or tab)
3577 while ((*String
== L
' ') || (*String
== L
'\t')) {
3582 // Ignore leading Zeros after the spaces
3584 while (*String
== L
'0') {
3588 if (InternalShellCharToUpper (*String
) == L
'X') {
3589 if (*(String
- 1) != L
'0') {
3601 // Skip spaces if requested
3603 while (StopAtSpace
&& *String
== L
' ') {
3607 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3609 // If the Hex Number represented by String overflows according
3610 // to the range defined by UINTN, then ASSERT().
3612 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3613 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3614 return (EFI_DEVICE_ERROR
);
3617 Result
= (LShiftU64(Result
, 4));
3618 Result
+= InternalShellHexCharToUintn (*String
);
3622 // Skip spaces if requested
3624 while (StopAtSpace
&& *String
== L
' ') {
3630 return (EFI_SUCCESS
);
3634 Convert a Null-terminated Unicode decimal string to a value of
3637 This function returns a value of type UINT64 by interpreting the contents
3638 of the Unicode string specified by String as a decimal number. The format
3639 of the input Unicode string String is:
3641 [spaces] [decimal digits].
3643 The valid decimal digit character is in the range [0-9]. The
3644 function will ignore the pad space, which includes spaces or
3645 tab characters, before [decimal digits]. The running zero in the
3646 beginning of [decimal digits] will be ignored. Then, the function
3647 stops at the first character that is a not a valid decimal character
3648 or a Null-terminator, whichever one comes first.
3650 If String has only pad spaces, then 0 is returned.
3651 If String has no pad spaces or valid decimal digits,
3654 @param[in] String A pointer to a Null-terminated Unicode string.
3655 @param[out] Value Upon a successful return the value of the conversion.
3656 @param[in] StopAtSpace FALSE to skip spaces.
3658 @retval EFI_SUCCESS The conversion was successful.
3659 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3660 @retval EFI_DEVICE_ERROR An overflow occured.
3664 InternalShellStrDecimalToUint64 (
3665 IN CONST CHAR16
*String
,
3667 IN CONST BOOLEAN StopAtSpace
3672 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3673 return (EFI_INVALID_PARAMETER
);
3677 // Ignore the pad spaces (space or tab)
3679 while ((*String
== L
' ') || (*String
== L
'\t')) {
3684 // Ignore leading Zeros after the spaces
3686 while (*String
== L
'0') {
3693 // Skip spaces if requested
3695 while (StopAtSpace
&& *String
== L
' ') {
3698 while (ShellIsDecimalDigitCharacter (*String
)) {
3700 // If the number represented by String overflows according
3701 // to the range defined by UINT64, then ASSERT().
3704 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3705 return (EFI_DEVICE_ERROR
);
3708 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3712 // Stop at spaces if requested
3714 if (StopAtSpace
&& *String
== L
' ') {
3721 return (EFI_SUCCESS
);
3725 Function to verify and convert a string to its numerical value.
3727 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3729 @param[in] String The string to evaluate.
3730 @param[out] Value Upon a successful return the value of the conversion.
3731 @param[in] ForceHex TRUE - always assume hex.
3732 @param[in] StopAtSpace FALSE to skip spaces.
3734 @retval EFI_SUCCESS The conversion was successful.
3735 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3736 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3740 ShellConvertStringToUint64(
3741 IN CONST CHAR16
*String
,
3743 IN CONST BOOLEAN ForceHex
,
3744 IN CONST BOOLEAN StopAtSpace
3748 CONST CHAR16
*Walker
;
3754 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
)) {
3757 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
)) {
3758 return (EFI_INVALID_PARAMETER
);
3761 return (EFI_INVALID_PARAMETER
);
3766 // Chop off leading spaces
3768 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
3771 // make sure we have something left that is numeric.
3773 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
)) {
3774 return (EFI_INVALID_PARAMETER
);
3778 // do the conversion.
3780 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
3781 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
3783 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
3786 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
3787 return (EFI_NOT_FOUND
);
3790 if (Value
!= NULL
) {
3798 Function to determin if an entire string is a valid number.
3800 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3802 @param[in] String The string to evaluate.
3803 @param[in] ForceHex TRUE - always assume hex.
3804 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3806 @retval TRUE It is all numeric (dec/hex) characters.
3807 @retval FALSE There is a non-numeric character.
3811 ShellIsHexOrDecimalNumber (
3812 IN CONST CHAR16
*String
,
3813 IN CONST BOOLEAN ForceHex
,
3814 IN CONST BOOLEAN StopAtSpace
3817 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
3824 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
3825 buffer. The returned buffer must be callee freed.
3827 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3828 maintained and not changed for all operations with the same file.
3830 @param[in] Handle SHELL_FILE_HANDLE to read from.
3831 @param[in, out] Ascii Boolean value for indicating whether the file is
3832 Ascii (TRUE) or UCS2 (FALSE).
3834 @return The line of text from the file.
3836 @sa ShellFileHandleReadLine
3840 ShellFileHandleReturnLine(
3841 IN SHELL_FILE_HANDLE Handle
,
3842 IN OUT BOOLEAN
*Ascii
3852 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
3853 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3854 RetVal
= AllocateZeroPool(Size
);
3855 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
3857 ASSERT_EFI_ERROR(Status
);
3858 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
3866 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
3868 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3869 maintained and not changed for all operations with the same file.
3871 @param[in] Handle SHELL_FILE_HANDLE to read from.
3872 @param[in, out] Buffer The pointer to buffer to read into.
3873 @param[in, out] Size The pointer to number of bytes in Buffer.
3874 @param[in] Truncate If the buffer is large enough, this has no effect.
3875 If the buffer is is too small and Truncate is TRUE,
3876 the line will be truncated.
3877 If the buffer is is too small and Truncate is FALSE,
3878 then no read will occur.
3880 @param[in, out] Ascii Boolean value for indicating whether the file is
3881 Ascii (TRUE) or UCS2 (FALSE).
3883 @retval EFI_SUCCESS The operation was successful. The line is stored in
3885 @retval EFI_INVALID_PARAMETER Handle was NULL.
3886 @retval EFI_INVALID_PARAMETER Size was NULL.
3887 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
3888 Size was updated to the minimum space required.
3892 ShellFileHandleReadLine(
3893 IN SHELL_FILE_HANDLE Handle
,
3894 IN OUT CHAR16
*Buffer
,
3896 IN BOOLEAN Truncate
,
3897 IN OUT BOOLEAN
*Ascii
3904 UINT64 OriginalFilePosition
;
3910 return (EFI_INVALID_PARAMETER
);
3912 if (Buffer
== NULL
) {
3915 *Buffer
= CHAR_NULL
;
3917 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
3918 if (OriginalFilePosition
== 0) {
3919 CharSize
= sizeof(CHAR16
);
3920 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
3921 ASSERT_EFI_ERROR(Status
);
3922 if (CharBuffer
== gUnicodeFileTag
) {
3926 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
3930 for (CountSoFar
= 0;;CountSoFar
++){
3933 CharSize
= sizeof(CHAR8
);
3935 CharSize
= sizeof(CHAR16
);
3937 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
3938 if ( EFI_ERROR(Status
)
3940 || (CharBuffer
== L
'\n' && !(*Ascii
))
3941 || (CharBuffer
== '\n' && *Ascii
)
3946 // if we have space save it...
3948 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
3949 ASSERT(Buffer
!= NULL
);
3950 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
3951 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
3956 // if we ran out of space tell when...
3958 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
3959 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
3961 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
3963 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
3965 return (EFI_BUFFER_TOO_SMALL
);
3967 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
3968 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;