2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLib.h"
16 #include <ShellBase.h>
18 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
23 SHELL_PARAM_ITEM EmptyParamList
[] = {
26 SHELL_PARAM_ITEM SfoParamList
[] = {
30 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
31 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
32 EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
33 EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
34 EFI_HANDLE mEfiShellEnvironment2Handle
;
35 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
36 CHAR16
*mPostReplaceFormat
;
37 CHAR16
*mPostReplaceFormat2
;
40 Check if a Unicode character is a hexadecimal character.
42 This internal function checks if a Unicode character is a
43 numeric character. The valid hexadecimal characters are
44 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
46 @param Char The character to check against.
48 @retval TRUE If the Char is a hexadecmial character.
49 @retval FALSE If the Char is not a hexadecmial character.
54 ShellIsHexaDecimalDigitCharacter (
58 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
62 Check if a Unicode character is a decimal character.
64 This internal function checks if a Unicode character is a
65 decimal character. The valid characters are
69 @param Char The character to check against.
71 @retval TRUE If the Char is a hexadecmial character.
72 @retval FALSE If the Char is not a hexadecmial character.
77 ShellIsDecimalDigitCharacter (
81 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
85 Helper function to find ShellEnvironment2 for constructor.
87 @param[in] ImageHandle A copy of the calling image's handle.
92 IN EFI_HANDLE ImageHandle
102 Status
= gBS
->OpenProtocol(ImageHandle
,
103 &gEfiShellEnvironment2Guid
,
104 (VOID
**)&mEfiShellEnvironment2
,
107 EFI_OPEN_PROTOCOL_GET_PROTOCOL
110 // look for the mEfiShellEnvironment2 protocol at a higher level
112 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
114 // figure out how big of a buffer we need.
116 Status
= gBS
->LocateHandle (ByProtocol
,
117 &gEfiShellEnvironment2Guid
,
118 NULL
, // ignored for ByProtocol
123 // maybe it's not there???
125 if (Status
== EFI_BUFFER_TOO_SMALL
) {
126 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
127 ASSERT(Buffer
!= NULL
);
128 Status
= gBS
->LocateHandle (ByProtocol
,
129 &gEfiShellEnvironment2Guid
,
130 NULL
, // ignored for ByProtocol
135 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
137 // now parse the list of returned handles
139 Status
= EFI_NOT_FOUND
;
140 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
141 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
142 &gEfiShellEnvironment2Guid
,
143 (VOID
**)&mEfiShellEnvironment2
,
146 EFI_OPEN_PROTOCOL_GET_PROTOCOL
148 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
149 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
150 Status
= EFI_SUCCESS
;
156 if (Buffer
!= NULL
) {
163 Function to do most of the work of the constructor. Allows for calling
164 multiple times without complete re-initialization.
166 @param[in] ImageHandle A copy of the ImageHandle.
167 @param[in] SystemTable A pointer to the SystemTable for the application.
171 ShellLibConstructorWorker (
172 IN EFI_HANDLE ImageHandle
,
173 IN EFI_SYSTEM_TABLE
*SystemTable
177 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
178 ASSERT (mPostReplaceFormat
!= NULL
);
179 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
180 ASSERT (mPostReplaceFormat2
!= NULL
);
183 // UEFI 2.0 shell interfaces (used preferentially)
185 Status
= gBS
->OpenProtocol(
187 &gEfiShellProtocolGuid
,
188 (VOID
**)&mEfiShellProtocol
,
191 EFI_OPEN_PROTOCOL_GET_PROTOCOL
193 if (EFI_ERROR(Status
)) {
195 // Search for the shell protocol
197 Status
= gBS
->LocateProtocol(
198 &gEfiShellProtocolGuid
,
200 (VOID
**)&mEfiShellProtocol
202 if (EFI_ERROR(Status
)) {
203 mEfiShellProtocol
= NULL
;
206 Status
= gBS
->OpenProtocol(
208 &gEfiShellParametersProtocolGuid
,
209 (VOID
**)&mEfiShellParametersProtocol
,
212 EFI_OPEN_PROTOCOL_GET_PROTOCOL
214 if (EFI_ERROR(Status
)) {
215 mEfiShellParametersProtocol
= NULL
;
218 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
220 // Moved to seperate function due to complexity
222 Status
= ShellFindSE2(ImageHandle
);
224 if (EFI_ERROR(Status
)) {
225 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
226 mEfiShellEnvironment2
= NULL
;
228 Status
= gBS
->OpenProtocol(ImageHandle
,
229 &gEfiShellInterfaceGuid
,
230 (VOID
**)&mEfiShellInterface
,
233 EFI_OPEN_PROTOCOL_GET_PROTOCOL
235 if (EFI_ERROR(Status
)) {
236 mEfiShellInterface
= NULL
;
241 // only success getting 2 of either the old or new, but no 1/2 and 1/2
243 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
244 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
245 if (mEfiShellProtocol
!= NULL
) {
246 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
247 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
248 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
249 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
250 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
251 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
252 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
253 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
254 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
255 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
257 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
258 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
259 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
260 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
261 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
262 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
263 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
264 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
265 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
266 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
268 return (EFI_SUCCESS
);
270 return (EFI_NOT_FOUND
);
273 Constructor for the Shell library.
275 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
277 @param ImageHandle the image handle of the process
278 @param SystemTable the EFI System Table pointer
280 @retval EFI_SUCCESS the initialization was complete sucessfully
281 @return others an error ocurred during initialization
285 ShellLibConstructor (
286 IN EFI_HANDLE ImageHandle
,
287 IN EFI_SYSTEM_TABLE
*SystemTable
290 mEfiShellEnvironment2
= NULL
;
291 mEfiShellProtocol
= NULL
;
292 mEfiShellParametersProtocol
= NULL
;
293 mEfiShellInterface
= NULL
;
294 mEfiShellEnvironment2Handle
= NULL
;
295 mPostReplaceFormat
= NULL
;
296 mPostReplaceFormat2
= NULL
;
299 // verify that auto initialize is not set false
301 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
302 return (EFI_SUCCESS
);
305 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
309 Destructor for the library. free any resources.
311 @param[in] ImageHandle A copy of the ImageHandle.
312 @param[in] SystemTable A pointer to the SystemTable for the application.
314 @retval EFI_SUCCESS The operation was successful.
315 @return An error from the CloseProtocol function.
320 IN EFI_HANDLE ImageHandle
,
321 IN EFI_SYSTEM_TABLE
*SystemTable
324 if (mEfiShellEnvironment2
!= NULL
) {
325 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
326 &gEfiShellEnvironment2Guid
,
329 mEfiShellEnvironment2
= NULL
;
331 if (mEfiShellInterface
!= NULL
) {
332 gBS
->CloseProtocol(ImageHandle
,
333 &gEfiShellInterfaceGuid
,
336 mEfiShellInterface
= NULL
;
338 if (mEfiShellProtocol
!= NULL
) {
339 gBS
->CloseProtocol(ImageHandle
,
340 &gEfiShellProtocolGuid
,
343 mEfiShellProtocol
= NULL
;
345 if (mEfiShellParametersProtocol
!= NULL
) {
346 gBS
->CloseProtocol(ImageHandle
,
347 &gEfiShellParametersProtocolGuid
,
350 mEfiShellParametersProtocol
= NULL
;
352 mEfiShellEnvironment2Handle
= NULL
;
354 if (mPostReplaceFormat
!= NULL
) {
355 FreePool(mPostReplaceFormat
);
357 if (mPostReplaceFormat2
!= NULL
) {
358 FreePool(mPostReplaceFormat2
);
360 mPostReplaceFormat
= NULL
;
361 mPostReplaceFormat2
= NULL
;
363 return (EFI_SUCCESS
);
367 This function causes the shell library to initialize itself. If the shell library
368 is already initialized it will de-initialize all the current protocol poitners and
369 re-populate them again.
371 When the library is used with PcdShellLibAutoInitialize set to true this function
372 will return EFI_SUCCESS and perform no actions.
374 This function is intended for internal access for shell commands only.
376 @retval EFI_SUCCESS the initialization was complete sucessfully
385 // if auto initialize is not false then skip
387 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
388 return (EFI_SUCCESS
);
392 // deinit the current stuff
394 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
397 // init the new stuff
399 return (ShellLibConstructorWorker(gImageHandle
, gST
));
403 This function will retrieve the information about the file for the handle
404 specified and store it in allocated pool memory.
406 This function allocates a buffer to store the file's information. It is the
407 caller's responsibility to free the buffer
409 @param FileHandle The file handle of the file for which information is
412 @retval NULL information could not be retrieved.
414 @return the information about the file
419 IN SHELL_FILE_HANDLE FileHandle
422 return (FileFunctionMap
.GetFileInfo(FileHandle
));
426 This function sets the information about the file for the opened handle
429 @param[in] FileHandle The file handle of the file for which information
432 @param[in] FileInfo The information to set.
434 @retval EFI_SUCCESS The information was set.
435 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
436 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
437 @retval EFI_NO_MEDIA The device has no medium.
438 @retval EFI_DEVICE_ERROR The device reported an error.
439 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
440 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
441 @retval EFI_ACCESS_DENIED The file was opened read only.
442 @retval EFI_VOLUME_FULL The volume is full.
447 IN SHELL_FILE_HANDLE FileHandle
,
448 IN EFI_FILE_INFO
*FileInfo
451 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
455 This function will open a file or directory referenced by DevicePath.
457 This function opens a file with the open mode according to the file path. The
458 Attributes is valid only for EFI_FILE_MODE_CREATE.
460 @param FilePath on input the device path to the file. On output
461 the remaining device path.
462 @param DeviceHandle pointer to the system device handle.
463 @param FileHandle pointer to the file handle.
464 @param OpenMode the mode to open the file with.
465 @param Attributes the file's file attributes.
467 @retval EFI_SUCCESS The information was set.
468 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
469 @retval EFI_UNSUPPORTED Could not open the file path.
470 @retval EFI_NOT_FOUND The specified file could not be found on the
471 device or the file system could not be found on
473 @retval EFI_NO_MEDIA The device has no medium.
474 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
475 medium is no longer supported.
476 @retval EFI_DEVICE_ERROR The device reported an error.
477 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
478 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
479 @retval EFI_ACCESS_DENIED The file was opened read only.
480 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
482 @retval EFI_VOLUME_FULL The volume is full.
486 ShellOpenFileByDevicePath(
487 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
488 OUT EFI_HANDLE
*DeviceHandle
,
489 OUT SHELL_FILE_HANDLE
*FileHandle
,
496 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
497 EFI_FILE_PROTOCOL
*Handle1
;
498 EFI_FILE_PROTOCOL
*Handle2
;
501 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
503 ASSERT(FilePath
!= NULL
);
504 ASSERT(FileHandle
!= NULL
);
505 ASSERT(DeviceHandle
!= NULL
);
507 // which shell interface should we use
509 if (mEfiShellProtocol
!= NULL
) {
511 // use UEFI Shell 2.0 method.
513 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
514 if (FileName
== NULL
) {
515 return (EFI_INVALID_PARAMETER
);
517 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
524 // use old shell method.
526 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
529 if (EFI_ERROR (Status
)) {
532 Status
= gBS
->OpenProtocol(*DeviceHandle
,
533 &gEfiSimpleFileSystemProtocolGuid
,
534 (VOID
**)&EfiSimpleFileSystemProtocol
,
537 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
538 if (EFI_ERROR (Status
)) {
541 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
542 if (EFI_ERROR (Status
)) {
548 // go down directories one node at a time.
550 while (!IsDevicePathEnd (*FilePath
)) {
552 // For file system access each node should be a file path component
554 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
555 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
558 return (EFI_INVALID_PARAMETER
);
561 // Open this file path node
567 // Try to test opening an existing file
569 Status
= Handle2
->Open (
572 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
573 OpenMode
&~EFI_FILE_MODE_CREATE
,
578 // see if the error was that it needs to be created
580 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
581 Status
= Handle2
->Open (
584 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
590 // Close the last node
592 Handle2
->Close (Handle2
);
594 if (EFI_ERROR(Status
)) {
601 *FilePath
= NextDevicePathNode (*FilePath
);
605 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
607 *FileHandle
= (VOID
*)Handle1
;
608 return (EFI_SUCCESS
);
612 This function will open a file or directory referenced by filename.
614 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
615 otherwise, the Filehandle is NULL. The Attributes is valid only for
616 EFI_FILE_MODE_CREATE.
618 if FileNAme is NULL then ASSERT()
620 @param FileName pointer to file name
621 @param FileHandle pointer to the file handle.
622 @param OpenMode the mode to open the file with.
623 @param Attributes the file's file attributes.
625 @retval EFI_SUCCESS The information was set.
626 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
627 @retval EFI_UNSUPPORTED Could not open the file path.
628 @retval EFI_NOT_FOUND The specified file could not be found on the
629 device or the file system could not be found
631 @retval EFI_NO_MEDIA The device has no medium.
632 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
633 medium is no longer supported.
634 @retval EFI_DEVICE_ERROR The device reported an error.
635 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
636 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
637 @retval EFI_ACCESS_DENIED The file was opened read only.
638 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
640 @retval EFI_VOLUME_FULL The volume is full.
645 IN CONST CHAR16
*FileName
,
646 OUT SHELL_FILE_HANDLE
*FileHandle
,
651 EFI_HANDLE DeviceHandle
;
652 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
654 EFI_FILE_INFO
*FileInfo
;
657 // ASSERT if FileName is NULL
659 ASSERT(FileName
!= NULL
);
661 if (FileName
== NULL
) {
662 return (EFI_INVALID_PARAMETER
);
665 if (mEfiShellProtocol
!= NULL
) {
666 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
&& (Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
667 return ShellCreateDirectory(FileName
, FileHandle
);
670 // Use UEFI Shell 2.0 method
672 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
675 if (StrCmp(FileName
, L
"NUL") != 0 && !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
676 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
677 ASSERT(FileInfo
!= NULL
);
678 FileInfo
->Attribute
= Attributes
;
679 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
685 // Using EFI Shell version
686 // this means convert name to path and call that function
687 // since this will use EFI method again that will open it.
689 ASSERT(mEfiShellEnvironment2
!= NULL
);
690 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
691 if (FilePath
!= NULL
) {
692 return (ShellOpenFileByDevicePath(&FilePath
,
698 return (EFI_DEVICE_ERROR
);
701 This function create a directory
703 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
704 otherwise, the Filehandle is NULL. If the directory already existed, this
705 function opens the existing directory.
707 @param DirectoryName pointer to directory name
708 @param FileHandle pointer to the file handle.
710 @retval EFI_SUCCESS The information was set.
711 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
712 @retval EFI_UNSUPPORTED Could not open the file path.
713 @retval EFI_NOT_FOUND The specified file could not be found on the
714 device or the file system could not be found
716 @retval EFI_NO_MEDIA The device has no medium.
717 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
718 medium is no longer supported.
719 @retval EFI_DEVICE_ERROR The device reported an error.
720 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
721 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
722 @retval EFI_ACCESS_DENIED The file was opened read only.
723 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
725 @retval EFI_VOLUME_FULL The volume is full.
726 @sa ShellOpenFileByName
730 ShellCreateDirectory(
731 IN CONST CHAR16
*DirectoryName
,
732 OUT SHELL_FILE_HANDLE
*FileHandle
735 if (mEfiShellProtocol
!= NULL
) {
737 // Use UEFI Shell 2.0 method
739 return (mEfiShellProtocol
->CreateFile(DirectoryName
,
744 return (ShellOpenFileByName(DirectoryName
,
746 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
753 This function reads information from an opened file.
755 If FileHandle is not a directory, the function reads the requested number of
756 bytes from the file at the file's current position and returns them in Buffer.
757 If the read goes beyond the end of the file, the read length is truncated to the
758 end of the file. The file's current position is increased by the number of bytes
759 returned. If FileHandle is a directory, the function reads the directory entry
760 at the file's current position and returns the entry in Buffer. If the Buffer
761 is not large enough to hold the current directory entry, then
762 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
763 BufferSize is set to be the size of the buffer needed to read the entry. On
764 success, the current position is updated to the next directory entry. If there
765 are no more directory entries, the read returns a zero-length buffer.
766 EFI_FILE_INFO is the structure returned as the directory entry.
768 @param FileHandle the opened file handle
769 @param BufferSize on input the size of buffer in bytes. on return
770 the number of bytes written.
771 @param Buffer the buffer to put read data into.
773 @retval EFI_SUCCESS Data was read.
774 @retval EFI_NO_MEDIA The device has no media.
775 @retval EFI_DEVICE_ERROR The device reported an error.
776 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
777 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
784 IN SHELL_FILE_HANDLE FileHandle
,
785 IN OUT UINTN
*BufferSize
,
789 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
794 Write data to a file.
796 This function writes the specified number of bytes to the file at the current
797 file position. The current file position is advanced the actual number of bytes
798 written, which is returned in BufferSize. Partial writes only occur when there
799 has been a data error during the write attempt (such as "volume space full").
800 The file is automatically grown to hold the data if required. Direct writes to
801 opened directories are not supported.
803 @param FileHandle The opened file for writing
804 @param BufferSize on input the number of bytes in Buffer. On output
805 the number of bytes written.
806 @param Buffer the buffer containing data to write is stored.
808 @retval EFI_SUCCESS Data was written.
809 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
810 @retval EFI_NO_MEDIA The device has no media.
811 @retval EFI_DEVICE_ERROR The device reported an error.
812 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
813 @retval EFI_WRITE_PROTECTED The device is write-protected.
814 @retval EFI_ACCESS_DENIED The file was open for read only.
815 @retval EFI_VOLUME_FULL The volume is full.
820 IN SHELL_FILE_HANDLE FileHandle
,
821 IN OUT UINTN
*BufferSize
,
825 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
829 Close an open file handle.
831 This function closes a specified file handle. All "dirty" cached file data is
832 flushed to the device, and the file is closed. In all cases the handle is
835 @param FileHandle the file handle to close.
837 @retval EFI_SUCCESS the file handle was closed sucessfully.
842 IN SHELL_FILE_HANDLE
*FileHandle
845 return (FileFunctionMap
.CloseFile(*FileHandle
));
849 Delete a file and close the handle
851 This function closes and deletes a file. In all cases the file handle is closed.
852 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
853 returned, but the handle is still closed.
855 @param FileHandle the file handle to delete
857 @retval EFI_SUCCESS the file was closed sucessfully
858 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
860 @retval INVALID_PARAMETER One of the parameters has an invalid value.
865 IN SHELL_FILE_HANDLE
*FileHandle
868 return (FileFunctionMap
.DeleteFile(*FileHandle
));
872 Set the current position in a file.
874 This function sets the current file position for the handle to the position
875 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
876 absolute positioning is supported, and seeking past the end of the file is
877 allowed (a subsequent write would grow the file). Seeking to position
878 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
879 If FileHandle is a directory, the only position that may be set is zero. This
880 has the effect of starting the read process of the directory entries over.
882 @param FileHandle The file handle on which the position is being set
883 @param Position Byte position from begining of file
885 @retval EFI_SUCCESS Operation completed sucessfully.
886 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
888 @retval INVALID_PARAMETER One of the parameters has an invalid value.
892 ShellSetFilePosition (
893 IN SHELL_FILE_HANDLE FileHandle
,
897 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
901 Gets a file's current position
903 This function retrieves the current file position for the file handle. For
904 directories, the current file position has no meaning outside of the file
905 system driver and as such the operation is not supported. An error is returned
906 if FileHandle is a directory.
908 @param FileHandle The open file handle on which to get the position.
909 @param Position Byte position from begining of file.
911 @retval EFI_SUCCESS the operation completed sucessfully.
912 @retval INVALID_PARAMETER One of the parameters has an invalid value.
913 @retval EFI_UNSUPPORTED the request is not valid on directories.
917 ShellGetFilePosition (
918 IN SHELL_FILE_HANDLE FileHandle
,
922 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
925 Flushes data on a file
927 This function flushes all modified data associated with a file to a device.
929 @param FileHandle The file handle on which to flush data
931 @retval EFI_SUCCESS The data was flushed.
932 @retval EFI_NO_MEDIA The device has no media.
933 @retval EFI_DEVICE_ERROR The device reported an error.
934 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
935 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
936 @retval EFI_ACCESS_DENIED The file was opened for read only.
941 IN SHELL_FILE_HANDLE FileHandle
944 return (FileFunctionMap
.FlushFile(FileHandle
));
948 Retrieves the first file from a directory
950 This function opens a directory and gets the first file's info in the
951 directory. Caller can use ShellFindNextFile() to get other files. When
952 complete the caller is responsible for calling FreePool() on Buffer.
954 @param DirHandle The file handle of the directory to search
955 @param Buffer Pointer to buffer for file's information
957 @retval EFI_SUCCESS Found the first file.
958 @retval EFI_NOT_FOUND Cannot find the directory.
959 @retval EFI_NO_MEDIA The device has no media.
960 @retval EFI_DEVICE_ERROR The device reported an error.
961 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
962 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
968 IN SHELL_FILE_HANDLE DirHandle
,
969 OUT EFI_FILE_INFO
**Buffer
973 // pass to file handle lib
975 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
978 Retrieves the next file in a directory.
980 To use this function, caller must call the ShellFindFirstFile() to get the
981 first file, and then use this function get other files. This function can be
982 called for several times to get each file's information in the directory. If
983 the call of ShellFindNextFile() got the last file in the directory, the next
984 call of this function has no file to get. *NoFile will be set to TRUE and the
985 Buffer memory will be automatically freed.
987 @param DirHandle the file handle of the directory
988 @param Buffer pointer to buffer for file's information
989 @param NoFile pointer to boolean when last file is found
991 @retval EFI_SUCCESS Found the next file, or reached last file
992 @retval EFI_NO_MEDIA The device has no media.
993 @retval EFI_DEVICE_ERROR The device reported an error.
994 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
999 IN SHELL_FILE_HANDLE DirHandle
,
1000 OUT EFI_FILE_INFO
*Buffer
,
1005 // pass to file handle lib
1007 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1010 Retrieve the size of a file.
1012 if FileHandle is NULL then ASSERT()
1013 if Size is NULL then ASSERT()
1015 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1018 @param FileHandle file handle from which size is retrieved
1019 @param Size pointer to size
1021 @retval EFI_SUCCESS operation was completed sucessfully
1022 @retval EFI_DEVICE_ERROR cannot access the file
1027 IN SHELL_FILE_HANDLE FileHandle
,
1031 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1034 Retrieves the status of the break execution flag
1036 this function is useful to check whether the application is being asked to halt by the shell.
1038 @retval TRUE the execution break is enabled
1039 @retval FALSE the execution break is not enabled
1043 ShellGetExecutionBreakFlag(
1048 // Check for UEFI Shell 2.0 protocols
1050 if (mEfiShellProtocol
!= NULL
) {
1053 // We are using UEFI Shell 2.0; see if the event has been triggered
1055 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1062 // using EFI Shell; call the function to check
1064 ASSERT(mEfiShellEnvironment2
!= NULL
);
1065 return (mEfiShellEnvironment2
->GetExecutionBreak());
1068 return the value of an environment variable
1070 this function gets the value of the environment variable set by the
1071 ShellSetEnvironmentVariable function
1073 @param EnvKey The key name of the environment variable.
1075 @retval NULL the named environment variable does not exist.
1076 @return != NULL pointer to the value of the environment variable
1080 ShellGetEnvironmentVariable (
1081 IN CONST CHAR16
*EnvKey
1085 // Check for UEFI Shell 2.0 protocols
1087 if (mEfiShellProtocol
!= NULL
) {
1088 return (mEfiShellProtocol
->GetEnv(EnvKey
));
1092 // ASSERT that we must have EFI shell
1094 ASSERT(mEfiShellEnvironment2
!= NULL
);
1099 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1102 set the value of an environment variable
1104 This function changes the current value of the specified environment variable. If the
1105 environment variable exists and the Value is an empty string, then the environment
1106 variable is deleted. If the environment variable exists and the Value is not an empty
1107 string, then the value of the environment variable is changed. If the environment
1108 variable does not exist and the Value is an empty string, there is no action. If the
1109 environment variable does not exist and the Value is a non-empty string, then the
1110 environment variable is created and assigned the specified value.
1112 This is not supported pre-UEFI Shell 2.0.
1114 @param EnvKey The key name of the environment variable.
1115 @param EnvVal The Value of the environment variable
1116 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1118 @retval EFI_SUCCESS the operation was completed sucessfully
1119 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1123 ShellSetEnvironmentVariable (
1124 IN CONST CHAR16
*EnvKey
,
1125 IN CONST CHAR16
*EnvVal
,
1130 // Check for UEFI Shell 2.0 protocols
1132 if (mEfiShellProtocol
!= NULL
) {
1133 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1137 // This feature does not exist under EFI shell
1139 return (EFI_UNSUPPORTED
);
1143 Cause the shell to parse and execute a command line.
1145 This function creates a nested instance of the shell and executes the specified
1146 command (CommandLine) with the specified environment (Environment). Upon return,
1147 the status code returned by the specified command is placed in StatusCode.
1148 If Environment is NULL, then the current environment is used and all changes made
1149 by the commands executed will be reflected in the current environment. If the
1150 Environment is non-NULL, then the changes made will be discarded.
1151 The CommandLine is executed from the current working directory on the current
1154 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1155 environment. The values pointed to by the parameters will be unchanged by the
1156 ShellExecute() function. The Output parameter has no effect in a
1157 UEFI Shell 2.0 environment.
1159 @param[in] ParentHandle The parent image starting the operation.
1160 @param[in] CommandLine The pointer to a NULL terminated command line.
1161 @param[in] Output True to display debug output. False to hide it.
1162 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1163 in the form "x=y". If NULL, the current set is used.
1164 @param[out] Status The status of the run command line.
1166 @retval EFI_SUCCESS The operation completed sucessfully. Status
1167 contains the status code returned.
1168 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1169 @retval EFI_OUT_OF_RESOURCES Out of resources.
1170 @retval EFI_UNSUPPORTED The operation is not allowed.
1176 IN EFI_HANDLE
*ParentHandle
,
1177 IN CHAR16
*CommandLine OPTIONAL
,
1178 IN BOOLEAN Output OPTIONAL
,
1179 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1180 OUT EFI_STATUS
*Status OPTIONAL
1184 // Check for UEFI Shell 2.0 protocols
1186 if (mEfiShellProtocol
!= NULL
) {
1188 // Call UEFI Shell 2.0 version (not using Output parameter)
1190 return (mEfiShellProtocol
->Execute(ParentHandle
,
1192 EnvironmentVariables
,
1196 // ASSERT that we must have EFI shell
1198 ASSERT(mEfiShellEnvironment2
!= NULL
);
1200 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1201 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1203 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1208 Retreives the current directory path
1210 If the DeviceName is NULL, it returns the current device's current directory
1211 name. If the DeviceName is not NULL, it returns the current directory name
1214 @param DeviceName the name of the drive to get directory on
1216 @retval NULL the directory does not exist
1217 @return != NULL the directory
1221 ShellGetCurrentDir (
1222 IN CHAR16
* CONST DeviceName OPTIONAL
1226 // Check for UEFI Shell 2.0 protocols
1228 if (mEfiShellProtocol
!= NULL
) {
1229 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1232 // ASSERT that we must have EFI shell
1234 ASSERT(mEfiShellEnvironment2
!= NULL
);
1235 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1238 sets (enabled or disabled) the page break mode
1240 when page break mode is enabled the screen will stop scrolling
1241 and wait for operator input before scrolling a subsequent screen.
1243 @param CurrentState TRUE to enable and FALSE to disable
1247 ShellSetPageBreakMode (
1248 IN BOOLEAN CurrentState
1252 // check for enabling
1254 if (CurrentState
!= 0x00) {
1256 // check for UEFI Shell 2.0
1258 if (mEfiShellProtocol
!= NULL
) {
1260 // Enable with UEFI 2.0 Shell
1262 mEfiShellProtocol
->EnablePageBreak();
1266 // ASSERT that must have EFI Shell
1268 ASSERT(mEfiShellEnvironment2
!= NULL
);
1270 // Enable with EFI Shell
1272 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1277 // check for UEFI Shell 2.0
1279 if (mEfiShellProtocol
!= NULL
) {
1281 // Disable with UEFI 2.0 Shell
1283 mEfiShellProtocol
->DisablePageBreak();
1287 // ASSERT that must have EFI Shell
1289 ASSERT(mEfiShellEnvironment2
!= NULL
);
1291 // Disable with EFI Shell
1293 mEfiShellEnvironment2
->DisablePageBreak ();
1300 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1301 /// This allows for the struct to be populated.
1308 SHELL_FILE_HANDLE Handle
;
1309 EFI_FILE_INFO
*Info
;
1310 } EFI_SHELL_FILE_INFO_NO_CONST
;
1313 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1315 if OldStyleFileList is NULL then ASSERT()
1317 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1318 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1319 the ShellCloseFileMetaArg function.
1321 @param[in] FileList the EFI shell list type
1322 @param[in,out] ListHead the list to add to
1324 @retval the resultant head of the double linked new format list;
1328 InternalShellConvertFileListType (
1329 IN LIST_ENTRY
*FileList
,
1330 IN OUT LIST_ENTRY
*ListHead
1333 SHELL_FILE_ARG
*OldInfo
;
1335 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1340 ASSERT(FileList
!= NULL
);
1341 ASSERT(ListHead
!= NULL
);
1344 // enumerate through each member of the old list and copy
1346 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1347 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1348 ASSERT(OldInfo
!= NULL
);
1351 // Skip ones that failed to open...
1353 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1358 // make sure the old list was valid
1360 ASSERT(OldInfo
->Info
!= NULL
);
1361 ASSERT(OldInfo
->FullName
!= NULL
);
1362 ASSERT(OldInfo
->FileName
!= NULL
);
1365 // allocate a new EFI_SHELL_FILE_INFO object
1367 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1368 ASSERT(NewInfo
!= NULL
);
1369 if (NewInfo
== NULL
) {
1374 // copy the simple items
1376 NewInfo
->Handle
= OldInfo
->Handle
;
1377 NewInfo
->Status
= OldInfo
->Status
;
1379 // old shell checks for 0 not NULL
1380 OldInfo
->Handle
= 0;
1383 // allocate new space to copy strings and structure
1385 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1386 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1387 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1390 // make sure all the memory allocations were sucessful
1392 ASSERT(NewInfo
->FullName
!= NULL
);
1393 ASSERT(NewInfo
->FileName
!= NULL
);
1394 ASSERT(NewInfo
->Info
!= NULL
);
1397 // Copt the strings and structure
1399 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1400 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1401 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1404 // add that to the list
1406 InsertTailList(ListHead
, &NewInfo
->Link
);
1411 Opens a group of files based on a path.
1413 This function uses the Arg to open all the matching files. Each matched
1414 file has a SHELL_FILE_ARG structure to record the file information. These
1415 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1416 structures from ListHead to access each file. This function supports wildcards
1417 and will process '?' and '*' as such. the list must be freed with a call to
1418 ShellCloseFileMetaArg().
1420 If you are NOT appending to an existing list *ListHead must be NULL. If
1421 *ListHead is NULL then it must be callee freed.
1423 @param Arg pointer to path string
1424 @param OpenMode mode to open files with
1425 @param ListHead head of linked list of results
1427 @retval EFI_SUCCESS the operation was sucessful and the list head
1428 contains the list of opened files
1429 @return != EFI_SUCCESS the operation failed
1431 @sa InternalShellConvertFileListType
1435 ShellOpenFileMetaArg (
1438 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1442 LIST_ENTRY mOldStyleFileList
;
1445 // ASSERT that Arg and ListHead are not NULL
1447 ASSERT(Arg
!= NULL
);
1448 ASSERT(ListHead
!= NULL
);
1451 // Check for UEFI Shell 2.0 protocols
1453 if (mEfiShellProtocol
!= NULL
) {
1454 if (*ListHead
== NULL
) {
1455 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1456 if (*ListHead
== NULL
) {
1457 return (EFI_OUT_OF_RESOURCES
);
1459 InitializeListHead(&((*ListHead
)->Link
));
1461 Status
= mEfiShellProtocol
->OpenFileList(Arg
,
1464 if (EFI_ERROR(Status
)) {
1465 mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1467 Status
= mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1469 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1470 FreePool(*ListHead
);
1472 return (EFI_NOT_FOUND
);
1478 // ASSERT that we must have EFI shell
1480 ASSERT(mEfiShellEnvironment2
!= NULL
);
1483 // make sure the list head is initialized
1485 InitializeListHead(&mOldStyleFileList
);
1488 // Get the EFI Shell list of files
1490 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1491 if (EFI_ERROR(Status
)) {
1496 if (*ListHead
== NULL
) {
1497 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1498 if (*ListHead
== NULL
) {
1499 return (EFI_OUT_OF_RESOURCES
);
1501 InitializeListHead(&((*ListHead
)->Link
));
1505 // Convert that to equivalent of UEFI Shell 2.0 structure
1507 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1510 // Free the EFI Shell version that was converted.
1512 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1514 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1515 FreePool(*ListHead
);
1517 Status
= EFI_NOT_FOUND
;
1523 Free the linked list returned from ShellOpenFileMetaArg.
1525 if ListHead is NULL then ASSERT().
1527 @param ListHead the pointer to free.
1529 @retval EFI_SUCCESS the operation was sucessful.
1533 ShellCloseFileMetaArg (
1534 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1540 // ASSERT that ListHead is not NULL
1542 ASSERT(ListHead
!= NULL
);
1545 // Check for UEFI Shell 2.0 protocols
1547 if (mEfiShellProtocol
!= NULL
) {
1548 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1551 // Since this is EFI Shell version we need to free our internally made copy
1554 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1555 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1556 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1557 RemoveEntryList(Node
);
1558 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1559 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1560 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1561 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1562 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1569 Find a file by searching the CWD and then the path.
1571 If FileName is NULL then ASSERT.
1573 If the return value is not NULL then the memory must be caller freed.
1575 @param FileName Filename string.
1577 @retval NULL the file was not found
1578 @return !NULL the full path to the file.
1583 IN CONST CHAR16
*FileName
1587 SHELL_FILE_HANDLE Handle
;
1591 CONST CHAR16
*Walker
;
1598 // First make sure its not an absolute path.
1600 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1601 if (!EFI_ERROR(Status
)){
1602 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1603 ASSERT(RetVal
== NULL
);
1604 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1605 ShellCloseFile(&Handle
);
1608 ShellCloseFile(&Handle
);
1612 Path
= ShellGetEnvironmentVariable(L
"cwd");
1614 Size
= StrSize(Path
);
1615 Size
+= StrSize(FileName
);
1616 TestPath
= AllocateZeroPool(Size
);
1617 ASSERT(TestPath
!= NULL
);
1618 if (TestPath
== NULL
) {
1621 StrCpy(TestPath
, Path
);
1622 StrCat(TestPath
, FileName
);
1623 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1624 if (!EFI_ERROR(Status
)){
1625 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1626 ASSERT(RetVal
== NULL
);
1627 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1628 ShellCloseFile(&Handle
);
1632 ShellCloseFile(&Handle
);
1637 Path
= ShellGetEnvironmentVariable(L
"path");
1639 Size
= StrSize(Path
)+sizeof(CHAR16
);
1640 Size
+= StrSize(FileName
);
1641 TestPath
= AllocateZeroPool(Size
);
1642 Walker
= (CHAR16
*)Path
;
1644 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1645 TempChar
= StrStr(TestPath
, L
";");
1646 if (TempChar
!= NULL
) {
1647 *TempChar
= CHAR_NULL
;
1649 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1650 StrCat(TestPath
, L
"\\");
1652 StrCat(TestPath
, FileName
);
1653 if (StrStr(Walker
, L
";") != NULL
) {
1654 Walker
= StrStr(Walker
, L
";") + 1;
1658 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1659 if (!EFI_ERROR(Status
)){
1660 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1661 ASSERT(RetVal
== NULL
);
1662 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1663 ShellCloseFile(&Handle
);
1666 ShellCloseFile(&Handle
);
1669 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1676 Find a file by searching the CWD and then the path with a variable set of file
1677 extensions. If the file is not found it will append each extension in the list
1678 in the order provided and return the first one that is successful.
1680 If FileName is NULL, then ASSERT.
1681 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1683 If the return value is not NULL then the memory must be caller freed.
1685 @param[in] FileName Filename string.
1686 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1688 @retval NULL The file was not found.
1689 @retval !NULL The path to the file.
1693 ShellFindFilePathEx (
1694 IN CONST CHAR16
*FileName
,
1695 IN CONST CHAR16
*FileExtension
1700 CONST CHAR16
*ExtensionWalker
;
1705 ASSERT(FileName
!= NULL
);
1706 if (FileExtension
== NULL
) {
1707 return (ShellFindFilePath(FileName
));
1709 RetVal
= ShellFindFilePath(FileName
);
1710 if (RetVal
!= NULL
) {
1713 Size
= StrSize(FileName
);
1714 Size
+= StrSize(FileExtension
);
1715 TestPath
= AllocateZeroPool(Size
);
1716 ASSERT(TestPath
!= NULL
);
1717 if (TestPath
== NULL
) {
1720 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1721 StrCpy(TestPath
, FileName
);
1722 if (ExtensionWalker
!= NULL
) {
1723 StrCat(TestPath
, ExtensionWalker
);
1725 TempChar
= StrStr(TestPath
, L
";");
1726 if (TempChar
!= NULL
) {
1727 *TempChar
= CHAR_NULL
;
1729 RetVal
= ShellFindFilePath(TestPath
);
1730 if (RetVal
!= NULL
) {
1733 ASSERT(ExtensionWalker
!= NULL
);
1734 TempChar2
= StrStr(ExtensionWalker
, L
";");
1743 SHELL_PARAM_TYPE Type
;
1745 UINTN OriginalPosition
;
1746 } SHELL_PARAM_PACKAGE
;
1749 Checks the list of valid arguments and returns TRUE if the item was found. If the
1750 return value is TRUE then the type parameter is set also.
1752 if CheckList is NULL then ASSERT();
1753 if Name is NULL then ASSERT();
1754 if Type is NULL then ASSERT();
1756 @param Name pointer to Name of parameter found
1757 @param CheckList List to check against
1758 @param Type pointer to type of parameter if it was found
1760 @retval TRUE the Parameter was found. Type is valid.
1761 @retval FALSE the Parameter was not found. Type is not valid.
1765 InternalIsOnCheckList (
1766 IN CONST CHAR16
*Name
,
1767 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1768 OUT SHELL_PARAM_TYPE
*Type
1771 SHELL_PARAM_ITEM
*TempListItem
;
1774 // ASSERT that all 3 pointer parameters aren't NULL
1776 ASSERT(CheckList
!= NULL
);
1777 ASSERT(Type
!= NULL
);
1778 ASSERT(Name
!= NULL
);
1781 // question mark and page break mode are always supported
1783 if ((StrCmp(Name
, L
"-?") == 0) ||
1784 (StrCmp(Name
, L
"-b") == 0)
1790 // Enumerate through the list
1792 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1794 // If the Type is TypeStart only check the first characters of the passed in param
1795 // If it matches set the type and return TRUE
1797 if (TempListItem
->Type
== TypeStart
&& StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1798 *Type
= TempListItem
->Type
;
1800 } else if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1801 *Type
= TempListItem
->Type
;
1809 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1811 @param[in] Name pointer to Name of parameter found
1812 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1814 @retval TRUE the Parameter is a flag.
1815 @retval FALSE the Parameter not a flag.
1820 IN CONST CHAR16
*Name
,
1821 IN BOOLEAN AlwaysAllowNumbers
1825 // ASSERT that Name isn't NULL
1827 ASSERT(Name
!= NULL
);
1830 // If we accept numbers then dont return TRUE. (they will be values)
1832 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
) {
1837 // If the Name has a /, +, or - as the first character return TRUE
1839 if ((Name
[0] == L
'/') ||
1840 (Name
[0] == L
'-') ||
1849 Checks the command line arguments passed against the list of valid ones.
1851 If no initialization is required, then return RETURN_SUCCESS.
1853 @param[in] CheckList pointer to list of parameters to check
1854 @param[out] CheckPackage pointer to pointer to list checked values
1855 @param[out] ProblemParam optional pointer to pointer to unicode string for
1856 the paramater that caused failure. If used then the
1857 caller is responsible for freeing the memory.
1858 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1859 @param[in] Argv pointer to array of parameters
1860 @param[in] Argc Count of parameters in Argv
1861 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
1863 @retval EFI_SUCCESS The operation completed sucessfully.
1864 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1865 @retval EFI_INVALID_PARAMETER A parameter was invalid
1866 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1867 duplicated. the duplicated command line argument
1868 was returned in ProblemParam if provided.
1869 @retval EFI_NOT_FOUND a argument required a value that was missing.
1870 the invalid command line argument was returned in
1871 ProblemParam if provided.
1875 InternalCommandLineParse (
1876 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1877 OUT LIST_ENTRY
**CheckPackage
,
1878 OUT CHAR16
**ProblemParam OPTIONAL
,
1879 IN BOOLEAN AutoPageBreak
,
1880 IN CONST CHAR16
**Argv
,
1882 IN BOOLEAN AlwaysAllowNumbers
1886 SHELL_PARAM_TYPE CurrentItemType
;
1887 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1892 CurrentItemPackage
= NULL
;
1898 // If there is only 1 item we dont need to do anything
1901 *CheckPackage
= NULL
;
1902 return (EFI_SUCCESS
);
1908 ASSERT(CheckList
!= NULL
);
1909 ASSERT(Argv
!= NULL
);
1912 // initialize the linked list
1914 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1915 InitializeListHead(*CheckPackage
);
1918 // loop through each of the arguments
1920 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1921 if (Argv
[LoopCounter
] == NULL
) {
1923 // do nothing for NULL argv
1925 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
1927 // We might have leftover if last parameter didnt have optional value
1929 if (GetItemValue
!= 0) {
1931 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1936 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1937 ASSERT(CurrentItemPackage
!= NULL
);
1938 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1939 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1940 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1941 CurrentItemPackage
->Type
= CurrentItemType
;
1942 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1943 CurrentItemPackage
->Value
= NULL
;
1946 // Does this flag require a value
1948 switch (CurrentItemPackage
->Type
) {
1950 // possibly trigger the next loop(s) to populate the value of this item
1956 case TypeDoubleValue
:
1961 GetItemValue
= (UINTN
)(-1);
1966 // this item has no value expected; we are done
1968 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1969 ASSERT(GetItemValue
== 0);
1972 } else if (GetItemValue
!= 0 && !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
)) {
1973 ASSERT(CurrentItemPackage
!= NULL
);
1975 // get the item VALUE for a previous flag
1977 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
1978 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1979 if (ValueSize
== 0) {
1980 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1982 StrCat(CurrentItemPackage
->Value
, L
" ");
1983 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1985 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
1987 if (GetItemValue
== 0) {
1988 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1990 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) ){ //|| ProblemParam == NULL) {
1992 // add this one as a non-flag
1994 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1995 ASSERT(CurrentItemPackage
!= NULL
);
1996 CurrentItemPackage
->Name
= NULL
;
1997 CurrentItemPackage
->Type
= TypePosition
;
1998 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1999 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2000 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2001 CurrentItemPackage
->OriginalPosition
= Count
++;
2002 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2003 } else if (ProblemParam
!= NULL
) {
2005 // this was a non-recognised flag... error!
2007 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
2008 ASSERT(*ProblemParam
!= NULL
);
2009 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
2010 ShellCommandLineFreeVarList(*CheckPackage
);
2011 *CheckPackage
= NULL
;
2012 return (EFI_VOLUME_CORRUPTED
);
2017 if (GetItemValue
!= 0) {
2019 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2022 // support for AutoPageBreak
2024 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2025 ShellSetPageBreakMode(TRUE
);
2027 return (EFI_SUCCESS
);
2031 Checks the command line arguments passed against the list of valid ones.
2032 Optionally removes NULL values first.
2034 If no initialization is required, then return RETURN_SUCCESS.
2036 @param[in] CheckList The pointer to list of parameters to check.
2037 @param[out] CheckPackage The package of checked values.
2038 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2039 the paramater that caused failure.
2040 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2041 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2043 @retval EFI_SUCCESS The operation completed sucessfully.
2044 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2045 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2046 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2047 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2048 of the command line arguments was returned in
2049 ProblemParam if provided.
2050 @retval EFI_NOT_FOUND A argument required a value that was missing.
2051 The invalid command line argument was returned in
2052 ProblemParam if provided.
2056 ShellCommandLineParseEx (
2057 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2058 OUT LIST_ENTRY
**CheckPackage
,
2059 OUT CHAR16
**ProblemParam OPTIONAL
,
2060 IN BOOLEAN AutoPageBreak
,
2061 IN BOOLEAN AlwaysAllowNumbers
2065 // ASSERT that CheckList and CheckPackage aren't NULL
2067 ASSERT(CheckList
!= NULL
);
2068 ASSERT(CheckPackage
!= NULL
);
2071 // Check for UEFI Shell 2.0 protocols
2073 if (mEfiShellParametersProtocol
!= NULL
) {
2074 return (InternalCommandLineParse(CheckList
,
2078 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
2079 mEfiShellParametersProtocol
->Argc
,
2080 AlwaysAllowNumbers
));
2084 // ASSERT That EFI Shell is not required
2086 ASSERT (mEfiShellInterface
!= NULL
);
2087 return (InternalCommandLineParse(CheckList
,
2091 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2092 mEfiShellInterface
->Argc
,
2093 AlwaysAllowNumbers
));
2097 Frees shell variable list that was returned from ShellCommandLineParse.
2099 This function will free all the memory that was used for the CheckPackage
2100 list of postprocessed shell arguments.
2102 this function has no return value.
2104 if CheckPackage is NULL, then return
2106 @param CheckPackage the list to de-allocate
2110 ShellCommandLineFreeVarList (
2111 IN LIST_ENTRY
*CheckPackage
2117 // check for CheckPackage == NULL
2119 if (CheckPackage
== NULL
) {
2124 // for each node in the list
2126 for ( Node
= GetFirstNode(CheckPackage
)
2127 ; !IsListEmpty(CheckPackage
)
2128 ; Node
= GetFirstNode(CheckPackage
)
2131 // Remove it from the list
2133 RemoveEntryList(Node
);
2136 // if it has a name free the name
2138 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2139 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2143 // if it has a value free the value
2145 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2146 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2150 // free the node structure
2152 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2155 // free the list head node
2157 FreePool(CheckPackage
);
2160 Checks for presence of a flag parameter
2162 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2164 if CheckPackage is NULL then return FALSE.
2165 if KeyString is NULL then ASSERT()
2167 @param CheckPackage The package of parsed command line arguments
2168 @param KeyString the Key of the command line argument to check for
2170 @retval TRUE the flag is on the command line
2171 @retval FALSE the flag is not on the command line
2175 ShellCommandLineGetFlag (
2176 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2177 IN CONST CHAR16
* CONST KeyString
2183 // ASSERT that both CheckPackage and KeyString aren't NULL
2185 ASSERT(KeyString
!= NULL
);
2188 // return FALSE for no package
2190 if (CheckPackage
== NULL
) {
2195 // enumerate through the list of parametrs
2197 for ( Node
= GetFirstNode(CheckPackage
)
2198 ; !IsNull (CheckPackage
, Node
)
2199 ; Node
= GetNextNode(CheckPackage
, Node
)
2202 // If the Name matches, return TRUE (and there may be NULL name)
2204 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2206 // If Type is TypeStart then only compare the begining of the strings
2208 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2209 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2212 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2220 Returns value from command line argument.
2222 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2224 If CheckPackage is NULL, then return NULL.
2226 @param[in] CheckPackage The package of parsed command line arguments.
2227 @param[in] KeyString The Key of the command line argument to check for.
2229 @retval NULL The flag is not on the command line.
2230 @retval !=NULL The pointer to unicode string of the value.
2234 ShellCommandLineGetValue (
2235 IN CONST LIST_ENTRY
*CheckPackage
,
2236 IN CHAR16
*KeyString
2242 // check for CheckPackage == NULL
2244 if (CheckPackage
== NULL
) {
2249 // enumerate through the list of parametrs
2251 for ( Node
= GetFirstNode(CheckPackage
)
2252 ; !IsNull (CheckPackage
, Node
)
2253 ; Node
= GetNextNode(CheckPackage
, Node
)
2256 // If the Name matches, return the value (name can be NULL)
2258 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2260 // If Type is TypeStart then only compare the begining of the strings
2262 if ( ((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
2263 && StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0
2266 // return the string part after the flag
2268 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2269 } else if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2273 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2281 Returns raw value from command line argument.
2283 Raw value parameters are in the form of "value" in a specific position in the list.
2285 If CheckPackage is NULL, then return NULL.
2287 @param[in] CheckPackage The package of parsed command line arguments.
2288 @param[in] Position The position of the value.
2290 @retval NULL The flag is not on the command line.
2291 @retval !=NULL The pointer to unicode string of the value.
2295 ShellCommandLineGetRawValue (
2296 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2303 // check for CheckPackage == NULL
2305 if (CheckPackage
== NULL
) {
2310 // enumerate through the list of parametrs
2312 for ( Node
= GetFirstNode(CheckPackage
)
2313 ; !IsNull (CheckPackage
, Node
)
2314 ; Node
= GetNextNode(CheckPackage
, Node
)
2317 // If the position matches, return the value
2319 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2320 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2327 returns the number of command line value parameters that were parsed.
2329 this will not include flags.
2331 @param[in] CheckPackage The package of parsed command line arguments.
2333 @retval (UINTN)-1 No parsing has ocurred
2334 @return other The number of value parameters found
2338 ShellCommandLineGetCount(
2339 IN CONST LIST_ENTRY
*CheckPackage
2345 if (CheckPackage
== NULL
) {
2348 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2349 ; !IsNull (CheckPackage
, Node1
)
2350 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2352 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2360 Determins if a parameter is duplicated.
2362 If Param is not NULL then it will point to a callee allocated string buffer
2363 with the parameter value if a duplicate is found.
2365 If CheckPackage is NULL, then ASSERT.
2367 @param[in] CheckPackage The package of parsed command line arguments.
2368 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2370 @retval EFI_SUCCESS No parameters were duplicated.
2371 @retval EFI_DEVICE_ERROR A duplicate was found.
2375 ShellCommandLineCheckDuplicate (
2376 IN CONST LIST_ENTRY
*CheckPackage
,
2383 ASSERT(CheckPackage
!= NULL
);
2385 for ( Node1
= GetFirstNode(CheckPackage
)
2386 ; !IsNull (CheckPackage
, Node1
)
2387 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2389 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2390 ; !IsNull (CheckPackage
, Node2
)
2391 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2393 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) {
2394 if (Param
!= NULL
) {
2396 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2398 return (EFI_DEVICE_ERROR
);
2402 return (EFI_SUCCESS
);
2406 This is a find and replace function. Upon successful return the NewString is a copy of
2407 SourceString with each instance of FindTarget replaced with ReplaceWith.
2409 If SourceString and NewString overlap the behavior is undefined.
2411 If the string would grow bigger than NewSize it will halt and return error.
2413 @param[in] SourceString The string with source buffer.
2414 @param[in,out] NewString The string with resultant buffer.
2415 @param[in] NewSize The size in bytes of NewString.
2416 @param[in] FindTarget The string to look for.
2417 @param[in] ReplaceWith The string to replace FindTarget with.
2418 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2419 immediately before it.
2420 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2422 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2423 @retval EFI_INVALID_PARAMETER NewString was NULL.
2424 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2425 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2426 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2427 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2428 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2429 the new string (truncation occurred).
2430 @retval EFI_SUCCESS The string was successfully copied with replacement.
2434 ShellCopySearchAndReplace(
2435 IN CHAR16 CONST
*SourceString
,
2436 IN OUT CHAR16
*NewString
,
2438 IN CONST CHAR16
*FindTarget
,
2439 IN CONST CHAR16
*ReplaceWith
,
2440 IN CONST BOOLEAN SkipPreCarrot
,
2441 IN CONST BOOLEAN ParameterReplacing
2447 if ( (SourceString
== NULL
)
2448 || (NewString
== NULL
)
2449 || (FindTarget
== NULL
)
2450 || (ReplaceWith
== NULL
)
2451 || (StrLen(FindTarget
) < 1)
2452 || (StrLen(SourceString
) < 1)
2454 return (EFI_INVALID_PARAMETER
);
2457 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2458 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2460 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2461 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2463 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2464 while (*SourceString
!= CHAR_NULL
) {
2466 // if we find the FindTarget and either Skip == FALSE or Skip and we
2467 // dont have a carrot do a replace...
2469 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2470 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2472 SourceString
+= StrLen(FindTarget
);
2473 Size
= StrSize(NewString
);
2474 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2476 return (EFI_BUFFER_TOO_SMALL
);
2478 StrCat(NewString
, Replace
);
2480 Size
= StrSize(NewString
);
2481 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2483 return (EFI_BUFFER_TOO_SMALL
);
2485 StrnCat(NewString
, SourceString
, 1);
2490 return (EFI_SUCCESS
);
2494 Internal worker function to output a string.
2496 This function will output a string to the correct StdOut.
2498 @param[in] String The string to print out.
2500 @retval EFI_SUCCESS The operation was sucessful.
2501 @retval !EFI_SUCCESS The operation failed.
2506 IN CONST CHAR16
*String
2510 Size
= StrSize(String
) - sizeof(CHAR16
);
2512 return (EFI_SUCCESS
);
2514 if (mEfiShellParametersProtocol
!= NULL
) {
2515 return (mEfiShellProtocol
->WriteFile(mEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2517 if (mEfiShellInterface
!= NULL
) {
2519 // Divide in half for old shell. Must be string length not size.
2522 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2525 return (EFI_UNSUPPORTED
);
2529 Print at a specific location on the screen.
2531 This function will move the cursor to a given screen location and print the specified string
2533 If -1 is specified for either the Row or Col the current screen location for BOTH
2536 if either Row or Col is out of range for the current console, then ASSERT
2537 if Format is NULL, then ASSERT
2539 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2540 the following additional flags:
2541 %N - Set output attribute to normal
2542 %H - Set output attribute to highlight
2543 %E - Set output attribute to error
2544 %B - Set output attribute to blue color
2545 %V - Set output attribute to green color
2547 Note: The background color is controlled by the shell command cls.
2549 @param[in] Row the row to print at
2550 @param[in] Col the column to print at
2551 @param[in] Format the format string
2552 @param[in] Marker the marker for the variable argument list
2554 @return EFI_SUCCESS The operation was successful.
2555 @return EFI_DEVICE_ERROR The console device reported an error.
2559 InternalShellPrintWorker(
2560 IN INT32 Col OPTIONAL
,
2561 IN INT32 Row OPTIONAL
,
2562 IN CONST CHAR16
*Format
,
2567 CHAR16
*ResumeLocation
;
2568 CHAR16
*FormatWalker
;
2569 UINTN OriginalAttribute
;
2571 Status
= EFI_SUCCESS
;
2572 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2575 // Back and forth each time fixing up 1 of our flags...
2577 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2578 ASSERT_EFI_ERROR(Status
);
2579 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2580 ASSERT_EFI_ERROR(Status
);
2581 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2582 ASSERT_EFI_ERROR(Status
);
2583 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2584 ASSERT_EFI_ERROR(Status
);
2585 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2586 ASSERT_EFI_ERROR(Status
);
2589 // Use the last buffer from replacing to print from...
2591 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2593 if (Col
!= -1 && Row
!= -1) {
2594 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2595 ASSERT_EFI_ERROR(Status
);
2598 FormatWalker
= mPostReplaceFormat2
;
2599 while (*FormatWalker
!= CHAR_NULL
) {
2601 // Find the next attribute change request
2603 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2604 if (ResumeLocation
!= NULL
) {
2605 *ResumeLocation
= CHAR_NULL
;
2608 // print the current FormatWalker string
2610 if (StrLen(FormatWalker
)>0) {
2611 Status
= InternalPrintTo(FormatWalker
);
2612 if (EFI_ERROR(Status
)) {
2618 // update the attribute
2620 if (ResumeLocation
!= NULL
) {
2621 switch (*(ResumeLocation
+1)) {
2623 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2626 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2629 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2632 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2635 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2639 // Print a simple '%' symbol
2641 Status
= InternalPrintTo(L
"%");
2642 if (EFI_ERROR(Status
)) {
2645 ResumeLocation
= ResumeLocation
- 1;
2650 // reset to normal now...
2656 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2658 FormatWalker
= ResumeLocation
+ 2;
2661 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2666 Print at a specific location on the screen.
2668 This function will move the cursor to a given screen location and print the specified string.
2670 If -1 is specified for either the Row or Col the current screen location for BOTH
2673 If either Row or Col is out of range for the current console, then ASSERT.
2674 If Format is NULL, then ASSERT.
2676 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2677 the following additional flags:
2678 %N - Set output attribute to normal
2679 %H - Set output attribute to highlight
2680 %E - Set output attribute to error
2681 %B - Set output attribute to blue color
2682 %V - Set output attribute to green color
2684 Note: The background color is controlled by the shell command cls.
2686 @param[in] Col the column to print at
2687 @param[in] Row the row to print at
2688 @param[in] Format the format string
2689 @param[in] ... The variable argument list.
2691 @return EFI_SUCCESS The printing was successful.
2692 @return EFI_DEVICE_ERROR The console device reported an error.
2697 IN INT32 Col OPTIONAL
,
2698 IN INT32 Row OPTIONAL
,
2699 IN CONST CHAR16
*Format
,
2705 VA_START (Marker
, Format
);
2706 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2712 Print at a specific location on the screen.
2714 This function will move the cursor to a given screen location and print the specified string.
2716 If -1 is specified for either the Row or Col the current screen location for BOTH
2719 If either Row or Col is out of range for the current console, then ASSERT.
2720 If Format is NULL, then ASSERT.
2722 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2723 the following additional flags:
2724 %N - Set output attribute to normal.
2725 %H - Set output attribute to highlight.
2726 %E - Set output attribute to error.
2727 %B - Set output attribute to blue color.
2728 %V - Set output attribute to green color.
2730 Note: The background color is controlled by the shell command cls.
2732 @param[in] Col The column to print at.
2733 @param[in] Row The row to print at.
2734 @param[in] Language The language of the string to retrieve. If this parameter
2735 is NULL, then the current platform language is used.
2736 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2737 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2738 @param[in] ... The variable argument list.
2740 @return EFI_SUCCESS The printing was successful.
2741 @return EFI_DEVICE_ERROR The console device reported an error.
2746 IN INT32 Col OPTIONAL
,
2747 IN INT32 Row OPTIONAL
,
2748 IN CONST CHAR8
*Language OPTIONAL
,
2749 IN CONST EFI_STRING_ID HiiFormatStringId
,
2750 IN CONST EFI_HANDLE HiiFormatHandle
,
2755 CHAR16
*HiiFormatString
;
2758 VA_START (Marker
, HiiFormatHandle
);
2759 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2760 ASSERT(HiiFormatString
!= NULL
);
2762 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2764 SHELL_FREE_NON_NULL(HiiFormatString
);
2771 Function to determine if a given filename represents a file or a directory.
2773 @param[in] DirName Path to directory to test.
2775 @retval EFI_SUCCESS The Path represents a directory
2776 @retval EFI_NOT_FOUND The Path does not represent a directory
2777 @return other The path failed to open
2782 IN CONST CHAR16
*DirName
2786 SHELL_FILE_HANDLE Handle
;
2787 CHAR16
*TempLocation
;
2789 ASSERT(DirName
!= NULL
);
2792 TempLocation
= NULL
;
2794 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2795 if (EFI_ERROR(Status
)) {
2797 // try good logic first.
2799 if (mEfiShellProtocol
!= NULL
) {
2800 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
2801 if (StrStr(TempLocation
, L
":") != NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
2802 *(StrStr(TempLocation
, L
":")+1) = CHAR_NULL
;
2804 if (mEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
2805 FreePool(TempLocation
);
2806 return (EFI_SUCCESS
);
2808 FreePool(TempLocation
);
2811 // probably a map name?!?!!?
2813 TempLocation
= StrStr(DirName
, L
"\\");
2814 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
2815 return (EFI_SUCCESS
);
2821 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2822 ShellCloseFile(&Handle
);
2823 return (EFI_SUCCESS
);
2825 ShellCloseFile(&Handle
);
2826 return (EFI_NOT_FOUND
);
2830 Function to determine if a given filename represents a file.
2832 @param[in] Name Path to file to test.
2834 @retval EFI_SUCCESS The Path represents a file.
2835 @retval EFI_NOT_FOUND The Path does not represent a file.
2836 @retval other The path failed to open.
2841 IN CONST CHAR16
*Name
2845 SHELL_FILE_HANDLE Handle
;
2847 ASSERT(Name
!= NULL
);
2851 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
2852 if (EFI_ERROR(Status
)) {
2856 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
2857 ShellCloseFile(&Handle
);
2858 return (EFI_SUCCESS
);
2860 ShellCloseFile(&Handle
);
2861 return (EFI_NOT_FOUND
);
2865 Function to determine if a given filename represents a file.
2867 This will search the CWD and then the Path.
2869 If Name is NULL, then ASSERT.
2871 @param[in] Name Path to file to test.
2873 @retval EFI_SUCCESS The Path represents a file.
2874 @retval EFI_NOT_FOUND The Path does not represent a file.
2875 @retval other The path failed to open.
2880 IN CONST CHAR16
*Name
2886 if (!EFI_ERROR(ShellIsFile(Name
))) {
2887 return (EFI_SUCCESS
);
2890 NewName
= ShellFindFilePath(Name
);
2891 if (NewName
== NULL
) {
2892 return (EFI_NOT_FOUND
);
2894 Status
= ShellIsFile(NewName
);
2899 Function to determine whether a string is decimal or hex representation of a number
2900 and return the number converted from the string.
2902 @param[in] String String representation of a number
2904 @retval all the number
2909 IN CONST CHAR16
*String
2912 CONST CHAR16
*Walker
;
2913 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
2914 if (Walker
== NULL
|| *Walker
== CHAR_NULL
) {
2916 return ((UINTN
)(-1));
2918 if (StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
2919 return (StrHexToUintn(Walker
));
2921 return (StrDecimalToUintn(Walker
));
2926 Safely append with automatic string resizing given length of Destination and
2927 desired length of copy from Source.
2929 append the first D characters of Source to the end of Destination, where D is
2930 the lesser of Count and the StrLen() of Source. If appending those D characters
2931 will fit within Destination (whose Size is given as CurrentSize) and
2932 still leave room for a NULL terminator, then those characters are appended,
2933 starting at the original terminating NULL of Destination, and a new terminating
2936 If appending D characters onto Destination will result in a overflow of the size
2937 given in CurrentSize the string will be grown such that the copy can be performed
2938 and CurrentSize will be updated to the new size.
2940 If Source is NULL, there is nothing to append, just return the current buffer in
2943 if Destination is NULL, then ASSERT()
2944 if Destination's current length (including NULL terminator) is already more then
2945 CurrentSize, then ASSERT()
2947 @param[in,out] Destination The String to append onto
2948 @param[in,out] CurrentSize on call the number of bytes in Destination. On
2949 return possibly the new size (still in bytes). if NULL
2950 then allocate whatever is needed.
2951 @param[in] Source The String to append from
2952 @param[in] Count Maximum number of characters to append. if 0 then
2955 @return Destination return the resultant string.
2960 IN OUT CHAR16
**Destination
,
2961 IN OUT UINTN
*CurrentSize
,
2962 IN CONST CHAR16
*Source
,
2966 UINTN DestinationStartSize
;
2972 ASSERT(Destination
!= NULL
);
2975 // If there's nothing to do then just return Destination
2977 if (Source
== NULL
) {
2978 return (*Destination
);
2982 // allow for un-initialized pointers, based on size being 0
2984 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
2985 *Destination
= NULL
;
2989 // allow for NULL pointers address as Destination
2991 if (*Destination
!= NULL
) {
2992 ASSERT(CurrentSize
!= 0);
2993 DestinationStartSize
= StrSize(*Destination
);
2994 ASSERT(DestinationStartSize
<= *CurrentSize
);
2996 DestinationStartSize
= 0;
2997 // ASSERT(*CurrentSize == 0);
3001 // Append all of Source?
3004 Count
= StrLen(Source
);
3008 // Test and grow if required
3010 if (CurrentSize
!= NULL
) {
3011 NewSize
= *CurrentSize
;
3012 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3013 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3015 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3016 ASSERT(*Destination
!= NULL
);
3017 *CurrentSize
= NewSize
;
3019 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
3020 ASSERT(*Destination
!= NULL
);
3024 // Now use standard StrnCat on a big enough buffer
3026 if (*Destination
== NULL
) {
3029 return StrnCat(*Destination
, Source
, Count
);
3033 Prompt the user and return the resultant answer to the requestor.
3035 This function will display the requested question on the shell prompt and then
3036 wait for an apropriate answer to be input from the console.
3038 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3039 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3041 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3044 In either case *Response must be callee freed if Response was not NULL;
3046 @param Type What type of question is asked. This is used to filter the input
3047 to prevent invalid answers to question.
3048 @param Prompt Pointer to string prompt to use to request input.
3049 @param Response Pointer to Response which will be populated upon return.
3051 @retval EFI_SUCCESS The operation was sucessful.
3052 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3053 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3054 @return other The operation failed.
3058 ShellPromptForResponse (
3059 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3060 IN CHAR16
*Prompt OPTIONAL
,
3061 IN OUT VOID
**Response OPTIONAL
3067 SHELL_PROMPT_RESPONSE
*Resp
;
3071 Status
= EFI_UNSUPPORTED
;
3075 if (Type
!= ShellPromptResponseTypeFreeform
) {
3076 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocatePool(sizeof(SHELL_PROMPT_RESPONSE
));
3080 case ShellPromptResponseTypeQuitContinue
:
3081 if (Prompt
!= NULL
) {
3082 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3085 // wait for valid response
3087 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3088 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3089 ASSERT_EFI_ERROR(Status
);
3090 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3091 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3092 *Resp
= ShellPromptResponseQuit
;
3094 *Resp
= ShellPromptResponseContinue
;
3097 case ShellPromptResponseTypeYesNoCancel
:
3098 if (Prompt
!= NULL
) {
3099 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3102 // wait for valid response
3104 *Resp
= ShellPromptResponseMax
;
3105 while (*Resp
== ShellPromptResponseMax
) {
3106 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3107 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3108 ASSERT_EFI_ERROR(Status
);
3109 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3110 switch (Key
.UnicodeChar
) {
3113 *Resp
= ShellPromptResponseYes
;
3117 *Resp
= ShellPromptResponseNo
;
3121 *Resp
= ShellPromptResponseCancel
;
3125 break; case ShellPromptResponseTypeYesNoAllCancel
:
3126 if (Prompt
!= NULL
) {
3127 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3130 // wait for valid response
3132 *Resp
= ShellPromptResponseMax
;
3133 while (*Resp
== ShellPromptResponseMax
) {
3134 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3135 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3136 ASSERT_EFI_ERROR(Status
);
3137 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3138 switch (Key
.UnicodeChar
) {
3141 *Resp
= ShellPromptResponseYes
;
3145 *Resp
= ShellPromptResponseNo
;
3149 *Resp
= ShellPromptResponseAll
;
3153 *Resp
= ShellPromptResponseCancel
;
3158 case ShellPromptResponseTypeEnterContinue
:
3159 case ShellPromptResponseTypeAnyKeyContinue
:
3160 if (Prompt
!= NULL
) {
3161 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3164 // wait for valid response
3166 *Resp
= ShellPromptResponseMax
;
3167 while (*Resp
== ShellPromptResponseMax
) {
3168 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3169 if (Type
== ShellPromptResponseTypeEnterContinue
) {
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
== CHAR_CARRIAGE_RETURN
) {
3174 *Resp
= ShellPromptResponseContinue
;
3178 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3179 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3180 ASSERT_EFI_ERROR(Status
);
3181 *Resp
= ShellPromptResponseContinue
;
3186 case ShellPromptResponseTypeYesNo
:
3187 if (Prompt
!= NULL
) {
3188 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3191 // wait for valid response
3193 *Resp
= ShellPromptResponseMax
;
3194 while (*Resp
== ShellPromptResponseMax
) {
3195 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3196 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3197 ASSERT_EFI_ERROR(Status
);
3198 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3199 switch (Key
.UnicodeChar
) {
3202 *Resp
= ShellPromptResponseYes
;
3206 *Resp
= ShellPromptResponseNo
;
3211 case ShellPromptResponseTypeFreeform
:
3212 if (Prompt
!= NULL
) {
3213 ShellPrintEx(-1, -1, L
"%s", Prompt
);
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 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3223 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3224 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3228 // This is the location to add new prompt types.
3234 if (Response
!= NULL
) {
3237 } else if (Buffer
!= NULL
) {
3244 if (Buffer
!= NULL
) {
3249 ShellPrintEx(-1, -1, L
"\r\n");
3254 Prompt the user and return the resultant answer to the requestor.
3256 This function is the same as ShellPromptForResponse, except that the prompt is
3257 automatically pulled from HII.
3259 @param Type What type of question is asked. This is used to filter the input
3260 to prevent invalid answers to question.
3261 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3262 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3263 @param Response Pointer to Response which will be populated upon return.
3265 @retval EFI_SUCCESS the operation was sucessful.
3266 @return other the operation failed.
3268 @sa ShellPromptForResponse
3272 ShellPromptForResponseHii (
3273 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3274 IN CONST EFI_STRING_ID HiiFormatStringId
,
3275 IN CONST EFI_HANDLE HiiFormatHandle
,
3276 IN OUT VOID
**Response
3282 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3283 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3289 Function to determin if an entire string is a valid number.
3291 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3293 @param[in] String The string to evaluate.
3294 @param[in] ForceHex TRUE - always assume hex.
3295 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3297 @retval TRUE It is all numeric (dec/hex) characters.
3298 @retval FALSE There is a non-numeric character.
3302 ShellIsHexOrDecimalNumber (
3303 IN CONST CHAR16
*String
,
3304 IN CONST BOOLEAN ForceHex
,
3305 IN CONST BOOLEAN StopAtSpace
3311 // chop off a single negative sign
3313 if (String
!= NULL
&& *String
== L
'-') {
3317 if (String
== NULL
) {
3322 // chop leading zeroes
3324 while(String
!= NULL
&& *String
== L
'0'){
3328 // allow '0x' or '0X', but not 'x' or 'X'
3330 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3331 if (*(String
-1) != L
'0') {
3333 // we got an x without a preceeding 0
3339 } else if (ForceHex
) {
3346 // loop through the remaining characters and use the lib function
3348 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3350 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3354 if (!ShellIsDecimalDigitCharacter(*String
)) {
3363 Function to determine if a given filename exists.
3365 @param[in] Name Path to test.
3367 @retval EFI_SUCCESS The Path represents a file.
3368 @retval EFI_NOT_FOUND The Path does not represent a file.
3369 @retval other The path failed to open.
3374 IN CONST CHAR16
*Name
3378 EFI_SHELL_FILE_INFO
*List
;
3380 ASSERT(Name
!= NULL
);
3383 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3384 if (EFI_ERROR(Status
)) {
3388 ShellCloseFileMetaArg(&List
);
3390 return (EFI_SUCCESS
);