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
*mEfiShellProtocol
;
34 EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
35 EFI_HANDLE mEfiShellEnvironment2Handle
;
36 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
37 CHAR16
*mPostReplaceFormat
;
38 CHAR16
*mPostReplaceFormat2
;
41 Check if a Unicode character is a hexadecimal character.
43 This internal function checks if a Unicode character is a
44 numeric character. The valid hexadecimal characters are
45 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
47 @param Char The character to check against.
49 @retval TRUE If the Char is a hexadecmial character.
50 @retval FALSE If the Char is not a hexadecmial character.
55 ShellIsHexaDecimalDigitCharacter (
59 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
63 Check if a Unicode character is a decimal character.
65 This internal function checks if a Unicode character is a
66 decimal character. The valid characters are
70 @param Char The character to check against.
72 @retval TRUE If the Char is a hexadecmial character.
73 @retval FALSE If the Char is not a hexadecmial character.
78 ShellIsDecimalDigitCharacter (
82 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
86 Helper function to find ShellEnvironment2 for constructor.
88 @param[in] ImageHandle A copy of the calling image's handle.
93 IN EFI_HANDLE ImageHandle
103 Status
= gBS
->OpenProtocol(ImageHandle
,
104 &gEfiShellEnvironment2Guid
,
105 (VOID
**)&mEfiShellEnvironment2
,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
111 // look for the mEfiShellEnvironment2 protocol at a higher level
113 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
115 // figure out how big of a buffer we need.
117 Status
= gBS
->LocateHandle (ByProtocol
,
118 &gEfiShellEnvironment2Guid
,
119 NULL
, // ignored for ByProtocol
124 // maybe it's not there???
126 if (Status
== EFI_BUFFER_TOO_SMALL
) {
127 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
128 ASSERT(Buffer
!= NULL
);
129 Status
= gBS
->LocateHandle (ByProtocol
,
130 &gEfiShellEnvironment2Guid
,
131 NULL
, // ignored for ByProtocol
136 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
138 // now parse the list of returned handles
140 Status
= EFI_NOT_FOUND
;
141 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
142 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
143 &gEfiShellEnvironment2Guid
,
144 (VOID
**)&mEfiShellEnvironment2
,
147 EFI_OPEN_PROTOCOL_GET_PROTOCOL
149 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
150 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
151 Status
= EFI_SUCCESS
;
157 if (Buffer
!= NULL
) {
164 Function to do most of the work of the constructor. Allows for calling
165 multiple times without complete re-initialization.
167 @param[in] ImageHandle A copy of the ImageHandle.
168 @param[in] SystemTable A pointer to the SystemTable for the application.
170 @retval EFI_SUCCESS The operationw as successful.
174 ShellLibConstructorWorker (
175 IN EFI_HANDLE ImageHandle
,
176 IN EFI_SYSTEM_TABLE
*SystemTable
180 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
181 ASSERT (mPostReplaceFormat
!= NULL
);
182 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
183 ASSERT (mPostReplaceFormat2
!= NULL
);
186 // UEFI 2.0 shell interfaces (used preferentially)
188 Status
= gBS
->OpenProtocol(
190 &gEfiShellProtocolGuid
,
191 (VOID
**)&mEfiShellProtocol
,
194 EFI_OPEN_PROTOCOL_GET_PROTOCOL
196 if (EFI_ERROR(Status
)) {
198 // Search for the shell protocol
200 Status
= gBS
->LocateProtocol(
201 &gEfiShellProtocolGuid
,
203 (VOID
**)&mEfiShellProtocol
205 if (EFI_ERROR(Status
)) {
206 mEfiShellProtocol
= NULL
;
209 Status
= gBS
->OpenProtocol(
211 &gEfiShellParametersProtocolGuid
,
212 (VOID
**)&mEfiShellParametersProtocol
,
215 EFI_OPEN_PROTOCOL_GET_PROTOCOL
217 if (EFI_ERROR(Status
)) {
218 mEfiShellParametersProtocol
= NULL
;
221 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
223 // Moved to seperate function due to complexity
225 Status
= ShellFindSE2(ImageHandle
);
227 if (EFI_ERROR(Status
)) {
228 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
229 mEfiShellEnvironment2
= NULL
;
231 Status
= gBS
->OpenProtocol(ImageHandle
,
232 &gEfiShellInterfaceGuid
,
233 (VOID
**)&mEfiShellInterface
,
236 EFI_OPEN_PROTOCOL_GET_PROTOCOL
238 if (EFI_ERROR(Status
)) {
239 mEfiShellInterface
= NULL
;
244 // only success getting 2 of either the old or new, but no 1/2 and 1/2
246 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
247 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
248 if (mEfiShellProtocol
!= NULL
) {
249 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
250 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
251 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
252 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
253 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
254 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
255 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
256 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
257 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
258 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
260 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
261 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
262 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
263 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
264 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
265 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
266 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
267 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
268 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
269 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
271 return (EFI_SUCCESS
);
273 return (EFI_NOT_FOUND
);
276 Constructor for the Shell library.
278 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
280 @param ImageHandle the image handle of the process
281 @param SystemTable the EFI System Table pointer
283 @retval EFI_SUCCESS the initialization was complete sucessfully
284 @return others an error ocurred during initialization
288 ShellLibConstructor (
289 IN EFI_HANDLE ImageHandle
,
290 IN EFI_SYSTEM_TABLE
*SystemTable
293 mEfiShellEnvironment2
= NULL
;
294 mEfiShellProtocol
= NULL
;
295 mEfiShellParametersProtocol
= NULL
;
296 mEfiShellInterface
= NULL
;
297 mEfiShellEnvironment2Handle
= NULL
;
298 mPostReplaceFormat
= NULL
;
299 mPostReplaceFormat2
= NULL
;
302 // verify that auto initialize is not set false
304 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
305 return (EFI_SUCCESS
);
308 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
312 Destructor for the library. free any resources.
314 @param[in] ImageHandle A copy of the ImageHandle.
315 @param[in] SystemTable A pointer to the SystemTable for the application.
317 @retval EFI_SUCCESS The operation was successful.
318 @return An error from the CloseProtocol function.
323 IN EFI_HANDLE ImageHandle
,
324 IN EFI_SYSTEM_TABLE
*SystemTable
327 if (mEfiShellEnvironment2
!= NULL
) {
328 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
329 &gEfiShellEnvironment2Guid
,
332 mEfiShellEnvironment2
= NULL
;
334 if (mEfiShellInterface
!= NULL
) {
335 gBS
->CloseProtocol(ImageHandle
,
336 &gEfiShellInterfaceGuid
,
339 mEfiShellInterface
= NULL
;
341 if (mEfiShellProtocol
!= NULL
) {
342 gBS
->CloseProtocol(ImageHandle
,
343 &gEfiShellProtocolGuid
,
346 mEfiShellProtocol
= NULL
;
348 if (mEfiShellParametersProtocol
!= NULL
) {
349 gBS
->CloseProtocol(ImageHandle
,
350 &gEfiShellParametersProtocolGuid
,
353 mEfiShellParametersProtocol
= NULL
;
355 mEfiShellEnvironment2Handle
= NULL
;
357 if (mPostReplaceFormat
!= NULL
) {
358 FreePool(mPostReplaceFormat
);
360 if (mPostReplaceFormat2
!= NULL
) {
361 FreePool(mPostReplaceFormat2
);
363 mPostReplaceFormat
= NULL
;
364 mPostReplaceFormat2
= NULL
;
366 return (EFI_SUCCESS
);
370 This function causes the shell library to initialize itself. If the shell library
371 is already initialized it will de-initialize all the current protocol poitners and
372 re-populate them again.
374 When the library is used with PcdShellLibAutoInitialize set to true this function
375 will return EFI_SUCCESS and perform no actions.
377 This function is intended for internal access for shell commands only.
379 @retval EFI_SUCCESS the initialization was complete sucessfully
388 // if auto initialize is not false then skip
390 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
391 return (EFI_SUCCESS
);
395 // deinit the current stuff
397 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
400 // init the new stuff
402 return (ShellLibConstructorWorker(gImageHandle
, gST
));
406 This function will retrieve the information about the file for the handle
407 specified and store it in allocated pool memory.
409 This function allocates a buffer to store the file's information. It is the
410 caller's responsibility to free the buffer
412 @param FileHandle The file handle of the file for which information is
415 @retval NULL information could not be retrieved.
417 @return the information about the file
422 IN SHELL_FILE_HANDLE FileHandle
425 return (FileFunctionMap
.GetFileInfo(FileHandle
));
429 This function sets the information about the file for the opened handle
432 @param[in] FileHandle The file handle of the file for which information
435 @param[in] FileInfo The information to set.
437 @retval EFI_SUCCESS The information was set.
438 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
439 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
440 @retval EFI_NO_MEDIA The device has no medium.
441 @retval EFI_DEVICE_ERROR The device reported an error.
442 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
443 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
444 @retval EFI_ACCESS_DENIED The file was opened read only.
445 @retval EFI_VOLUME_FULL The volume is full.
450 IN SHELL_FILE_HANDLE FileHandle
,
451 IN EFI_FILE_INFO
*FileInfo
454 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
458 This function will open a file or directory referenced by DevicePath.
460 This function opens a file with the open mode according to the file path. The
461 Attributes is valid only for EFI_FILE_MODE_CREATE.
463 @param FilePath on input the device path to the file. On output
464 the remaining device path.
465 @param DeviceHandle pointer to the system device handle.
466 @param FileHandle pointer to the file handle.
467 @param OpenMode the mode to open the file with.
468 @param Attributes the file's file attributes.
470 @retval EFI_SUCCESS The information was set.
471 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
472 @retval EFI_UNSUPPORTED Could not open the file path.
473 @retval EFI_NOT_FOUND The specified file could not be found on the
474 device or the file system could not be found on
476 @retval EFI_NO_MEDIA The device has no medium.
477 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
478 medium is no longer supported.
479 @retval EFI_DEVICE_ERROR The device reported an error.
480 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
481 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
482 @retval EFI_ACCESS_DENIED The file was opened read only.
483 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
485 @retval EFI_VOLUME_FULL The volume is full.
489 ShellOpenFileByDevicePath(
490 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
491 OUT EFI_HANDLE
*DeviceHandle
,
492 OUT SHELL_FILE_HANDLE
*FileHandle
,
499 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
500 EFI_FILE_PROTOCOL
*Handle1
;
501 EFI_FILE_PROTOCOL
*Handle2
;
504 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
506 ASSERT(FilePath
!= NULL
);
507 ASSERT(FileHandle
!= NULL
);
508 ASSERT(DeviceHandle
!= NULL
);
510 // which shell interface should we use
512 if (mEfiShellProtocol
!= NULL
) {
514 // use UEFI Shell 2.0 method.
516 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
517 if (FileName
== NULL
) {
518 return (EFI_INVALID_PARAMETER
);
520 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
527 // use old shell method.
529 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
532 if (EFI_ERROR (Status
)) {
535 Status
= gBS
->OpenProtocol(*DeviceHandle
,
536 &gEfiSimpleFileSystemProtocolGuid
,
537 (VOID
**)&EfiSimpleFileSystemProtocol
,
540 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
541 if (EFI_ERROR (Status
)) {
544 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
545 if (EFI_ERROR (Status
)) {
551 // go down directories one node at a time.
553 while (!IsDevicePathEnd (*FilePath
)) {
555 // For file system access each node should be a file path component
557 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
558 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
561 return (EFI_INVALID_PARAMETER
);
564 // Open this file path node
570 // Try to test opening an existing file
572 Status
= Handle2
->Open (
575 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
576 OpenMode
&~EFI_FILE_MODE_CREATE
,
581 // see if the error was that it needs to be created
583 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
584 Status
= Handle2
->Open (
587 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
593 // Close the last node
595 Handle2
->Close (Handle2
);
597 if (EFI_ERROR(Status
)) {
604 *FilePath
= NextDevicePathNode (*FilePath
);
608 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
610 *FileHandle
= (VOID
*)Handle1
;
611 return (EFI_SUCCESS
);
615 This function will open a file or directory referenced by filename.
617 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
618 otherwise, the Filehandle is NULL. The Attributes is valid only for
619 EFI_FILE_MODE_CREATE.
621 if FileNAme is NULL then ASSERT()
623 @param FileName pointer to file name
624 @param FileHandle pointer to the file handle.
625 @param OpenMode the mode to open the file with.
626 @param Attributes the file's file attributes.
628 @retval EFI_SUCCESS The information was set.
629 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
630 @retval EFI_UNSUPPORTED Could not open the file path.
631 @retval EFI_NOT_FOUND The specified file could not be found on the
632 device or the file system could not be found
634 @retval EFI_NO_MEDIA The device has no medium.
635 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
636 medium is no longer supported.
637 @retval EFI_DEVICE_ERROR The device reported an error.
638 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
639 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
640 @retval EFI_ACCESS_DENIED The file was opened read only.
641 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
643 @retval EFI_VOLUME_FULL The volume is full.
648 IN CONST CHAR16
*FileName
,
649 OUT SHELL_FILE_HANDLE
*FileHandle
,
654 EFI_HANDLE DeviceHandle
;
655 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
657 EFI_FILE_INFO
*FileInfo
;
660 // ASSERT if FileName is NULL
662 ASSERT(FileName
!= NULL
);
664 if (FileName
== NULL
) {
665 return (EFI_INVALID_PARAMETER
);
668 if (mEfiShellProtocol
!= NULL
) {
669 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
&& (Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
670 return ShellCreateDirectory(FileName
, FileHandle
);
673 // Use UEFI Shell 2.0 method
675 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
678 if (StrCmp(FileName
, L
"NUL") != 0 && !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
679 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
680 ASSERT(FileInfo
!= NULL
);
681 FileInfo
->Attribute
= Attributes
;
682 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
688 // Using EFI Shell version
689 // this means convert name to path and call that function
690 // since this will use EFI method again that will open it.
692 ASSERT(mEfiShellEnvironment2
!= NULL
);
693 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
694 if (FilePath
!= NULL
) {
695 return (ShellOpenFileByDevicePath(&FilePath
,
701 return (EFI_DEVICE_ERROR
);
704 This function create a directory
706 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
707 otherwise, the Filehandle is NULL. If the directory already existed, this
708 function opens the existing directory.
710 @param DirectoryName pointer to directory name
711 @param FileHandle pointer to the file handle.
713 @retval EFI_SUCCESS The information was set.
714 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
715 @retval EFI_UNSUPPORTED Could not open the file path.
716 @retval EFI_NOT_FOUND The specified file could not be found on the
717 device or the file system could not be found
719 @retval EFI_NO_MEDIA The device has no medium.
720 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
721 medium is no longer supported.
722 @retval EFI_DEVICE_ERROR The device reported an error.
723 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
724 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
725 @retval EFI_ACCESS_DENIED The file was opened read only.
726 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
728 @retval EFI_VOLUME_FULL The volume is full.
729 @sa ShellOpenFileByName
733 ShellCreateDirectory(
734 IN CONST CHAR16
*DirectoryName
,
735 OUT SHELL_FILE_HANDLE
*FileHandle
738 if (mEfiShellProtocol
!= NULL
) {
740 // Use UEFI Shell 2.0 method
742 return (mEfiShellProtocol
->CreateFile(DirectoryName
,
747 return (ShellOpenFileByName(DirectoryName
,
749 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
756 This function reads information from an opened file.
758 If FileHandle is not a directory, the function reads the requested number of
759 bytes from the file at the file's current position and returns them in Buffer.
760 If the read goes beyond the end of the file, the read length is truncated to the
761 end of the file. The file's current position is increased by the number of bytes
762 returned. If FileHandle is a directory, the function reads the directory entry
763 at the file's current position and returns the entry in Buffer. If the Buffer
764 is not large enough to hold the current directory entry, then
765 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
766 BufferSize is set to be the size of the buffer needed to read the entry. On
767 success, the current position is updated to the next directory entry. If there
768 are no more directory entries, the read returns a zero-length buffer.
769 EFI_FILE_INFO is the structure returned as the directory entry.
771 @param FileHandle the opened file handle
772 @param BufferSize on input the size of buffer in bytes. on return
773 the number of bytes written.
774 @param Buffer the buffer to put read data into.
776 @retval EFI_SUCCESS Data was read.
777 @retval EFI_NO_MEDIA The device has no media.
778 @retval EFI_DEVICE_ERROR The device reported an error.
779 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
780 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
787 IN SHELL_FILE_HANDLE FileHandle
,
788 IN OUT UINTN
*BufferSize
,
792 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
797 Write data to a file.
799 This function writes the specified number of bytes to the file at the current
800 file position. The current file position is advanced the actual number of bytes
801 written, which is returned in BufferSize. Partial writes only occur when there
802 has been a data error during the write attempt (such as "volume space full").
803 The file is automatically grown to hold the data if required. Direct writes to
804 opened directories are not supported.
806 @param FileHandle The opened file for writing
807 @param BufferSize on input the number of bytes in Buffer. On output
808 the number of bytes written.
809 @param Buffer the buffer containing data to write is stored.
811 @retval EFI_SUCCESS Data was written.
812 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
813 @retval EFI_NO_MEDIA The device has no media.
814 @retval EFI_DEVICE_ERROR The device reported an error.
815 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
816 @retval EFI_WRITE_PROTECTED The device is write-protected.
817 @retval EFI_ACCESS_DENIED The file was open for read only.
818 @retval EFI_VOLUME_FULL The volume is full.
823 IN SHELL_FILE_HANDLE FileHandle
,
824 IN OUT UINTN
*BufferSize
,
828 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
832 Close an open file handle.
834 This function closes a specified file handle. All "dirty" cached file data is
835 flushed to the device, and the file is closed. In all cases the handle is
838 @param FileHandle the file handle to close.
840 @retval EFI_SUCCESS the file handle was closed sucessfully.
845 IN SHELL_FILE_HANDLE
*FileHandle
848 return (FileFunctionMap
.CloseFile(*FileHandle
));
852 Delete a file and close the handle
854 This function closes and deletes a file. In all cases the file handle is closed.
855 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
856 returned, but the handle is still closed.
858 @param FileHandle the file handle to delete
860 @retval EFI_SUCCESS the file was closed sucessfully
861 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
863 @retval INVALID_PARAMETER One of the parameters has an invalid value.
868 IN SHELL_FILE_HANDLE
*FileHandle
871 return (FileFunctionMap
.DeleteFile(*FileHandle
));
875 Set the current position in a file.
877 This function sets the current file position for the handle to the position
878 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
879 absolute positioning is supported, and seeking past the end of the file is
880 allowed (a subsequent write would grow the file). Seeking to position
881 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
882 If FileHandle is a directory, the only position that may be set is zero. This
883 has the effect of starting the read process of the directory entries over.
885 @param FileHandle The file handle on which the position is being set
886 @param Position Byte position from begining of file
888 @retval EFI_SUCCESS Operation completed sucessfully.
889 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
891 @retval INVALID_PARAMETER One of the parameters has an invalid value.
895 ShellSetFilePosition (
896 IN SHELL_FILE_HANDLE FileHandle
,
900 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
904 Gets a file's current position
906 This function retrieves the current file position for the file handle. For
907 directories, the current file position has no meaning outside of the file
908 system driver and as such the operation is not supported. An error is returned
909 if FileHandle is a directory.
911 @param FileHandle The open file handle on which to get the position.
912 @param Position Byte position from begining of file.
914 @retval EFI_SUCCESS the operation completed sucessfully.
915 @retval INVALID_PARAMETER One of the parameters has an invalid value.
916 @retval EFI_UNSUPPORTED the request is not valid on directories.
920 ShellGetFilePosition (
921 IN SHELL_FILE_HANDLE FileHandle
,
925 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
928 Flushes data on a file
930 This function flushes all modified data associated with a file to a device.
932 @param FileHandle The file handle on which to flush data
934 @retval EFI_SUCCESS The data was flushed.
935 @retval EFI_NO_MEDIA The device has no media.
936 @retval EFI_DEVICE_ERROR The device reported an error.
937 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
938 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
939 @retval EFI_ACCESS_DENIED The file was opened for read only.
944 IN SHELL_FILE_HANDLE FileHandle
947 return (FileFunctionMap
.FlushFile(FileHandle
));
951 Retrieves the first file from a directory
953 This function opens a directory and gets the first file's info in the
954 directory. Caller can use ShellFindNextFile() to get other files. When
955 complete the caller is responsible for calling FreePool() on Buffer.
957 @param DirHandle The file handle of the directory to search
958 @param Buffer Pointer to buffer for file's information
960 @retval EFI_SUCCESS Found the first file.
961 @retval EFI_NOT_FOUND Cannot find the directory.
962 @retval EFI_NO_MEDIA The device has no media.
963 @retval EFI_DEVICE_ERROR The device reported an error.
964 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
965 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
971 IN SHELL_FILE_HANDLE DirHandle
,
972 OUT EFI_FILE_INFO
**Buffer
976 // pass to file handle lib
978 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
981 Retrieves the next file in a directory.
983 To use this function, caller must call the ShellFindFirstFile() to get the
984 first file, and then use this function get other files. This function can be
985 called for several times to get each file's information in the directory. If
986 the call of ShellFindNextFile() got the last file in the directory, the next
987 call of this function has no file to get. *NoFile will be set to TRUE and the
988 Buffer memory will be automatically freed.
990 @param DirHandle the file handle of the directory
991 @param Buffer pointer to buffer for file's information
992 @param NoFile pointer to boolean when last file is found
994 @retval EFI_SUCCESS Found the next file, or reached last file
995 @retval EFI_NO_MEDIA The device has no media.
996 @retval EFI_DEVICE_ERROR The device reported an error.
997 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1002 IN SHELL_FILE_HANDLE DirHandle
,
1003 OUT EFI_FILE_INFO
*Buffer
,
1008 // pass to file handle lib
1010 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1013 Retrieve the size of a file.
1015 if FileHandle is NULL then ASSERT()
1016 if Size is NULL then ASSERT()
1018 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1021 @param FileHandle file handle from which size is retrieved
1022 @param Size pointer to size
1024 @retval EFI_SUCCESS operation was completed sucessfully
1025 @retval EFI_DEVICE_ERROR cannot access the file
1030 IN SHELL_FILE_HANDLE FileHandle
,
1034 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1037 Retrieves the status of the break execution flag
1039 this function is useful to check whether the application is being asked to halt by the shell.
1041 @retval TRUE the execution break is enabled
1042 @retval FALSE the execution break is not enabled
1046 ShellGetExecutionBreakFlag(
1051 // Check for UEFI Shell 2.0 protocols
1053 if (mEfiShellProtocol
!= NULL
) {
1056 // We are using UEFI Shell 2.0; see if the event has been triggered
1058 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1065 // using EFI Shell; call the function to check
1067 ASSERT(mEfiShellEnvironment2
!= NULL
);
1068 return (mEfiShellEnvironment2
->GetExecutionBreak());
1071 return the value of an environment variable
1073 this function gets the value of the environment variable set by the
1074 ShellSetEnvironmentVariable function
1076 @param EnvKey The key name of the environment variable.
1078 @retval NULL the named environment variable does not exist.
1079 @return != NULL pointer to the value of the environment variable
1083 ShellGetEnvironmentVariable (
1084 IN CONST CHAR16
*EnvKey
1088 // Check for UEFI Shell 2.0 protocols
1090 if (mEfiShellProtocol
!= NULL
) {
1091 return (mEfiShellProtocol
->GetEnv(EnvKey
));
1095 // ASSERT that we must have EFI shell
1097 ASSERT(mEfiShellEnvironment2
!= NULL
);
1102 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1105 set the value of an environment variable
1107 This function changes the current value of the specified environment variable. If the
1108 environment variable exists and the Value is an empty string, then the environment
1109 variable is deleted. If the environment variable exists and the Value is not an empty
1110 string, then the value of the environment variable is changed. If the environment
1111 variable does not exist and the Value is an empty string, there is no action. If the
1112 environment variable does not exist and the Value is a non-empty string, then the
1113 environment variable is created and assigned the specified value.
1115 This is not supported pre-UEFI Shell 2.0.
1117 @param EnvKey The key name of the environment variable.
1118 @param EnvVal The Value of the environment variable
1119 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1121 @retval EFI_SUCCESS the operation was completed sucessfully
1122 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1126 ShellSetEnvironmentVariable (
1127 IN CONST CHAR16
*EnvKey
,
1128 IN CONST CHAR16
*EnvVal
,
1133 // Check for UEFI Shell 2.0 protocols
1135 if (mEfiShellProtocol
!= NULL
) {
1136 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1140 // This feature does not exist under EFI shell
1142 return (EFI_UNSUPPORTED
);
1146 Cause the shell to parse and execute a command line.
1148 This function creates a nested instance of the shell and executes the specified
1149 command (CommandLine) with the specified environment (Environment). Upon return,
1150 the status code returned by the specified command is placed in StatusCode.
1151 If Environment is NULL, then the current environment is used and all changes made
1152 by the commands executed will be reflected in the current environment. If the
1153 Environment is non-NULL, then the changes made will be discarded.
1154 The CommandLine is executed from the current working directory on the current
1157 The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0
1158 environment. The values pointed to by the parameters will be unchanged by the
1159 ShellExecute() function. The Output parameter has no effect in a
1160 UEFI Shell 2.0 environment.
1162 @param[in] ParentHandle The parent image starting the operation.
1163 @param[in] CommandLine The pointer to a NULL terminated command line.
1164 @param[in] Output True to display debug output. False to hide it.
1165 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1166 in the form "x=y". If NULL, the current set is used.
1167 @param[out] Status The status of the run command line.
1169 @retval EFI_SUCCESS The operation completed sucessfully. Status
1170 contains the status code returned.
1171 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1172 @retval EFI_OUT_OF_RESOURCES Out of resources.
1173 @retval EFI_UNSUPPORTED The operation is not allowed.
1178 IN EFI_HANDLE
*ParentHandle
,
1179 IN CHAR16
*CommandLine OPTIONAL
,
1180 IN BOOLEAN Output OPTIONAL
,
1181 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1182 OUT EFI_STATUS
*Status OPTIONAL
1186 // Check for UEFI Shell 2.0 protocols
1188 if (mEfiShellProtocol
!= NULL
) {
1190 // Call UEFI Shell 2.0 version (not using Output parameter)
1192 return (mEfiShellProtocol
->Execute(ParentHandle
,
1194 EnvironmentVariables
,
1198 // ASSERT that we must have EFI shell
1200 ASSERT(mEfiShellEnvironment2
!= NULL
);
1202 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1203 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1205 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1210 Retreives the current directory path
1212 If the DeviceName is NULL, it returns the current device's current directory
1213 name. If the DeviceName is not NULL, it returns the current directory name
1216 @param DeviceName the name of the drive to get directory on
1218 @retval NULL the directory does not exist
1219 @return != NULL the directory
1223 ShellGetCurrentDir (
1224 IN CHAR16
* CONST DeviceName OPTIONAL
1228 // Check for UEFI Shell 2.0 protocols
1230 if (mEfiShellProtocol
!= NULL
) {
1231 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1234 // ASSERT that we must have EFI shell
1236 ASSERT(mEfiShellEnvironment2
!= NULL
);
1237 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1240 sets (enabled or disabled) the page break mode
1242 when page break mode is enabled the screen will stop scrolling
1243 and wait for operator input before scrolling a subsequent screen.
1245 @param CurrentState TRUE to enable and FALSE to disable
1249 ShellSetPageBreakMode (
1250 IN BOOLEAN CurrentState
1254 // check for enabling
1256 if (CurrentState
!= 0x00) {
1258 // check for UEFI Shell 2.0
1260 if (mEfiShellProtocol
!= NULL
) {
1262 // Enable with UEFI 2.0 Shell
1264 mEfiShellProtocol
->EnablePageBreak();
1268 // ASSERT that must have EFI Shell
1270 ASSERT(mEfiShellEnvironment2
!= NULL
);
1272 // Enable with EFI Shell
1274 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1279 // check for UEFI Shell 2.0
1281 if (mEfiShellProtocol
!= NULL
) {
1283 // Disable with UEFI 2.0 Shell
1285 mEfiShellProtocol
->DisablePageBreak();
1289 // ASSERT that must have EFI Shell
1291 ASSERT(mEfiShellEnvironment2
!= NULL
);
1293 // Disable with EFI Shell
1295 mEfiShellEnvironment2
->DisablePageBreak ();
1302 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1303 /// This allows for the struct to be populated.
1310 SHELL_FILE_HANDLE Handle
;
1311 EFI_FILE_INFO
*Info
;
1312 } EFI_SHELL_FILE_INFO_NO_CONST
;
1315 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1317 if OldStyleFileList is NULL then ASSERT()
1319 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1320 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1321 the ShellCloseFileMetaArg function.
1323 @param[in] FileList the EFI shell list type
1324 @param[in,out] ListHead the list to add to
1326 @retval the resultant head of the double linked new format list;
1330 InternalShellConvertFileListType (
1331 IN LIST_ENTRY
*FileList
,
1332 IN OUT LIST_ENTRY
*ListHead
1335 SHELL_FILE_ARG
*OldInfo
;
1337 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1342 ASSERT(FileList
!= NULL
);
1343 ASSERT(ListHead
!= NULL
);
1346 // enumerate through each member of the old list and copy
1348 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1349 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1350 ASSERT(OldInfo
!= NULL
);
1353 // Skip ones that failed to open...
1355 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1360 // make sure the old list was valid
1362 ASSERT(OldInfo
->Info
!= NULL
);
1363 ASSERT(OldInfo
->FullName
!= NULL
);
1364 ASSERT(OldInfo
->FileName
!= NULL
);
1367 // allocate a new EFI_SHELL_FILE_INFO object
1369 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1370 ASSERT(NewInfo
!= NULL
);
1371 if (NewInfo
== NULL
) {
1376 // copy the simple items
1378 NewInfo
->Handle
= OldInfo
->Handle
;
1379 NewInfo
->Status
= OldInfo
->Status
;
1381 // old shell checks for 0 not NULL
1382 OldInfo
->Handle
= 0;
1385 // allocate new space to copy strings and structure
1387 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1388 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1389 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1392 // make sure all the memory allocations were sucessful
1394 ASSERT(NewInfo
->FullName
!= NULL
);
1395 ASSERT(NewInfo
->FileName
!= NULL
);
1396 ASSERT(NewInfo
->Info
!= NULL
);
1399 // Copt the strings and structure
1401 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1402 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1403 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1406 // add that to the list
1408 InsertTailList(ListHead
, &NewInfo
->Link
);
1413 Opens a group of files based on a path.
1415 This function uses the Arg to open all the matching files. Each matched
1416 file has a SHELL_FILE_ARG structure to record the file information. These
1417 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1418 structures from ListHead to access each file. This function supports wildcards
1419 and will process '?' and '*' as such. the list must be freed with a call to
1420 ShellCloseFileMetaArg().
1422 If you are NOT appending to an existing list *ListHead must be NULL. If
1423 *ListHead is NULL then it must be callee freed.
1425 @param Arg pointer to path string
1426 @param OpenMode mode to open files with
1427 @param ListHead head of linked list of results
1429 @retval EFI_SUCCESS the operation was sucessful and the list head
1430 contains the list of opened files
1431 @return != EFI_SUCCESS the operation failed
1433 @sa InternalShellConvertFileListType
1437 ShellOpenFileMetaArg (
1440 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1444 LIST_ENTRY mOldStyleFileList
;
1447 // ASSERT that Arg and ListHead are not NULL
1449 ASSERT(Arg
!= NULL
);
1450 ASSERT(ListHead
!= NULL
);
1453 // Check for UEFI Shell 2.0 protocols
1455 if (mEfiShellProtocol
!= NULL
) {
1456 if (*ListHead
== NULL
) {
1457 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1458 if (*ListHead
== NULL
) {
1459 return (EFI_OUT_OF_RESOURCES
);
1461 InitializeListHead(&((*ListHead
)->Link
));
1463 Status
= mEfiShellProtocol
->OpenFileList(Arg
,
1466 if (EFI_ERROR(Status
)) {
1467 mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1469 Status
= mEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1471 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1472 FreePool(*ListHead
);
1474 return (EFI_NOT_FOUND
);
1480 // ASSERT that we must have EFI shell
1482 ASSERT(mEfiShellEnvironment2
!= NULL
);
1485 // make sure the list head is initialized
1487 InitializeListHead(&mOldStyleFileList
);
1490 // Get the EFI Shell list of files
1492 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1493 if (EFI_ERROR(Status
)) {
1498 if (*ListHead
== NULL
) {
1499 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1500 if (*ListHead
== NULL
) {
1501 return (EFI_OUT_OF_RESOURCES
);
1503 InitializeListHead(&((*ListHead
)->Link
));
1507 // Convert that to equivalent of UEFI Shell 2.0 structure
1509 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1512 // Free the EFI Shell version that was converted.
1514 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1516 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1517 FreePool(*ListHead
);
1519 Status
= EFI_NOT_FOUND
;
1525 Free the linked list returned from ShellOpenFileMetaArg.
1527 if ListHead is NULL then ASSERT().
1529 @param ListHead the pointer to free.
1531 @retval EFI_SUCCESS the operation was sucessful.
1535 ShellCloseFileMetaArg (
1536 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1542 // ASSERT that ListHead is not NULL
1544 ASSERT(ListHead
!= NULL
);
1547 // Check for UEFI Shell 2.0 protocols
1549 if (mEfiShellProtocol
!= NULL
) {
1550 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1553 // Since this is EFI Shell version we need to free our internally made copy
1556 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1557 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1558 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1559 RemoveEntryList(Node
);
1560 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1561 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1562 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1563 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1564 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1571 Find a file by searching the CWD and then the path.
1573 If FileName is NULL then ASSERT.
1575 If the return value is not NULL then the memory must be caller freed.
1577 @param FileName Filename string.
1579 @retval NULL the file was not found
1580 @return !NULL the full path to the file.
1585 IN CONST CHAR16
*FileName
1589 SHELL_FILE_HANDLE Handle
;
1593 CONST CHAR16
*Walker
;
1600 // First make sure its not an absolute path.
1602 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1603 if (!EFI_ERROR(Status
)){
1604 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1605 ASSERT(RetVal
== NULL
);
1606 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1607 ShellCloseFile(&Handle
);
1610 ShellCloseFile(&Handle
);
1614 Path
= ShellGetEnvironmentVariable(L
"cwd");
1616 Size
= StrSize(Path
);
1617 Size
+= StrSize(FileName
);
1618 TestPath
= AllocateZeroPool(Size
);
1619 ASSERT(TestPath
!= NULL
);
1620 if (TestPath
== NULL
) {
1623 StrCpy(TestPath
, Path
);
1624 StrCat(TestPath
, FileName
);
1625 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1626 if (!EFI_ERROR(Status
)){
1627 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1628 ASSERT(RetVal
== NULL
);
1629 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1630 ShellCloseFile(&Handle
);
1634 ShellCloseFile(&Handle
);
1639 Path
= ShellGetEnvironmentVariable(L
"path");
1641 Size
= StrSize(Path
)+sizeof(CHAR16
);
1642 Size
+= StrSize(FileName
);
1643 TestPath
= AllocateZeroPool(Size
);
1644 if (TestPath
== NULL
) {
1647 Walker
= (CHAR16
*)Path
;
1649 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1650 if (TestPath
!= NULL
) {
1651 TempChar
= StrStr(TestPath
, L
";");
1652 if (TempChar
!= NULL
) {
1653 *TempChar
= CHAR_NULL
;
1655 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1656 StrCat(TestPath
, L
"\\");
1658 StrCat(TestPath
, FileName
);
1659 if (StrStr(Walker
, L
";") != NULL
) {
1660 Walker
= StrStr(Walker
, L
";") + 1;
1664 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1665 if (!EFI_ERROR(Status
)){
1666 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1667 ASSERT(RetVal
== NULL
);
1668 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1669 ShellCloseFile(&Handle
);
1672 ShellCloseFile(&Handle
);
1676 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1683 Find a file by searching the CWD and then the path with a variable set of file
1684 extensions. If the file is not found it will append each extension in the list
1685 in the order provided and return the first one that is successful.
1687 If FileName is NULL, then ASSERT.
1688 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1690 If the return value is not NULL then the memory must be caller freed.
1692 @param[in] FileName Filename string.
1693 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1695 @retval NULL The file was not found.
1696 @retval !NULL The path to the file.
1700 ShellFindFilePathEx (
1701 IN CONST CHAR16
*FileName
,
1702 IN CONST CHAR16
*FileExtension
1707 CONST CHAR16
*ExtensionWalker
;
1712 ASSERT(FileName
!= NULL
);
1713 if (FileExtension
== NULL
) {
1714 return (ShellFindFilePath(FileName
));
1716 RetVal
= ShellFindFilePath(FileName
);
1717 if (RetVal
!= NULL
) {
1720 Size
= StrSize(FileName
);
1721 Size
+= StrSize(FileExtension
);
1722 TestPath
= AllocateZeroPool(Size
);
1723 ASSERT(TestPath
!= NULL
);
1724 if (TestPath
== NULL
) {
1727 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1728 StrCpy(TestPath
, FileName
);
1729 if (ExtensionWalker
!= NULL
) {
1730 StrCat(TestPath
, ExtensionWalker
);
1732 TempChar
= StrStr(TestPath
, L
";");
1733 if (TempChar
!= NULL
) {
1734 *TempChar
= CHAR_NULL
;
1736 RetVal
= ShellFindFilePath(TestPath
);
1737 if (RetVal
!= NULL
) {
1740 ASSERT(ExtensionWalker
!= NULL
);
1741 TempChar2
= StrStr(ExtensionWalker
, L
";");
1750 SHELL_PARAM_TYPE Type
;
1752 UINTN OriginalPosition
;
1753 } SHELL_PARAM_PACKAGE
;
1756 Checks the list of valid arguments and returns TRUE if the item was found. If the
1757 return value is TRUE then the type parameter is set also.
1759 if CheckList is NULL then ASSERT();
1760 if Name is NULL then ASSERT();
1761 if Type is NULL then ASSERT();
1763 @param Name pointer to Name of parameter found
1764 @param CheckList List to check against
1765 @param Type pointer to type of parameter if it was found
1767 @retval TRUE the Parameter was found. Type is valid.
1768 @retval FALSE the Parameter was not found. Type is not valid.
1772 InternalIsOnCheckList (
1773 IN CONST CHAR16
*Name
,
1774 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1775 OUT SHELL_PARAM_TYPE
*Type
1778 SHELL_PARAM_ITEM
*TempListItem
;
1782 // ASSERT that all 3 pointer parameters aren't NULL
1784 ASSERT(CheckList
!= NULL
);
1785 ASSERT(Type
!= NULL
);
1786 ASSERT(Name
!= NULL
);
1789 // question mark and page break mode are always supported
1791 if ((StrCmp(Name
, L
"-?") == 0) ||
1792 (StrCmp(Name
, L
"-b") == 0)
1799 // Enumerate through the list
1801 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1803 // If the Type is TypeStart only check the first characters of the passed in param
1804 // If it matches set the type and return TRUE
1806 if (TempListItem
->Type
== TypeStart
) {
1807 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1808 *Type
= TempListItem
->Type
;
1812 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1813 if (TempString
!= NULL
) {
1814 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1815 *Type
= TempListItem
->Type
;
1816 FreePool(TempString
);
1819 FreePool(TempString
);
1821 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1822 *Type
= TempListItem
->Type
;
1830 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1832 @param[in] Name pointer to Name of parameter found
1833 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1835 @retval TRUE the Parameter is a flag.
1836 @retval FALSE the Parameter not a flag.
1841 IN CONST CHAR16
*Name
,
1842 IN BOOLEAN AlwaysAllowNumbers
1846 // ASSERT that Name isn't NULL
1848 ASSERT(Name
!= NULL
);
1851 // If we accept numbers then dont return TRUE. (they will be values)
1853 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
) {
1858 // If the Name has a /, +, or - as the first character return TRUE
1860 if ((Name
[0] == L
'/') ||
1861 (Name
[0] == L
'-') ||
1870 Checks the command line arguments passed against the list of valid ones.
1872 If no initialization is required, then return RETURN_SUCCESS.
1874 @param[in] CheckList pointer to list of parameters to check
1875 @param[out] CheckPackage pointer to pointer to list checked values
1876 @param[out] ProblemParam optional pointer to pointer to unicode string for
1877 the paramater that caused failure. If used then the
1878 caller is responsible for freeing the memory.
1879 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1880 @param[in] Argv pointer to array of parameters
1881 @param[in] Argc Count of parameters in Argv
1882 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
1884 @retval EFI_SUCCESS The operation completed sucessfully.
1885 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1886 @retval EFI_INVALID_PARAMETER A parameter was invalid
1887 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1888 duplicated. the duplicated command line argument
1889 was returned in ProblemParam if provided.
1890 @retval EFI_NOT_FOUND a argument required a value that was missing.
1891 the invalid command line argument was returned in
1892 ProblemParam if provided.
1896 InternalCommandLineParse (
1897 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1898 OUT LIST_ENTRY
**CheckPackage
,
1899 OUT CHAR16
**ProblemParam OPTIONAL
,
1900 IN BOOLEAN AutoPageBreak
,
1901 IN CONST CHAR16
**Argv
,
1903 IN BOOLEAN AlwaysAllowNumbers
1907 SHELL_PARAM_TYPE CurrentItemType
;
1908 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1912 CONST CHAR16
*TempPointer
;
1914 CurrentItemPackage
= NULL
;
1920 // If there is only 1 item we dont need to do anything
1923 *CheckPackage
= NULL
;
1924 return (EFI_SUCCESS
);
1930 ASSERT(CheckList
!= NULL
);
1931 ASSERT(Argv
!= NULL
);
1934 // initialize the linked list
1936 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1937 InitializeListHead(*CheckPackage
);
1940 // loop through each of the arguments
1942 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1943 if (Argv
[LoopCounter
] == NULL
) {
1945 // do nothing for NULL argv
1947 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
1949 // We might have leftover if last parameter didnt have optional value
1951 if (GetItemValue
!= 0) {
1953 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1958 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
1959 ASSERT(CurrentItemPackage
!= NULL
);
1960 CurrentItemPackage
->Name
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1961 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1962 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1963 CurrentItemPackage
->Type
= CurrentItemType
;
1964 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1965 CurrentItemPackage
->Value
= NULL
;
1968 // Does this flag require a value
1970 switch (CurrentItemPackage
->Type
) {
1972 // possibly trigger the next loop(s) to populate the value of this item
1978 case TypeDoubleValue
:
1983 GetItemValue
= (UINTN
)(-1);
1988 // this item has no value expected; we are done
1990 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1991 ASSERT(GetItemValue
== 0);
1994 } else if (GetItemValue
!= 0 && !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
)) {
1995 ASSERT(CurrentItemPackage
!= NULL
);
1997 // get the item VALUE for a previous flag
1999 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
2000 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2001 if (ValueSize
== 0) {
2002 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2004 StrCat(CurrentItemPackage
->Value
, L
" ");
2005 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2007 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2009 if (GetItemValue
== 0) {
2010 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2012 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) ){ //|| ProblemParam == NULL) {
2014 // add this one as a non-flag
2017 TempPointer
= Argv
[LoopCounter
];
2018 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2019 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2020 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2024 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2025 ASSERT(CurrentItemPackage
!= NULL
);
2026 CurrentItemPackage
->Name
= NULL
;
2027 CurrentItemPackage
->Type
= TypePosition
;
2028 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(TempPointer
));
2029 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2030 StrCpy(CurrentItemPackage
->Value
, TempPointer
);
2031 CurrentItemPackage
->OriginalPosition
= Count
++;
2032 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2035 // this was a non-recognised flag... error!
2037 if (ProblemParam
!= NULL
) {
2038 *ProblemParam
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
2039 ASSERT(*ProblemParam
!= NULL
);
2040 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
2042 ShellCommandLineFreeVarList(*CheckPackage
);
2043 *CheckPackage
= NULL
;
2044 return (EFI_VOLUME_CORRUPTED
);
2047 if (GetItemValue
!= 0) {
2049 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2052 // support for AutoPageBreak
2054 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2055 ShellSetPageBreakMode(TRUE
);
2057 return (EFI_SUCCESS
);
2061 Checks the command line arguments passed against the list of valid ones.
2062 Optionally removes NULL values first.
2064 If no initialization is required, then return RETURN_SUCCESS.
2066 @param[in] CheckList The pointer to list of parameters to check.
2067 @param[out] CheckPackage The package of checked values.
2068 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2069 the paramater that caused failure.
2070 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2071 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2073 @retval EFI_SUCCESS The operation completed sucessfully.
2074 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2075 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2076 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2077 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2078 of the command line arguments was returned in
2079 ProblemParam if provided.
2080 @retval EFI_NOT_FOUND A argument required a value that was missing.
2081 The invalid command line argument was returned in
2082 ProblemParam if provided.
2086 ShellCommandLineParseEx (
2087 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2088 OUT LIST_ENTRY
**CheckPackage
,
2089 OUT CHAR16
**ProblemParam OPTIONAL
,
2090 IN BOOLEAN AutoPageBreak
,
2091 IN BOOLEAN AlwaysAllowNumbers
2095 // ASSERT that CheckList and CheckPackage aren't NULL
2097 ASSERT(CheckList
!= NULL
);
2098 ASSERT(CheckPackage
!= NULL
);
2101 // Check for UEFI Shell 2.0 protocols
2103 if (mEfiShellParametersProtocol
!= NULL
) {
2104 return (InternalCommandLineParse(CheckList
,
2108 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
2109 mEfiShellParametersProtocol
->Argc
,
2110 AlwaysAllowNumbers
));
2114 // ASSERT That EFI Shell is not required
2116 ASSERT (mEfiShellInterface
!= NULL
);
2117 return (InternalCommandLineParse(CheckList
,
2121 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2122 mEfiShellInterface
->Argc
,
2123 AlwaysAllowNumbers
));
2127 Frees shell variable list that was returned from ShellCommandLineParse.
2129 This function will free all the memory that was used for the CheckPackage
2130 list of postprocessed shell arguments.
2132 this function has no return value.
2134 if CheckPackage is NULL, then return
2136 @param CheckPackage the list to de-allocate
2140 ShellCommandLineFreeVarList (
2141 IN LIST_ENTRY
*CheckPackage
2147 // check for CheckPackage == NULL
2149 if (CheckPackage
== NULL
) {
2154 // for each node in the list
2156 for ( Node
= GetFirstNode(CheckPackage
)
2157 ; !IsListEmpty(CheckPackage
)
2158 ; Node
= GetFirstNode(CheckPackage
)
2161 // Remove it from the list
2163 RemoveEntryList(Node
);
2166 // if it has a name free the name
2168 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2169 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2173 // if it has a value free the value
2175 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2176 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2180 // free the node structure
2182 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2185 // free the list head node
2187 FreePool(CheckPackage
);
2190 Checks for presence of a flag parameter
2192 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2194 if CheckPackage is NULL then return FALSE.
2195 if KeyString is NULL then ASSERT()
2197 @param CheckPackage The package of parsed command line arguments
2198 @param KeyString the Key of the command line argument to check for
2200 @retval TRUE the flag is on the command line
2201 @retval FALSE the flag is not on the command line
2205 ShellCommandLineGetFlag (
2206 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2207 IN CONST CHAR16
* CONST KeyString
2214 // ASSERT that both CheckPackage and KeyString aren't NULL
2216 ASSERT(KeyString
!= NULL
);
2219 // return FALSE for no package
2221 if (CheckPackage
== NULL
) {
2226 // enumerate through the list of parametrs
2228 for ( Node
= GetFirstNode(CheckPackage
)
2229 ; !IsNull (CheckPackage
, Node
)
2230 ; Node
= GetNextNode(CheckPackage
, Node
)
2233 // If the Name matches, return TRUE (and there may be NULL name)
2235 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2237 // If Type is TypeStart then only compare the begining of the strings
2239 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2240 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2244 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2245 if (TempString
!= NULL
) {
2246 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2247 FreePool(TempString
);
2250 FreePool(TempString
);
2252 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2260 Returns value from command line argument.
2262 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2264 If CheckPackage is NULL, then return NULL.
2266 @param[in] CheckPackage The package of parsed command line arguments.
2267 @param[in] KeyString The Key of the command line argument to check for.
2269 @retval NULL The flag is not on the command line.
2270 @retval !=NULL The pointer to unicode string of the value.
2274 ShellCommandLineGetValue (
2275 IN CONST LIST_ENTRY
*CheckPackage
,
2276 IN CHAR16
*KeyString
2283 // check for CheckPackage == NULL
2285 if (CheckPackage
== NULL
) {
2290 // enumerate through the list of parametrs
2292 for ( Node
= GetFirstNode(CheckPackage
)
2293 ; !IsNull (CheckPackage
, Node
)
2294 ; Node
= GetNextNode(CheckPackage
, Node
)
2297 // If the Name matches, return TRUE (and there may be NULL name)
2299 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2301 // If Type is TypeStart then only compare the begining of the strings
2303 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2304 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2305 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2308 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2309 if (TempString
!= NULL
) {
2310 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2311 FreePool(TempString
);
2312 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2314 FreePool(TempString
);
2316 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2317 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2325 Returns raw value from command line argument.
2327 Raw value parameters are in the form of "value" in a specific position in the list.
2329 If CheckPackage is NULL, then return NULL.
2331 @param[in] CheckPackage The package of parsed command line arguments.
2332 @param[in] Position The position of the value.
2334 @retval NULL The flag is not on the command line.
2335 @retval !=NULL The pointer to unicode string of the value.
2339 ShellCommandLineGetRawValue (
2340 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2347 // check for CheckPackage == NULL
2349 if (CheckPackage
== NULL
) {
2354 // enumerate through the list of parametrs
2356 for ( Node
= GetFirstNode(CheckPackage
)
2357 ; !IsNull (CheckPackage
, Node
)
2358 ; Node
= GetNextNode(CheckPackage
, Node
)
2361 // If the position matches, return the value
2363 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2364 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2371 returns the number of command line value parameters that were parsed.
2373 this will not include flags.
2375 @param[in] CheckPackage The package of parsed command line arguments.
2377 @retval (UINTN)-1 No parsing has ocurred
2378 @return other The number of value parameters found
2382 ShellCommandLineGetCount(
2383 IN CONST LIST_ENTRY
*CheckPackage
2389 if (CheckPackage
== NULL
) {
2392 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2393 ; !IsNull (CheckPackage
, Node1
)
2394 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2396 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2404 Determins if a parameter is duplicated.
2406 If Param is not NULL then it will point to a callee allocated string buffer
2407 with the parameter value if a duplicate is found.
2409 If CheckPackage is NULL, then ASSERT.
2411 @param[in] CheckPackage The package of parsed command line arguments.
2412 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2414 @retval EFI_SUCCESS No parameters were duplicated.
2415 @retval EFI_DEVICE_ERROR A duplicate was found.
2419 ShellCommandLineCheckDuplicate (
2420 IN CONST LIST_ENTRY
*CheckPackage
,
2427 ASSERT(CheckPackage
!= NULL
);
2429 for ( Node1
= GetFirstNode(CheckPackage
)
2430 ; !IsNull (CheckPackage
, Node1
)
2431 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2433 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2434 ; !IsNull (CheckPackage
, Node2
)
2435 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2437 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) {
2438 if (Param
!= NULL
) {
2440 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2442 return (EFI_DEVICE_ERROR
);
2446 return (EFI_SUCCESS
);
2450 This is a find and replace function. Upon successful return the NewString is a copy of
2451 SourceString with each instance of FindTarget replaced with ReplaceWith.
2453 If SourceString and NewString overlap the behavior is undefined.
2455 If the string would grow bigger than NewSize it will halt and return error.
2457 @param[in] SourceString The string with source buffer.
2458 @param[in,out] NewString The string with resultant buffer.
2459 @param[in] NewSize The size in bytes of NewString.
2460 @param[in] FindTarget The string to look for.
2461 @param[in] ReplaceWith The string to replace FindTarget with.
2462 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2463 immediately before it.
2464 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2466 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2467 @retval EFI_INVALID_PARAMETER NewString was NULL.
2468 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2469 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2470 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2471 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2472 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2473 the new string (truncation occurred).
2474 @retval EFI_SUCCESS The string was successfully copied with replacement.
2478 ShellCopySearchAndReplace(
2479 IN CHAR16 CONST
*SourceString
,
2480 IN OUT CHAR16
*NewString
,
2482 IN CONST CHAR16
*FindTarget
,
2483 IN CONST CHAR16
*ReplaceWith
,
2484 IN CONST BOOLEAN SkipPreCarrot
,
2485 IN CONST BOOLEAN ParameterReplacing
2491 if ( (SourceString
== NULL
)
2492 || (NewString
== NULL
)
2493 || (FindTarget
== NULL
)
2494 || (ReplaceWith
== NULL
)
2495 || (StrLen(FindTarget
) < 1)
2496 || (StrLen(SourceString
) < 1)
2498 return (EFI_INVALID_PARAMETER
);
2501 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2502 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2504 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2505 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2507 if (Replace
== NULL
) {
2508 return (EFI_OUT_OF_RESOURCES
);
2510 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2511 while (*SourceString
!= CHAR_NULL
) {
2513 // if we find the FindTarget and either Skip == FALSE or Skip and we
2514 // dont have a carrot do a replace...
2516 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2517 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2519 SourceString
+= StrLen(FindTarget
);
2520 Size
= StrSize(NewString
);
2521 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2523 return (EFI_BUFFER_TOO_SMALL
);
2525 StrCat(NewString
, Replace
);
2527 Size
= StrSize(NewString
);
2528 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2530 return (EFI_BUFFER_TOO_SMALL
);
2532 StrnCat(NewString
, SourceString
, 1);
2537 return (EFI_SUCCESS
);
2541 Internal worker function to output a string.
2543 This function will output a string to the correct StdOut.
2545 @param[in] String The string to print out.
2547 @retval EFI_SUCCESS The operation was sucessful.
2548 @retval !EFI_SUCCESS The operation failed.
2553 IN CONST CHAR16
*String
2557 Size
= StrSize(String
) - sizeof(CHAR16
);
2559 return (EFI_SUCCESS
);
2561 if (mEfiShellParametersProtocol
!= NULL
) {
2562 return (mEfiShellProtocol
->WriteFile(mEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2564 if (mEfiShellInterface
!= NULL
) {
2566 // Divide in half for old shell. Must be string length not size.
2569 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2572 return (EFI_UNSUPPORTED
);
2576 Print at a specific location on the screen.
2578 This function will move the cursor to a given screen location and print the specified string
2580 If -1 is specified for either the Row or Col the current screen location for BOTH
2583 if either Row or Col is out of range for the current console, then ASSERT
2584 if Format is NULL, then ASSERT
2586 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2587 the following additional flags:
2588 %N - Set output attribute to normal
2589 %H - Set output attribute to highlight
2590 %E - Set output attribute to error
2591 %B - Set output attribute to blue color
2592 %V - Set output attribute to green color
2594 Note: The background color is controlled by the shell command cls.
2596 @param[in] Col the column to print at
2597 @param[in] Row the row to print at
2598 @param[in] Format the format string
2599 @param[in] Marker the marker for the variable argument list
2601 @return EFI_SUCCESS The operation was successful.
2602 @return EFI_DEVICE_ERROR The console device reported an error.
2606 InternalShellPrintWorker(
2607 IN INT32 Col OPTIONAL
,
2608 IN INT32 Row OPTIONAL
,
2609 IN CONST CHAR16
*Format
,
2614 CHAR16
*ResumeLocation
;
2615 CHAR16
*FormatWalker
;
2616 UINTN OriginalAttribute
;
2618 Status
= EFI_SUCCESS
;
2619 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2622 // Back and forth each time fixing up 1 of our flags...
2624 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2625 ASSERT_EFI_ERROR(Status
);
2626 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2627 ASSERT_EFI_ERROR(Status
);
2628 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2629 ASSERT_EFI_ERROR(Status
);
2630 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2631 ASSERT_EFI_ERROR(Status
);
2632 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2633 ASSERT_EFI_ERROR(Status
);
2636 // Use the last buffer from replacing to print from...
2638 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2640 if (Col
!= -1 && Row
!= -1) {
2641 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2644 FormatWalker
= mPostReplaceFormat2
;
2645 while (*FormatWalker
!= CHAR_NULL
) {
2647 // Find the next attribute change request
2649 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2650 if (ResumeLocation
!= NULL
) {
2651 *ResumeLocation
= CHAR_NULL
;
2654 // print the current FormatWalker string
2656 if (StrLen(FormatWalker
)>0) {
2657 Status
= InternalPrintTo(FormatWalker
);
2658 if (EFI_ERROR(Status
)) {
2664 // update the attribute
2666 if (ResumeLocation
!= NULL
) {
2667 switch (*(ResumeLocation
+1)) {
2669 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2672 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2675 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2678 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2681 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2685 // Print a simple '%' symbol
2687 Status
= InternalPrintTo(L
"%");
2688 if (EFI_ERROR(Status
)) {
2691 ResumeLocation
= ResumeLocation
- 1;
2696 // reset to normal now...
2702 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2704 FormatWalker
= ResumeLocation
+ 2;
2707 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
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] Format the format string
2735 @param[in] ... The variable argument list.
2737 @return EFI_SUCCESS The printing was successful.
2738 @return EFI_DEVICE_ERROR The console device reported an error.
2743 IN INT32 Col OPTIONAL
,
2744 IN INT32 Row OPTIONAL
,
2745 IN CONST CHAR16
*Format
,
2751 if (Format
== NULL
) {
2752 return (EFI_INVALID_PARAMETER
);
2754 VA_START (Marker
, Format
);
2755 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2761 Print at a specific location on the screen.
2763 This function will move the cursor to a given screen location and print the specified string.
2765 If -1 is specified for either the Row or Col the current screen location for BOTH
2768 If either Row or Col is out of range for the current console, then ASSERT.
2769 If Format is NULL, then ASSERT.
2771 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2772 the following additional flags:
2773 %N - Set output attribute to normal.
2774 %H - Set output attribute to highlight.
2775 %E - Set output attribute to error.
2776 %B - Set output attribute to blue color.
2777 %V - Set output attribute to green color.
2779 Note: The background color is controlled by the shell command cls.
2781 @param[in] Col The column to print at.
2782 @param[in] Row The row to print at.
2783 @param[in] Language The language of the string to retrieve. If this parameter
2784 is NULL, then the current platform language is used.
2785 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2786 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2787 @param[in] ... The variable argument list.
2789 @return EFI_SUCCESS The printing was successful.
2790 @return EFI_DEVICE_ERROR The console device reported an error.
2795 IN INT32 Col OPTIONAL
,
2796 IN INT32 Row OPTIONAL
,
2797 IN CONST CHAR8
*Language OPTIONAL
,
2798 IN CONST EFI_STRING_ID HiiFormatStringId
,
2799 IN CONST EFI_HANDLE HiiFormatHandle
,
2804 CHAR16
*HiiFormatString
;
2807 VA_START (Marker
, HiiFormatHandle
);
2808 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2809 ASSERT(HiiFormatString
!= NULL
);
2811 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2813 SHELL_FREE_NON_NULL(HiiFormatString
);
2820 Function to determine if a given filename represents a file or a directory.
2822 @param[in] DirName Path to directory to test.
2824 @retval EFI_SUCCESS The Path represents a directory
2825 @retval EFI_NOT_FOUND The Path does not represent a directory
2826 @return other The path failed to open
2831 IN CONST CHAR16
*DirName
2835 SHELL_FILE_HANDLE Handle
;
2836 CHAR16
*TempLocation
;
2837 CHAR16
*TempLocation2
;
2839 ASSERT(DirName
!= NULL
);
2842 TempLocation
= NULL
;
2844 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2845 if (EFI_ERROR(Status
)) {
2847 // try good logic first.
2849 if (mEfiShellProtocol
!= NULL
) {
2850 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
2851 TempLocation2
= StrStr(TempLocation
, L
":");
2852 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
2853 *(TempLocation2
+1) = CHAR_NULL
;
2855 if (mEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
2856 FreePool(TempLocation
);
2857 return (EFI_SUCCESS
);
2859 FreePool(TempLocation
);
2862 // probably a map name?!?!!?
2864 TempLocation
= StrStr(DirName
, L
"\\");
2865 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
2866 return (EFI_SUCCESS
);
2872 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2873 ShellCloseFile(&Handle
);
2874 return (EFI_SUCCESS
);
2876 ShellCloseFile(&Handle
);
2877 return (EFI_NOT_FOUND
);
2881 Function to determine if a given filename represents a file.
2883 @param[in] Name Path to file to test.
2885 @retval EFI_SUCCESS The Path represents a file.
2886 @retval EFI_NOT_FOUND The Path does not represent a file.
2887 @retval other The path failed to open.
2892 IN CONST CHAR16
*Name
2896 SHELL_FILE_HANDLE Handle
;
2898 ASSERT(Name
!= NULL
);
2902 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
2903 if (EFI_ERROR(Status
)) {
2907 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
2908 ShellCloseFile(&Handle
);
2909 return (EFI_SUCCESS
);
2911 ShellCloseFile(&Handle
);
2912 return (EFI_NOT_FOUND
);
2916 Function to determine if a given filename represents a file.
2918 This will search the CWD and then the Path.
2920 If Name is NULL, then ASSERT.
2922 @param[in] Name Path to file to test.
2924 @retval EFI_SUCCESS The Path represents a file.
2925 @retval EFI_NOT_FOUND The Path does not represent a file.
2926 @retval other The path failed to open.
2931 IN CONST CHAR16
*Name
2937 if (!EFI_ERROR(ShellIsFile(Name
))) {
2938 return (EFI_SUCCESS
);
2941 NewName
= ShellFindFilePath(Name
);
2942 if (NewName
== NULL
) {
2943 return (EFI_NOT_FOUND
);
2945 Status
= ShellIsFile(NewName
);
2951 Function to determine whether a string is decimal or hex representation of a number
2952 and return the number converted from the string.
2954 @param[in] String String representation of a number
2957 @retval (UINTN)(-1) An error ocurred.
2962 IN CONST CHAR16
*String
2970 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
)) {
2974 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
2975 return ((UINTN
)RetVal
);
2977 return ((UINTN
)(-1));
2981 Safely append with automatic string resizing given length of Destination and
2982 desired length of copy from Source.
2984 append the first D characters of Source to the end of Destination, where D is
2985 the lesser of Count and the StrLen() of Source. If appending those D characters
2986 will fit within Destination (whose Size is given as CurrentSize) and
2987 still leave room for a NULL terminator, then those characters are appended,
2988 starting at the original terminating NULL of Destination, and a new terminating
2991 If appending D characters onto Destination will result in a overflow of the size
2992 given in CurrentSize the string will be grown such that the copy can be performed
2993 and CurrentSize will be updated to the new size.
2995 If Source is NULL, there is nothing to append, just return the current buffer in
2998 if Destination is NULL, then ASSERT()
2999 if Destination's current length (including NULL terminator) is already more then
3000 CurrentSize, then ASSERT()
3002 @param[in,out] Destination The String to append onto
3003 @param[in,out] CurrentSize on call the number of bytes in Destination. On
3004 return possibly the new size (still in bytes). if NULL
3005 then allocate whatever is needed.
3006 @param[in] Source The String to append from
3007 @param[in] Count Maximum number of characters to append. if 0 then
3010 @return Destination return the resultant string.
3015 IN OUT CHAR16
**Destination
,
3016 IN OUT UINTN
*CurrentSize
,
3017 IN CONST CHAR16
*Source
,
3021 UINTN DestinationStartSize
;
3027 ASSERT(Destination
!= NULL
);
3030 // If there's nothing to do then just return Destination
3032 if (Source
== NULL
) {
3033 return (*Destination
);
3037 // allow for un-initialized pointers, based on size being 0
3039 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3040 *Destination
= NULL
;
3044 // allow for NULL pointers address as Destination
3046 if (*Destination
!= NULL
) {
3047 ASSERT(CurrentSize
!= 0);
3048 DestinationStartSize
= StrSize(*Destination
);
3049 ASSERT(DestinationStartSize
<= *CurrentSize
);
3051 DestinationStartSize
= 0;
3052 // ASSERT(*CurrentSize == 0);
3056 // Append all of Source?
3059 Count
= StrLen(Source
);
3063 // Test and grow if required
3065 if (CurrentSize
!= NULL
) {
3066 NewSize
= *CurrentSize
;
3067 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3068 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3070 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3071 ASSERT(*Destination
!= NULL
);
3072 *CurrentSize
= NewSize
;
3074 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
3075 ASSERT(*Destination
!= NULL
);
3079 // Now use standard StrnCat on a big enough buffer
3081 if (*Destination
== NULL
) {
3084 return StrnCat(*Destination
, Source
, Count
);
3088 Prompt the user and return the resultant answer to the requestor.
3090 This function will display the requested question on the shell prompt and then
3091 wait for an apropriate answer to be input from the console.
3093 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3094 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3096 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3099 In either case *Response must be callee freed if Response was not NULL;
3101 @param Type What type of question is asked. This is used to filter the input
3102 to prevent invalid answers to question.
3103 @param Prompt Pointer to string prompt to use to request input.
3104 @param Response Pointer to Response which will be populated upon return.
3106 @retval EFI_SUCCESS The operation was sucessful.
3107 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3108 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3109 @return other The operation failed.
3113 ShellPromptForResponse (
3114 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3115 IN CHAR16
*Prompt OPTIONAL
,
3116 IN OUT VOID
**Response OPTIONAL
3122 SHELL_PROMPT_RESPONSE
*Resp
;
3126 Status
= EFI_UNSUPPORTED
;
3130 if (Type
!= ShellPromptResponseTypeFreeform
) {
3131 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3133 return (EFI_OUT_OF_RESOURCES
);
3138 case ShellPromptResponseTypeQuitContinue
:
3139 if (Prompt
!= NULL
) {
3140 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3143 // wait for valid response
3145 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3146 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3147 ASSERT_EFI_ERROR(Status
);
3148 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3149 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3150 *Resp
= ShellPromptResponseQuit
;
3152 *Resp
= ShellPromptResponseContinue
;
3155 case ShellPromptResponseTypeYesNoCancel
:
3156 if (Prompt
!= NULL
) {
3157 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3160 // wait for valid response
3162 *Resp
= ShellPromptResponseMax
;
3163 while (*Resp
== ShellPromptResponseMax
) {
3164 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3165 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3166 ASSERT_EFI_ERROR(Status
);
3167 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3168 switch (Key
.UnicodeChar
) {
3171 *Resp
= ShellPromptResponseYes
;
3175 *Resp
= ShellPromptResponseNo
;
3179 *Resp
= ShellPromptResponseCancel
;
3183 break; case ShellPromptResponseTypeYesNoAllCancel
:
3184 if (Prompt
!= NULL
) {
3185 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3188 // wait for valid response
3190 *Resp
= ShellPromptResponseMax
;
3191 while (*Resp
== ShellPromptResponseMax
) {
3192 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3193 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3194 ASSERT_EFI_ERROR(Status
);
3195 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3196 switch (Key
.UnicodeChar
) {
3199 *Resp
= ShellPromptResponseYes
;
3203 *Resp
= ShellPromptResponseNo
;
3207 *Resp
= ShellPromptResponseAll
;
3211 *Resp
= ShellPromptResponseCancel
;
3216 case ShellPromptResponseTypeEnterContinue
:
3217 case ShellPromptResponseTypeAnyKeyContinue
:
3218 if (Prompt
!= NULL
) {
3219 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3222 // wait for valid response
3224 *Resp
= ShellPromptResponseMax
;
3225 while (*Resp
== ShellPromptResponseMax
) {
3226 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3227 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3228 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3229 ASSERT_EFI_ERROR(Status
);
3230 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3231 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3232 *Resp
= ShellPromptResponseContinue
;
3236 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3237 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3238 ASSERT_EFI_ERROR(Status
);
3239 *Resp
= ShellPromptResponseContinue
;
3244 case ShellPromptResponseTypeYesNo
:
3245 if (Prompt
!= NULL
) {
3246 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3249 // wait for valid response
3251 *Resp
= ShellPromptResponseMax
;
3252 while (*Resp
== ShellPromptResponseMax
) {
3253 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3254 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3255 ASSERT_EFI_ERROR(Status
);
3256 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3257 switch (Key
.UnicodeChar
) {
3260 *Resp
= ShellPromptResponseYes
;
3264 *Resp
= ShellPromptResponseNo
;
3269 case ShellPromptResponseTypeFreeform
:
3270 if (Prompt
!= NULL
) {
3271 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3274 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3275 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3276 ASSERT_EFI_ERROR(Status
);
3277 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3278 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3281 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3282 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3286 // This is the location to add new prompt types.
3292 if (Response
!= NULL
) {
3295 } else if (Buffer
!= NULL
) {
3302 if (Buffer
!= NULL
) {
3307 ShellPrintEx(-1, -1, L
"\r\n");
3312 Prompt the user and return the resultant answer to the requestor.
3314 This function is the same as ShellPromptForResponse, except that the prompt is
3315 automatically pulled from HII.
3317 @param Type What type of question is asked. This is used to filter the input
3318 to prevent invalid answers to question.
3319 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3320 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3321 @param Response Pointer to Response which will be populated upon return.
3323 @retval EFI_SUCCESS the operation was sucessful.
3324 @return other the operation failed.
3326 @sa ShellPromptForResponse
3330 ShellPromptForResponseHii (
3331 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3332 IN CONST EFI_STRING_ID HiiFormatStringId
,
3333 IN CONST EFI_HANDLE HiiFormatHandle
,
3334 IN OUT VOID
**Response
3340 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3341 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3347 Function to determin if an entire string is a valid number.
3349 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3351 @param[in] String The string to evaluate.
3352 @param[in] ForceHex TRUE - always assume hex.
3353 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3355 @retval TRUE It is all numeric (dec/hex) characters.
3356 @retval FALSE There is a non-numeric character.
3360 InternalShellIsHexOrDecimalNumber (
3361 IN CONST CHAR16
*String
,
3362 IN CONST BOOLEAN ForceHex
,
3363 IN CONST BOOLEAN StopAtSpace
3369 // chop off a single negative sign
3371 if (String
!= NULL
&& *String
== L
'-') {
3375 if (String
== NULL
) {
3380 // chop leading zeroes
3382 while(String
!= NULL
&& *String
== L
'0'){
3386 // allow '0x' or '0X', but not 'x' or 'X'
3388 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3389 if (*(String
-1) != L
'0') {
3391 // we got an x without a preceeding 0
3397 } else if (ForceHex
) {
3404 // loop through the remaining characters and use the lib function
3406 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3408 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3412 if (!ShellIsDecimalDigitCharacter(*String
)) {
3422 Function to determine if a given filename exists.
3424 @param[in] Name Path to test.
3426 @retval EFI_SUCCESS The Path represents a file.
3427 @retval EFI_NOT_FOUND The Path does not represent a file.
3428 @retval other The path failed to open.
3433 IN CONST CHAR16
*Name
3437 EFI_SHELL_FILE_INFO
*List
;
3439 ASSERT(Name
!= NULL
);
3442 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3443 if (EFI_ERROR(Status
)) {
3447 ShellCloseFileMetaArg(&List
);
3449 return (EFI_SUCCESS
);
3453 Convert a Unicode character to upper case only if
3454 it maps to a valid small-case ASCII character.
3456 This internal function only deal with Unicode character
3457 which maps to a valid small-case ASCII character, i.e.
3458 L'a' to L'z'. For other Unicode character, the input character
3459 is returned directly.
3461 @param Char The character to convert.
3463 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3464 @retval Unchanged Otherwise.
3469 InternalShellCharToUpper (
3473 if (Char
>= L
'a' && Char
<= L
'z') {
3474 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3481 Convert a Unicode character to numerical value.
3483 This internal function only deal with Unicode character
3484 which maps to a valid hexadecimal ASII character, i.e.
3485 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3486 Unicode character, the value returned does not make sense.
3488 @param Char The character to convert.
3490 @return The numerical value converted.
3495 InternalShellHexCharToUintn (
3499 if (ShellIsDecimalDigitCharacter (Char
)) {
3503 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3507 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3509 This function returns a value of type UINTN by interpreting the contents
3510 of the Unicode string specified by String as a hexadecimal number.
3511 The format of the input Unicode string String is:
3513 [spaces][zeros][x][hexadecimal digits].
3515 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3516 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3517 If "x" appears in the input string, it must be prefixed with at least one 0.
3518 The function will ignore the pad space, which includes spaces or tab characters,
3519 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3520 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3521 first valid hexadecimal digit. Then, the function stops at the first character that is
3522 a not a valid hexadecimal character or NULL, whichever one comes first.
3524 If String has only pad spaces, then zero is returned.
3525 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3526 then zero is returned.
3528 @param[in] String A pointer to a Null-terminated Unicode string.
3529 @param[out] Value Upon a successful return the value of the conversion.
3530 @param[in] StopAtSpace FALSE to skip spaces.
3532 @retval EFI_SUCCESS The conversion was successful.
3533 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3534 @retval EFI_DEVICE_ERROR An overflow occured.
3538 InternalShellStrHexToUint64 (
3539 IN CONST CHAR16
*String
,
3541 IN CONST BOOLEAN StopAtSpace
3546 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3547 return (EFI_INVALID_PARAMETER
);
3551 // Ignore the pad spaces (space or tab)
3553 while ((*String
== L
' ') || (*String
== L
'\t')) {
3558 // Ignore leading Zeros after the spaces
3560 while (*String
== L
'0') {
3564 if (InternalShellCharToUpper (*String
) == L
'X') {
3565 if (*(String
- 1) != L
'0') {
3577 // Skip spaces if requested
3579 while (StopAtSpace
&& *String
== L
' ') {
3583 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3585 // If the Hex Number represented by String overflows according
3586 // to the range defined by UINTN, then ASSERT().
3588 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3589 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3590 return (EFI_DEVICE_ERROR
);
3593 Result
= (LShiftU64(Result
, 4));
3594 Result
+= InternalShellHexCharToUintn (*String
);
3598 // Skip spaces if requested
3600 while (StopAtSpace
&& *String
== L
' ') {
3606 return (EFI_SUCCESS
);
3610 Convert a Null-terminated Unicode decimal string to a value of
3613 This function returns a value of type UINT64 by interpreting the contents
3614 of the Unicode string specified by String as a decimal number. The format
3615 of the input Unicode string String is:
3617 [spaces] [decimal digits].
3619 The valid decimal digit character is in the range [0-9]. The
3620 function will ignore the pad space, which includes spaces or
3621 tab characters, before [decimal digits]. The running zero in the
3622 beginning of [decimal digits] will be ignored. Then, the function
3623 stops at the first character that is a not a valid decimal character
3624 or a Null-terminator, whichever one comes first.
3626 If String has only pad spaces, then 0 is returned.
3627 If String has no pad spaces or valid decimal digits,
3630 @param[in] String A pointer to a Null-terminated Unicode string.
3631 @param[out] Value Upon a successful return the value of the conversion.
3632 @param[in] StopAtSpace FALSE to skip spaces.
3634 @retval EFI_SUCCESS The conversion was successful.
3635 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3636 @retval EFI_DEVICE_ERROR An overflow occured.
3640 InternalShellStrDecimalToUint64 (
3641 IN CONST CHAR16
*String
,
3643 IN CONST BOOLEAN StopAtSpace
3648 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3649 return (EFI_INVALID_PARAMETER
);
3653 // Ignore the pad spaces (space or tab)
3655 while ((*String
== L
' ') || (*String
== L
'\t')) {
3660 // Ignore leading Zeros after the spaces
3662 while (*String
== L
'0') {
3669 // Skip spaces if requested
3671 while (StopAtSpace
&& *String
== L
' ') {
3674 while (ShellIsDecimalDigitCharacter (*String
)) {
3676 // If the number represented by String overflows according
3677 // to the range defined by UINT64, then ASSERT().
3680 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3681 return (EFI_DEVICE_ERROR
);
3684 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3688 // Stop at spaces if requested
3690 if (StopAtSpace
&& *String
== L
' ') {
3697 return (EFI_SUCCESS
);
3701 Function to verify and convert a string to its numerical value.
3703 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3705 @param[in] String The string to evaluate.
3706 @param[out] Value Upon a successful return the value of the conversion.
3707 @param[in] ForceHex TRUE - always assume hex.
3708 @param[in] StopAtSpace FALSE to skip spaces.
3710 @retval EFI_SUCCESS The conversion was successful.
3711 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3712 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3716 ShellConvertStringToUint64(
3717 IN CONST CHAR16
*String
,
3719 IN CONST BOOLEAN ForceHex
,
3720 IN CONST BOOLEAN StopAtSpace
3724 CONST CHAR16
*Walker
;
3730 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
)) {
3733 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
)) {
3734 return (EFI_INVALID_PARAMETER
);
3737 return (EFI_INVALID_PARAMETER
);
3742 // Chop off leading spaces
3744 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
3747 // make sure we have something left that is numeric.
3749 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
)) {
3750 return (EFI_INVALID_PARAMETER
);
3754 // do the conversion.
3756 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
3757 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
3759 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
3762 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
3763 return (EFI_NOT_FOUND
);
3766 if (Value
!= NULL
) {
3774 Function to determin if an entire string is a valid number.
3776 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3778 @param[in] String The string to evaluate.
3779 @param[in] ForceHex TRUE - always assume hex.
3780 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3782 @retval TRUE It is all numeric (dec/hex) characters.
3783 @retval FALSE There is a non-numeric character.
3787 ShellIsHexOrDecimalNumber (
3788 IN CONST CHAR16
*String
,
3789 IN CONST BOOLEAN ForceHex
,
3790 IN CONST BOOLEAN StopAtSpace
3793 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {