2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2015, 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>
18 #include <Library/BaseLib.h>
20 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
25 SHELL_PARAM_ITEM EmptyParamList
[] = {
28 SHELL_PARAM_ITEM SfoParamList
[] = {
32 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
33 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
34 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
35 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
36 EFI_HANDLE mEfiShellEnvironment2Handle
;
37 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
40 Check if a Unicode character is a hexadecimal character.
42 This internal function checks if a Unicode character is a
43 numeric character. The valid hexadecimal characters are
44 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
46 @param Char The character to check against.
48 @retval TRUE If the Char is a hexadecmial character.
49 @retval FALSE If the Char is not a hexadecmial character.
54 ShellIsHexaDecimalDigitCharacter (
58 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
62 Check if a Unicode character is a decimal character.
64 This internal function checks if a Unicode character is a
65 decimal character. The valid characters are
69 @param Char The character to check against.
71 @retval TRUE If the Char is a hexadecmial character.
72 @retval FALSE If the Char is not a hexadecmial character.
77 ShellIsDecimalDigitCharacter (
81 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
85 Helper function to find ShellEnvironment2 for constructor.
87 @param[in] ImageHandle A copy of the calling image's handle.
89 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
94 IN EFI_HANDLE ImageHandle
104 Status
= gBS
->OpenProtocol(ImageHandle
,
105 &gEfiShellEnvironment2Guid
,
106 (VOID
**)&mEfiShellEnvironment2
,
109 EFI_OPEN_PROTOCOL_GET_PROTOCOL
112 // look for the mEfiShellEnvironment2 protocol at a higher level
114 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
116 // figure out how big of a buffer we need.
118 Status
= gBS
->LocateHandle (ByProtocol
,
119 &gEfiShellEnvironment2Guid
,
120 NULL
, // ignored for ByProtocol
125 // maybe it's not there???
127 if (Status
== EFI_BUFFER_TOO_SMALL
) {
128 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
129 if (Buffer
== NULL
) {
130 return (EFI_OUT_OF_RESOURCES
);
132 Status
= gBS
->LocateHandle (ByProtocol
,
133 &gEfiShellEnvironment2Guid
,
134 NULL
, // ignored for ByProtocol
139 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
141 // now parse the list of returned handles
143 Status
= EFI_NOT_FOUND
;
144 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
145 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
146 &gEfiShellEnvironment2Guid
,
147 (VOID
**)&mEfiShellEnvironment2
,
150 EFI_OPEN_PROTOCOL_GET_PROTOCOL
152 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
153 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
154 Status
= EFI_SUCCESS
;
160 if (Buffer
!= NULL
) {
167 Function to do most of the work of the constructor. Allows for calling
168 multiple times without complete re-initialization.
170 @param[in] ImageHandle A copy of the ImageHandle.
171 @param[in] SystemTable A pointer to the SystemTable for the application.
173 @retval EFI_SUCCESS The operationw as successful.
177 ShellLibConstructorWorker (
178 IN EFI_HANDLE ImageHandle
,
179 IN EFI_SYSTEM_TABLE
*SystemTable
185 // UEFI 2.0 shell interfaces (used preferentially)
187 Status
= gBS
->OpenProtocol(
189 &gEfiShellProtocolGuid
,
190 (VOID
**)&gEfiShellProtocol
,
193 EFI_OPEN_PROTOCOL_GET_PROTOCOL
195 if (EFI_ERROR(Status
)) {
197 // Search for the shell protocol
199 Status
= gBS
->LocateProtocol(
200 &gEfiShellProtocolGuid
,
202 (VOID
**)&gEfiShellProtocol
204 if (EFI_ERROR(Status
)) {
205 gEfiShellProtocol
= NULL
;
208 Status
= gBS
->OpenProtocol(
210 &gEfiShellParametersProtocolGuid
,
211 (VOID
**)&gEfiShellParametersProtocol
,
214 EFI_OPEN_PROTOCOL_GET_PROTOCOL
216 if (EFI_ERROR(Status
)) {
217 gEfiShellParametersProtocol
= NULL
;
220 if (gEfiShellParametersProtocol
== NULL
|| gEfiShellProtocol
== NULL
) {
222 // Moved to seperate function due to complexity
224 Status
= ShellFindSE2(ImageHandle
);
226 if (EFI_ERROR(Status
)) {
227 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
228 mEfiShellEnvironment2
= NULL
;
230 Status
= gBS
->OpenProtocol(ImageHandle
,
231 &gEfiShellInterfaceGuid
,
232 (VOID
**)&mEfiShellInterface
,
235 EFI_OPEN_PROTOCOL_GET_PROTOCOL
237 if (EFI_ERROR(Status
)) {
238 mEfiShellInterface
= NULL
;
243 // only success getting 2 of either the old or new, but no 1/2 and 1/2
245 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
246 (gEfiShellProtocol
!= NULL
&& gEfiShellParametersProtocol
!= NULL
) ) {
247 if (gEfiShellProtocol
!= NULL
) {
248 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
249 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
250 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
251 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
252 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
253 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
254 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
255 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
256 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
257 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
259 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
260 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
261 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
262 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
263 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
264 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
265 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
266 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
267 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
268 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
270 return (EFI_SUCCESS
);
272 return (EFI_NOT_FOUND
);
275 Constructor for the Shell library.
277 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
279 @param ImageHandle the image handle of the process
280 @param SystemTable the EFI System Table pointer
282 @retval EFI_SUCCESS the initialization was complete sucessfully
283 @return others an error ocurred during initialization
287 ShellLibConstructor (
288 IN EFI_HANDLE ImageHandle
,
289 IN EFI_SYSTEM_TABLE
*SystemTable
292 mEfiShellEnvironment2
= NULL
;
293 gEfiShellProtocol
= NULL
;
294 gEfiShellParametersProtocol
= NULL
;
295 mEfiShellInterface
= NULL
;
296 mEfiShellEnvironment2Handle
= NULL
;
299 // verify that auto initialize is not set false
301 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
302 return (EFI_SUCCESS
);
305 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
309 Destructor for the library. free any resources.
311 @param[in] ImageHandle A copy of the ImageHandle.
312 @param[in] SystemTable A pointer to the SystemTable for the application.
314 @retval EFI_SUCCESS The operation was successful.
315 @return An error from the CloseProtocol function.
320 IN EFI_HANDLE ImageHandle
,
321 IN EFI_SYSTEM_TABLE
*SystemTable
324 if (mEfiShellEnvironment2
!= NULL
) {
325 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
326 &gEfiShellEnvironment2Guid
,
329 mEfiShellEnvironment2
= NULL
;
331 if (mEfiShellInterface
!= NULL
) {
332 gBS
->CloseProtocol(ImageHandle
,
333 &gEfiShellInterfaceGuid
,
336 mEfiShellInterface
= NULL
;
338 if (gEfiShellProtocol
!= NULL
) {
339 gBS
->CloseProtocol(ImageHandle
,
340 &gEfiShellProtocolGuid
,
343 gEfiShellProtocol
= NULL
;
345 if (gEfiShellParametersProtocol
!= NULL
) {
346 gBS
->CloseProtocol(ImageHandle
,
347 &gEfiShellParametersProtocolGuid
,
350 gEfiShellParametersProtocol
= NULL
;
352 mEfiShellEnvironment2Handle
= NULL
;
354 return (EFI_SUCCESS
);
358 This function causes the shell library to initialize itself. If the shell library
359 is already initialized it will de-initialize all the current protocol poitners and
360 re-populate them again.
362 When the library is used with PcdShellLibAutoInitialize set to true this function
363 will return EFI_SUCCESS and perform no actions.
365 This function is intended for internal access for shell commands only.
367 @retval EFI_SUCCESS the initialization was complete sucessfully
376 // if auto initialize is not false then skip
378 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
379 return (EFI_SUCCESS
);
383 // deinit the current stuff
385 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
388 // init the new stuff
390 return (ShellLibConstructorWorker(gImageHandle
, gST
));
394 This function will retrieve the information about the file for the handle
395 specified and store it in allocated pool memory.
397 This function allocates a buffer to store the file's information. It is the
398 caller's responsibility to free the buffer
400 @param FileHandle The file handle of the file for which information is
403 @retval NULL information could not be retrieved.
405 @return the information about the file
410 IN SHELL_FILE_HANDLE FileHandle
413 return (FileFunctionMap
.GetFileInfo(FileHandle
));
417 This function sets the information about the file for the opened handle
420 @param[in] FileHandle The file handle of the file for which information
423 @param[in] FileInfo The information to set.
425 @retval EFI_SUCCESS The information was set.
426 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
427 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
428 @retval EFI_NO_MEDIA The device has no medium.
429 @retval EFI_DEVICE_ERROR The device reported an error.
430 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
431 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
432 @retval EFI_ACCESS_DENIED The file was opened read only.
433 @retval EFI_VOLUME_FULL The volume is full.
438 IN SHELL_FILE_HANDLE FileHandle
,
439 IN EFI_FILE_INFO
*FileInfo
442 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
446 This function will open a file or directory referenced by DevicePath.
448 This function opens a file with the open mode according to the file path. The
449 Attributes is valid only for EFI_FILE_MODE_CREATE.
451 @param FilePath on input the device path to the file. On output
452 the remaining device path.
453 @param DeviceHandle pointer to the system device handle.
454 @param FileHandle pointer to the file handle.
455 @param OpenMode the mode to open the file with.
456 @param Attributes the file's file attributes.
458 @retval EFI_SUCCESS The information was set.
459 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
460 @retval EFI_UNSUPPORTED Could not open the file path.
461 @retval EFI_NOT_FOUND The specified file could not be found on the
462 device or the file system could not be found on
464 @retval EFI_NO_MEDIA The device has no medium.
465 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
466 medium is no longer supported.
467 @retval EFI_DEVICE_ERROR The device reported an error.
468 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
469 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
470 @retval EFI_ACCESS_DENIED The file was opened read only.
471 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
473 @retval EFI_VOLUME_FULL The volume is full.
477 ShellOpenFileByDevicePath(
478 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
479 OUT EFI_HANDLE
*DeviceHandle
,
480 OUT SHELL_FILE_HANDLE
*FileHandle
,
487 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
488 EFI_FILE_PROTOCOL
*Handle1
;
489 EFI_FILE_PROTOCOL
*Handle2
;
490 CHAR16
*FnafPathName
;
493 if (FilePath
== NULL
|| FileHandle
== NULL
|| DeviceHandle
== NULL
) {
494 return (EFI_INVALID_PARAMETER
);
498 // which shell interface should we use
500 if (gEfiShellProtocol
!= NULL
) {
502 // use UEFI Shell 2.0 method.
504 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
505 if (FileName
== NULL
) {
506 return (EFI_INVALID_PARAMETER
);
508 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
515 // use old shell method.
517 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
520 if (EFI_ERROR (Status
)) {
523 Status
= gBS
->OpenProtocol(*DeviceHandle
,
524 &gEfiSimpleFileSystemProtocolGuid
,
525 (VOID
**)&EfiSimpleFileSystemProtocol
,
528 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
529 if (EFI_ERROR (Status
)) {
532 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
533 if (EFI_ERROR (Status
)) {
539 // go down directories one node at a time.
541 while (!IsDevicePathEnd (*FilePath
)) {
543 // For file system access each node should be a file path component
545 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
546 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
549 return (EFI_INVALID_PARAMETER
);
552 // Open this file path node
558 // File Name Alignment Fix (FNAF)
559 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
560 // The structure pointed to by FilePath may be not CHAR16 aligned.
561 // This code copies the potentially unaligned PathName data from the
562 // FilePath structure to the aligned FnafPathName for use in the
563 // calls to Handl2->Open.
567 // Determine length of PathName, in bytes.
569 PathLen
= DevicePathNodeLength (*FilePath
) - SIZE_OF_FILEPATH_DEVICE_PATH
;
572 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
573 // Copy bytes from possibly unaligned location to aligned location
575 FnafPathName
= AllocateCopyPool(PathLen
, (UINT8
*)((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
);
576 if (FnafPathName
== NULL
) {
577 return EFI_OUT_OF_RESOURCES
;
581 // Try to test opening an existing file
583 Status
= Handle2
->Open (
587 OpenMode
&~EFI_FILE_MODE_CREATE
,
592 // see if the error was that it needs to be created
594 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
595 Status
= Handle2
->Open (
605 // Free the alignment buffer
607 FreePool(FnafPathName
);
610 // Close the last node
612 Handle2
->Close (Handle2
);
614 if (EFI_ERROR(Status
)) {
621 *FilePath
= NextDevicePathNode (*FilePath
);
625 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
627 *FileHandle
= (VOID
*)Handle1
;
628 return (EFI_SUCCESS
);
632 This function will open a file or directory referenced by filename.
634 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
635 otherwise, the Filehandle is NULL. The Attributes is valid only for
636 EFI_FILE_MODE_CREATE.
638 if FileName is NULL then ASSERT()
640 @param FileName pointer to file name
641 @param FileHandle pointer to the file handle.
642 @param OpenMode the mode to open the file with.
643 @param Attributes the file's file attributes.
645 @retval EFI_SUCCESS The information was set.
646 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
647 @retval EFI_UNSUPPORTED Could not open the file path.
648 @retval EFI_NOT_FOUND The specified file could not be found on the
649 device or the file system could not be found
651 @retval EFI_NO_MEDIA The device has no medium.
652 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
653 medium is no longer supported.
654 @retval EFI_DEVICE_ERROR The device reported an error.
655 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
656 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
657 @retval EFI_ACCESS_DENIED The file was opened read only.
658 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
660 @retval EFI_VOLUME_FULL The volume is full.
665 IN CONST CHAR16
*FileName
,
666 OUT SHELL_FILE_HANDLE
*FileHandle
,
671 EFI_HANDLE DeviceHandle
;
672 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
674 EFI_FILE_INFO
*FileInfo
;
675 CHAR16
*FileNameCopy
;
678 // ASSERT if FileName is NULL
680 ASSERT(FileName
!= NULL
);
682 if (FileName
== NULL
) {
683 return (EFI_INVALID_PARAMETER
);
686 if (gEfiShellProtocol
!= NULL
) {
687 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
690 // Create only a directory
692 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
693 return ShellCreateDirectory(FileName
, FileHandle
);
697 // Create the directory to create the file in
699 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
700 if (FileName
== NULL
) {
701 return (EFI_OUT_OF_RESOURCES
);
703 PathCleanUpDirectories (FileNameCopy
);
704 if (PathRemoveLastItem (FileNameCopy
)) {
705 ShellCreateDirectory (FileNameCopy
, FileHandle
);
706 ShellCloseFile (FileHandle
);
708 SHELL_FREE_NON_NULL (FileNameCopy
);
712 // Use UEFI Shell 2.0 method to create the file
714 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
717 if (StrCmp(FileName
, L
"NUL") != 0 && !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
718 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
719 ASSERT(FileInfo
!= NULL
);
720 FileInfo
->Attribute
= Attributes
;
721 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
727 // Using EFI Shell version
728 // this means convert name to path and call that function
729 // since this will use EFI method again that will open it.
731 ASSERT(mEfiShellEnvironment2
!= NULL
);
732 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
733 if (FilePath
!= NULL
) {
734 return (ShellOpenFileByDevicePath(&FilePath
,
740 return (EFI_DEVICE_ERROR
);
743 This function create a directory
745 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
746 otherwise, the Filehandle is NULL. If the directory already existed, this
747 function opens the existing directory.
749 @param DirectoryName pointer to directory name
750 @param FileHandle pointer to the file handle.
752 @retval EFI_SUCCESS The information was set.
753 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
754 @retval EFI_UNSUPPORTED Could not open the file path.
755 @retval EFI_NOT_FOUND The specified file could not be found on the
756 device or the file system could not be found
758 @retval EFI_NO_MEDIA The device has no medium.
759 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
760 medium is no longer supported.
761 @retval EFI_DEVICE_ERROR The device reported an error.
762 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
763 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
764 @retval EFI_ACCESS_DENIED The file was opened read only.
765 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
767 @retval EFI_VOLUME_FULL The volume is full.
768 @sa ShellOpenFileByName
772 ShellCreateDirectory(
773 IN CONST CHAR16
*DirectoryName
,
774 OUT SHELL_FILE_HANDLE
*FileHandle
777 if (gEfiShellProtocol
!= NULL
) {
779 // Use UEFI Shell 2.0 method
781 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
786 return (ShellOpenFileByName(DirectoryName
,
788 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
795 This function reads information from an opened file.
797 If FileHandle is not a directory, the function reads the requested number of
798 bytes from the file at the file's current position and returns them in Buffer.
799 If the read goes beyond the end of the file, the read length is truncated to the
800 end of the file. The file's current position is increased by the number of bytes
801 returned. If FileHandle is a directory, the function reads the directory entry
802 at the file's current position and returns the entry in Buffer. If the Buffer
803 is not large enough to hold the current directory entry, then
804 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
805 BufferSize is set to be the size of the buffer needed to read the entry. On
806 success, the current position is updated to the next directory entry. If there
807 are no more directory entries, the read returns a zero-length buffer.
808 EFI_FILE_INFO is the structure returned as the directory entry.
810 @param FileHandle the opened file handle
811 @param BufferSize on input the size of buffer in bytes. on return
812 the number of bytes written.
813 @param Buffer the buffer to put read data into.
815 @retval EFI_SUCCESS Data was read.
816 @retval EFI_NO_MEDIA The device has no media.
817 @retval EFI_DEVICE_ERROR The device reported an error.
818 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
819 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
826 IN SHELL_FILE_HANDLE FileHandle
,
827 IN OUT UINTN
*BufferSize
,
831 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
836 Write data to a file.
838 This function writes the specified number of bytes to the file at the current
839 file position. The current file position is advanced the actual number of bytes
840 written, which is returned in BufferSize. Partial writes only occur when there
841 has been a data error during the write attempt (such as "volume space full").
842 The file is automatically grown to hold the data if required. Direct writes to
843 opened directories are not supported.
845 @param FileHandle The opened file for writing
846 @param BufferSize on input the number of bytes in Buffer. On output
847 the number of bytes written.
848 @param Buffer the buffer containing data to write is stored.
850 @retval EFI_SUCCESS Data was written.
851 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
852 @retval EFI_NO_MEDIA The device has no media.
853 @retval EFI_DEVICE_ERROR The device reported an error.
854 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
855 @retval EFI_WRITE_PROTECTED The device is write-protected.
856 @retval EFI_ACCESS_DENIED The file was open for read only.
857 @retval EFI_VOLUME_FULL The volume is full.
862 IN SHELL_FILE_HANDLE FileHandle
,
863 IN OUT UINTN
*BufferSize
,
867 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
871 Close an open file handle.
873 This function closes a specified file handle. All "dirty" cached file data is
874 flushed to the device, and the file is closed. In all cases the handle is
877 @param FileHandle the file handle to close.
879 @retval EFI_SUCCESS the file handle was closed sucessfully.
884 IN SHELL_FILE_HANDLE
*FileHandle
887 return (FileFunctionMap
.CloseFile(*FileHandle
));
891 Delete a file and close the handle
893 This function closes and deletes a file. In all cases the file handle is closed.
894 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
895 returned, but the handle is still closed.
897 @param FileHandle the file handle to delete
899 @retval EFI_SUCCESS the file was closed sucessfully
900 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
902 @retval INVALID_PARAMETER One of the parameters has an invalid value.
907 IN SHELL_FILE_HANDLE
*FileHandle
910 return (FileFunctionMap
.DeleteFile(*FileHandle
));
914 Set the current position in a file.
916 This function sets the current file position for the handle to the position
917 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
918 absolute positioning is supported, and seeking past the end of the file is
919 allowed (a subsequent write would grow the file). Seeking to position
920 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
921 If FileHandle is a directory, the only position that may be set is zero. This
922 has the effect of starting the read process of the directory entries over.
924 @param FileHandle The file handle on which the position is being set
925 @param Position Byte position from begining of file
927 @retval EFI_SUCCESS Operation completed sucessfully.
928 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
930 @retval INVALID_PARAMETER One of the parameters has an invalid value.
934 ShellSetFilePosition (
935 IN SHELL_FILE_HANDLE FileHandle
,
939 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
943 Gets a file's current position
945 This function retrieves the current file position for the file handle. For
946 directories, the current file position has no meaning outside of the file
947 system driver and as such the operation is not supported. An error is returned
948 if FileHandle is a directory.
950 @param FileHandle The open file handle on which to get the position.
951 @param Position Byte position from begining of file.
953 @retval EFI_SUCCESS the operation completed sucessfully.
954 @retval INVALID_PARAMETER One of the parameters has an invalid value.
955 @retval EFI_UNSUPPORTED the request is not valid on directories.
959 ShellGetFilePosition (
960 IN SHELL_FILE_HANDLE FileHandle
,
964 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
967 Flushes data on a file
969 This function flushes all modified data associated with a file to a device.
971 @param FileHandle The file handle on which to flush data
973 @retval EFI_SUCCESS The data was flushed.
974 @retval EFI_NO_MEDIA The device has no media.
975 @retval EFI_DEVICE_ERROR The device reported an error.
976 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
977 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
978 @retval EFI_ACCESS_DENIED The file was opened for read only.
983 IN SHELL_FILE_HANDLE FileHandle
986 return (FileFunctionMap
.FlushFile(FileHandle
));
989 /** Retrieve first entry from a directory.
991 This function takes an open directory handle and gets information from the
992 first entry in the directory. A buffer is allocated to contain
993 the information and a pointer to the buffer is returned in *Buffer. The
994 caller can use ShellFindNextFile() to get subsequent directory entries.
996 The buffer will be freed by ShellFindNextFile() when the last directory
997 entry is read. Otherwise, the caller must free the buffer, using FreePool,
998 when finished with it.
1000 @param[in] DirHandle The file handle of the directory to search.
1001 @param[out] Buffer The pointer to the buffer for the file's information.
1003 @retval EFI_SUCCESS Found the first file.
1004 @retval EFI_NOT_FOUND Cannot find the directory.
1005 @retval EFI_NO_MEDIA The device has no media.
1006 @retval EFI_DEVICE_ERROR The device reported an error.
1007 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1008 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1013 ShellFindFirstFile (
1014 IN SHELL_FILE_HANDLE DirHandle
,
1015 OUT EFI_FILE_INFO
**Buffer
1019 // pass to file handle lib
1021 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1023 /** Retrieve next entries from a directory.
1025 To use this function, the caller must first call the ShellFindFirstFile()
1026 function to get the first directory entry. Subsequent directory entries are
1027 retrieved by using the ShellFindNextFile() function. This function can
1028 be called several times to get each entry from the directory. If the call of
1029 ShellFindNextFile() retrieved the last directory entry, the next call of
1030 this function will set *NoFile to TRUE and free the buffer.
1032 @param[in] DirHandle The file handle of the directory.
1033 @param[out] Buffer The pointer to buffer for file's information.
1034 @param[out] NoFile The pointer to boolean when last file is found.
1036 @retval EFI_SUCCESS Found the next file, or reached last file
1037 @retval EFI_NO_MEDIA The device has no media.
1038 @retval EFI_DEVICE_ERROR The device reported an error.
1039 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1044 IN SHELL_FILE_HANDLE DirHandle
,
1045 OUT EFI_FILE_INFO
*Buffer
,
1050 // pass to file handle lib
1052 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1055 Retrieve the size of a file.
1057 if FileHandle is NULL then ASSERT()
1058 if Size is NULL then ASSERT()
1060 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1063 @param FileHandle file handle from which size is retrieved
1064 @param Size pointer to size
1066 @retval EFI_SUCCESS operation was completed sucessfully
1067 @retval EFI_DEVICE_ERROR cannot access the file
1072 IN SHELL_FILE_HANDLE FileHandle
,
1076 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1079 Retrieves the status of the break execution flag
1081 this function is useful to check whether the application is being asked to halt by the shell.
1083 @retval TRUE the execution break is enabled
1084 @retval FALSE the execution break is not enabled
1088 ShellGetExecutionBreakFlag(
1093 // Check for UEFI Shell 2.0 protocols
1095 if (gEfiShellProtocol
!= NULL
) {
1098 // We are using UEFI Shell 2.0; see if the event has been triggered
1100 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1107 // using EFI Shell; call the function to check
1109 if (mEfiShellEnvironment2
!= NULL
) {
1110 return (mEfiShellEnvironment2
->GetExecutionBreak());
1116 return the value of an environment variable
1118 this function gets the value of the environment variable set by the
1119 ShellSetEnvironmentVariable function
1121 @param EnvKey The key name of the environment variable.
1123 @retval NULL the named environment variable does not exist.
1124 @return != NULL pointer to the value of the environment variable
1128 ShellGetEnvironmentVariable (
1129 IN CONST CHAR16
*EnvKey
1133 // Check for UEFI Shell 2.0 protocols
1135 if (gEfiShellProtocol
!= NULL
) {
1136 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1140 // Check for EFI shell
1142 if (mEfiShellEnvironment2
!= NULL
) {
1143 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1149 set the value of an environment variable
1151 This function changes the current value of the specified environment variable. If the
1152 environment variable exists and the Value is an empty string, then the environment
1153 variable is deleted. If the environment variable exists and the Value is not an empty
1154 string, then the value of the environment variable is changed. If the environment
1155 variable does not exist and the Value is an empty string, there is no action. If the
1156 environment variable does not exist and the Value is a non-empty string, then the
1157 environment variable is created and assigned the specified value.
1159 This is not supported pre-UEFI Shell 2.0.
1161 @param EnvKey The key name of the environment variable.
1162 @param EnvVal The Value of the environment variable
1163 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1165 @retval EFI_SUCCESS the operation was completed sucessfully
1166 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1170 ShellSetEnvironmentVariable (
1171 IN CONST CHAR16
*EnvKey
,
1172 IN CONST CHAR16
*EnvVal
,
1177 // Check for UEFI Shell 2.0 protocols
1179 if (gEfiShellProtocol
!= NULL
) {
1180 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1184 // This feature does not exist under EFI shell
1186 return (EFI_UNSUPPORTED
);
1190 Cause the shell to parse and execute a command line.
1192 This function creates a nested instance of the shell and executes the specified
1193 command (CommandLine) with the specified environment (Environment). Upon return,
1194 the status code returned by the specified command is placed in StatusCode.
1195 If Environment is NULL, then the current environment is used and all changes made
1196 by the commands executed will be reflected in the current environment. If the
1197 Environment is non-NULL, then the changes made will be discarded.
1198 The CommandLine is executed from the current working directory on the current
1201 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1202 environment. The values pointed to by the parameters will be unchanged by the
1203 ShellExecute() function. The Output parameter has no effect in a
1204 UEFI Shell 2.0 environment.
1206 @param[in] ParentHandle The parent image starting the operation.
1207 @param[in] CommandLine The pointer to a NULL terminated command line.
1208 @param[in] Output True to display debug output. False to hide it.
1209 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1210 in the form "x=y". If NULL, the current set is used.
1211 @param[out] Status The status of the run command line.
1213 @retval EFI_SUCCESS The operation completed sucessfully. Status
1214 contains the status code returned.
1215 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1216 @retval EFI_OUT_OF_RESOURCES Out of resources.
1217 @retval EFI_UNSUPPORTED The operation is not allowed.
1222 IN EFI_HANDLE
*ParentHandle
,
1223 IN CHAR16
*CommandLine OPTIONAL
,
1224 IN BOOLEAN Output OPTIONAL
,
1225 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1226 OUT EFI_STATUS
*Status OPTIONAL
1229 EFI_STATUS CmdStatus
;
1231 // Check for UEFI Shell 2.0 protocols
1233 if (gEfiShellProtocol
!= NULL
) {
1235 // Call UEFI Shell 2.0 version (not using Output parameter)
1237 return (gEfiShellProtocol
->Execute(ParentHandle
,
1239 EnvironmentVariables
,
1244 // Check for EFI shell
1246 if (mEfiShellEnvironment2
!= NULL
) {
1248 // Call EFI Shell version.
1249 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1251 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1255 // No Status output parameter so just use the returned status
1257 if (Status
!= NULL
) {
1258 *Status
= CmdStatus
;
1261 // If there was an error, we can't tell if it was from the command or from
1262 // the Execute() function, so we'll just assume the shell ran successfully
1263 // and the error came from the command.
1268 return (EFI_UNSUPPORTED
);
1272 Retreives the current directory path
1274 If the DeviceName is NULL, it returns the current device's current directory
1275 name. If the DeviceName is not NULL, it returns the current directory name
1278 @param DeviceName the name of the drive to get directory on
1280 @retval NULL the directory does not exist
1281 @return != NULL the directory
1285 ShellGetCurrentDir (
1286 IN CHAR16
* CONST DeviceName OPTIONAL
1290 // Check for UEFI Shell 2.0 protocols
1292 if (gEfiShellProtocol
!= NULL
) {
1293 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1297 // Check for EFI shell
1299 if (mEfiShellEnvironment2
!= NULL
) {
1300 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1306 sets (enabled or disabled) the page break mode
1308 when page break mode is enabled the screen will stop scrolling
1309 and wait for operator input before scrolling a subsequent screen.
1311 @param CurrentState TRUE to enable and FALSE to disable
1315 ShellSetPageBreakMode (
1316 IN BOOLEAN CurrentState
1320 // check for enabling
1322 if (CurrentState
!= 0x00) {
1324 // check for UEFI Shell 2.0
1326 if (gEfiShellProtocol
!= NULL
) {
1328 // Enable with UEFI 2.0 Shell
1330 gEfiShellProtocol
->EnablePageBreak();
1334 // Check for EFI shell
1336 if (mEfiShellEnvironment2
!= NULL
) {
1338 // Enable with EFI Shell
1340 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1346 // check for UEFI Shell 2.0
1348 if (gEfiShellProtocol
!= NULL
) {
1350 // Disable with UEFI 2.0 Shell
1352 gEfiShellProtocol
->DisablePageBreak();
1356 // Check for EFI shell
1358 if (mEfiShellEnvironment2
!= NULL
) {
1360 // Disable with EFI Shell
1362 mEfiShellEnvironment2
->DisablePageBreak ();
1370 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1371 /// This allows for the struct to be populated.
1378 SHELL_FILE_HANDLE Handle
;
1379 EFI_FILE_INFO
*Info
;
1380 } EFI_SHELL_FILE_INFO_NO_CONST
;
1383 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1385 if OldStyleFileList is NULL then ASSERT()
1387 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1388 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1389 the ShellCloseFileMetaArg function.
1391 @param[in] FileList the EFI shell list type
1392 @param[in, out] ListHead the list to add to
1394 @retval the resultant head of the double linked new format list;
1398 InternalShellConvertFileListType (
1399 IN LIST_ENTRY
*FileList
,
1400 IN OUT LIST_ENTRY
*ListHead
1403 SHELL_FILE_ARG
*OldInfo
;
1405 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1410 ASSERT(FileList
!= NULL
);
1411 ASSERT(ListHead
!= NULL
);
1414 // enumerate through each member of the old list and copy
1416 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1417 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1418 ASSERT(OldInfo
!= NULL
);
1421 // Skip ones that failed to open...
1423 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1428 // make sure the old list was valid
1430 ASSERT(OldInfo
->Info
!= NULL
);
1431 ASSERT(OldInfo
->FullName
!= NULL
);
1432 ASSERT(OldInfo
->FileName
!= NULL
);
1435 // allocate a new EFI_SHELL_FILE_INFO object
1437 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1438 if (NewInfo
== NULL
) {
1439 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1445 // copy the simple items
1447 NewInfo
->Handle
= OldInfo
->Handle
;
1448 NewInfo
->Status
= OldInfo
->Status
;
1450 // old shell checks for 0 not NULL
1451 OldInfo
->Handle
= 0;
1454 // allocate new space to copy strings and structure
1456 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1457 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1458 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1461 // make sure all the memory allocations were sucessful
1463 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1465 // Free the partially allocated new node
1467 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1468 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1469 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1470 SHELL_FREE_NON_NULL(NewInfo
);
1473 // Free the previously converted stuff
1475 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1481 // add that to the list
1483 InsertTailList(ListHead
, &NewInfo
->Link
);
1488 Opens a group of files based on a path.
1490 This function uses the Arg to open all the matching files. Each matched
1491 file has a SHELL_FILE_INFO structure to record the file information. These
1492 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1493 structures from ListHead to access each file. This function supports wildcards
1494 and will process '?' and '*' as such. the list must be freed with a call to
1495 ShellCloseFileMetaArg().
1497 If you are NOT appending to an existing list *ListHead must be NULL. If
1498 *ListHead is NULL then it must be callee freed.
1500 @param Arg pointer to path string
1501 @param OpenMode mode to open files with
1502 @param ListHead head of linked list of results
1504 @retval EFI_SUCCESS the operation was sucessful and the list head
1505 contains the list of opened files
1506 @return != EFI_SUCCESS the operation failed
1508 @sa InternalShellConvertFileListType
1512 ShellOpenFileMetaArg (
1515 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1519 LIST_ENTRY mOldStyleFileList
;
1520 CHAR16
*CleanFilePathStr
;
1523 // ASSERT that Arg and ListHead are not NULL
1525 ASSERT(Arg
!= NULL
);
1526 ASSERT(ListHead
!= NULL
);
1528 CleanFilePathStr
= NULL
;
1530 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1531 if (EFI_ERROR (Status
)) {
1536 // Check for UEFI Shell 2.0 protocols
1538 if (gEfiShellProtocol
!= NULL
) {
1539 if (*ListHead
== NULL
) {
1540 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1541 if (*ListHead
== NULL
) {
1542 FreePool(CleanFilePathStr
);
1543 return (EFI_OUT_OF_RESOURCES
);
1545 InitializeListHead(&((*ListHead
)->Link
));
1547 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1550 if (EFI_ERROR(Status
)) {
1551 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1553 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1555 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1556 FreePool(*ListHead
);
1557 FreePool(CleanFilePathStr
);
1559 return (EFI_NOT_FOUND
);
1561 FreePool(CleanFilePathStr
);
1566 // Check for EFI shell
1568 if (mEfiShellEnvironment2
!= NULL
) {
1570 // make sure the list head is initialized
1572 InitializeListHead(&mOldStyleFileList
);
1575 // Get the EFI Shell list of files
1577 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1578 if (EFI_ERROR(Status
)) {
1580 FreePool(CleanFilePathStr
);
1584 if (*ListHead
== NULL
) {
1585 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1586 if (*ListHead
== NULL
) {
1587 FreePool(CleanFilePathStr
);
1588 return (EFI_OUT_OF_RESOURCES
);
1590 InitializeListHead(&((*ListHead
)->Link
));
1594 // Convert that to equivalent of UEFI Shell 2.0 structure
1596 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1599 // Free the EFI Shell version that was converted.
1601 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1603 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1604 FreePool(*ListHead
);
1606 Status
= EFI_NOT_FOUND
;
1608 FreePool(CleanFilePathStr
);
1612 FreePool(CleanFilePathStr
);
1613 return (EFI_UNSUPPORTED
);
1616 Free the linked list returned from ShellOpenFileMetaArg.
1618 if ListHead is NULL then ASSERT().
1620 @param ListHead the pointer to free.
1622 @retval EFI_SUCCESS the operation was sucessful.
1626 ShellCloseFileMetaArg (
1627 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1633 // ASSERT that ListHead is not NULL
1635 ASSERT(ListHead
!= NULL
);
1638 // Check for UEFI Shell 2.0 protocols
1640 if (gEfiShellProtocol
!= NULL
) {
1641 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1642 } else if (mEfiShellEnvironment2
!= NULL
) {
1644 // Since this is EFI Shell version we need to free our internally made copy
1647 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1648 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1649 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1650 RemoveEntryList(Node
);
1651 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1652 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1653 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1654 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1655 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1657 SHELL_FREE_NON_NULL(*ListHead
);
1661 return (EFI_UNSUPPORTED
);
1665 Find a file by searching the CWD and then the path.
1667 If FileName is NULL then ASSERT.
1669 If the return value is not NULL then the memory must be caller freed.
1671 @param FileName Filename string.
1673 @retval NULL the file was not found
1674 @return !NULL the full path to the file.
1679 IN CONST CHAR16
*FileName
1683 SHELL_FILE_HANDLE Handle
;
1687 CONST CHAR16
*Walker
;
1694 // First make sure its not an absolute path.
1696 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1697 if (!EFI_ERROR(Status
)){
1698 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1699 ASSERT(RetVal
== NULL
);
1700 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1701 ShellCloseFile(&Handle
);
1704 ShellCloseFile(&Handle
);
1708 Path
= ShellGetEnvironmentVariable(L
"cwd");
1710 Size
= StrSize(Path
);
1711 Size
+= StrSize(FileName
);
1712 TestPath
= AllocateZeroPool(Size
);
1713 if (TestPath
== NULL
) {
1716 StrnCpy(TestPath
, Path
, Size
/sizeof(CHAR16
) - 1);
1717 StrnCat(TestPath
, FileName
, Size
/sizeof(CHAR16
) - 1 - StrLen(TestPath
));
1718 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1719 if (!EFI_ERROR(Status
)){
1720 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1721 ASSERT(RetVal
== NULL
);
1722 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1723 ShellCloseFile(&Handle
);
1727 ShellCloseFile(&Handle
);
1732 Path
= ShellGetEnvironmentVariable(L
"path");
1734 Size
= StrSize(Path
)+sizeof(CHAR16
);
1735 Size
+= StrSize(FileName
);
1736 TestPath
= AllocateZeroPool(Size
);
1737 if (TestPath
== NULL
) {
1740 Walker
= (CHAR16
*)Path
;
1742 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1743 if (TestPath
!= NULL
) {
1744 TempChar
= StrStr(TestPath
, L
";");
1745 if (TempChar
!= NULL
) {
1746 *TempChar
= CHAR_NULL
;
1748 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1749 StrnCat(TestPath
, L
"\\", Size
/sizeof(CHAR16
) - 1 - StrLen(TestPath
));
1751 if (FileName
[0] == L
'\\') {
1754 StrnCat(TestPath
, FileName
, Size
/sizeof(CHAR16
) - 1 - StrLen(TestPath
));
1755 if (StrStr(Walker
, L
";") != NULL
) {
1756 Walker
= StrStr(Walker
, L
";") + 1;
1760 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1761 if (!EFI_ERROR(Status
)){
1762 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1763 ASSERT(RetVal
== NULL
);
1764 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1765 ShellCloseFile(&Handle
);
1768 ShellCloseFile(&Handle
);
1772 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1779 Find a file by searching the CWD and then the path with a variable set of file
1780 extensions. If the file is not found it will append each extension in the list
1781 in the order provided and return the first one that is successful.
1783 If FileName is NULL, then ASSERT.
1784 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1786 If the return value is not NULL then the memory must be caller freed.
1788 @param[in] FileName Filename string.
1789 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1791 @retval NULL The file was not found.
1792 @retval !NULL The path to the file.
1796 ShellFindFilePathEx (
1797 IN CONST CHAR16
*FileName
,
1798 IN CONST CHAR16
*FileExtension
1803 CONST CHAR16
*ExtensionWalker
;
1808 ASSERT(FileName
!= NULL
);
1809 if (FileExtension
== NULL
) {
1810 return (ShellFindFilePath(FileName
));
1812 RetVal
= ShellFindFilePath(FileName
);
1813 if (RetVal
!= NULL
) {
1816 Size
= StrSize(FileName
);
1817 Size
+= StrSize(FileExtension
);
1818 TestPath
= AllocateZeroPool(Size
);
1819 if (TestPath
== NULL
) {
1822 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1823 StrnCpy(TestPath
, FileName
, Size
/sizeof(CHAR16
) - 1);
1824 if (ExtensionWalker
!= NULL
) {
1825 StrnCat(TestPath
, ExtensionWalker
, Size
/sizeof(CHAR16
) - 1 - StrLen(TestPath
));
1827 TempChar
= StrStr(TestPath
, L
";");
1828 if (TempChar
!= NULL
) {
1829 *TempChar
= CHAR_NULL
;
1831 RetVal
= ShellFindFilePath(TestPath
);
1832 if (RetVal
!= NULL
) {
1835 ASSERT(ExtensionWalker
!= NULL
);
1836 TempChar2
= StrStr(ExtensionWalker
, L
";");
1845 SHELL_PARAM_TYPE Type
;
1847 UINTN OriginalPosition
;
1848 } SHELL_PARAM_PACKAGE
;
1851 Checks the list of valid arguments and returns TRUE if the item was found. If the
1852 return value is TRUE then the type parameter is set also.
1854 if CheckList is NULL then ASSERT();
1855 if Name is NULL then ASSERT();
1856 if Type is NULL then ASSERT();
1858 @param Name pointer to Name of parameter found
1859 @param CheckList List to check against
1860 @param Type pointer to type of parameter if it was found
1862 @retval TRUE the Parameter was found. Type is valid.
1863 @retval FALSE the Parameter was not found. Type is not valid.
1867 InternalIsOnCheckList (
1868 IN CONST CHAR16
*Name
,
1869 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1870 OUT SHELL_PARAM_TYPE
*Type
1873 SHELL_PARAM_ITEM
*TempListItem
;
1877 // ASSERT that all 3 pointer parameters aren't NULL
1879 ASSERT(CheckList
!= NULL
);
1880 ASSERT(Type
!= NULL
);
1881 ASSERT(Name
!= NULL
);
1884 // question mark and page break mode are always supported
1886 if ((StrCmp(Name
, L
"-?") == 0) ||
1887 (StrCmp(Name
, L
"-b") == 0)
1894 // Enumerate through the list
1896 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1898 // If the Type is TypeStart only check the first characters of the passed in param
1899 // If it matches set the type and return TRUE
1901 if (TempListItem
->Type
== TypeStart
) {
1902 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1903 *Type
= TempListItem
->Type
;
1907 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1908 if (TempString
!= NULL
) {
1909 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1910 *Type
= TempListItem
->Type
;
1911 FreePool(TempString
);
1914 FreePool(TempString
);
1916 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1917 *Type
= TempListItem
->Type
;
1925 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1927 @param[in] Name pointer to Name of parameter found
1928 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1929 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1931 @retval TRUE the Parameter is a flag.
1932 @retval FALSE the Parameter not a flag.
1937 IN CONST CHAR16
*Name
,
1938 IN CONST BOOLEAN AlwaysAllowNumbers
,
1939 IN CONST BOOLEAN TimeNumbers
1943 // ASSERT that Name isn't NULL
1945 ASSERT(Name
!= NULL
);
1948 // If we accept numbers then dont return TRUE. (they will be values)
1950 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
1955 // If the Name has a /, +, or - as the first character return TRUE
1957 if ((Name
[0] == L
'/') ||
1958 (Name
[0] == L
'-') ||
1967 Checks the command line arguments passed against the list of valid ones.
1969 If no initialization is required, then return RETURN_SUCCESS.
1971 @param[in] CheckList pointer to list of parameters to check
1972 @param[out] CheckPackage pointer to pointer to list checked values
1973 @param[out] ProblemParam optional pointer to pointer to unicode string for
1974 the paramater that caused failure. If used then the
1975 caller is responsible for freeing the memory.
1976 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1977 @param[in] Argv pointer to array of parameters
1978 @param[in] Argc Count of parameters in Argv
1979 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
1981 @retval EFI_SUCCESS The operation completed sucessfully.
1982 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1983 @retval EFI_INVALID_PARAMETER A parameter was invalid
1984 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1985 duplicated. the duplicated command line argument
1986 was returned in ProblemParam if provided.
1987 @retval EFI_NOT_FOUND a argument required a value that was missing.
1988 the invalid command line argument was returned in
1989 ProblemParam if provided.
1993 InternalCommandLineParse (
1994 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1995 OUT LIST_ENTRY
**CheckPackage
,
1996 OUT CHAR16
**ProblemParam OPTIONAL
,
1997 IN BOOLEAN AutoPageBreak
,
1998 IN CONST CHAR16
**Argv
,
2000 IN BOOLEAN AlwaysAllowNumbers
2004 SHELL_PARAM_TYPE CurrentItemType
;
2005 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2009 CONST CHAR16
*TempPointer
;
2010 UINTN CurrentValueSize
;
2012 CurrentItemPackage
= NULL
;
2018 // If there is only 1 item we dont need to do anything
2021 *CheckPackage
= NULL
;
2022 return (EFI_SUCCESS
);
2028 ASSERT(CheckList
!= NULL
);
2029 ASSERT(Argv
!= NULL
);
2032 // initialize the linked list
2034 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2035 if (*CheckPackage
== NULL
) {
2036 return (EFI_OUT_OF_RESOURCES
);
2039 InitializeListHead(*CheckPackage
);
2042 // loop through each of the arguments
2044 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2045 if (Argv
[LoopCounter
] == NULL
) {
2047 // do nothing for NULL argv
2049 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2051 // We might have leftover if last parameter didnt have optional value
2053 if (GetItemValue
!= 0) {
2055 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2060 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2061 if (CurrentItemPackage
== NULL
) {
2062 ShellCommandLineFreeVarList(*CheckPackage
);
2063 *CheckPackage
= NULL
;
2064 return (EFI_OUT_OF_RESOURCES
);
2066 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2067 if (CurrentItemPackage
->Name
== NULL
) {
2068 ShellCommandLineFreeVarList(*CheckPackage
);
2069 *CheckPackage
= NULL
;
2070 return (EFI_OUT_OF_RESOURCES
);
2072 CurrentItemPackage
->Type
= CurrentItemType
;
2073 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2074 CurrentItemPackage
->Value
= NULL
;
2077 // Does this flag require a value
2079 switch (CurrentItemPackage
->Type
) {
2081 // possibly trigger the next loop(s) to populate the value of this item
2088 case TypeDoubleValue
:
2093 GetItemValue
= (UINTN
)(-1);
2098 // this item has no value expected; we are done
2100 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2101 ASSERT(GetItemValue
== 0);
2104 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, CurrentItemPackage
->Type
== TypeTimeValue
)) {
2106 // get the item VALUE for a previous flag
2108 if (StrStr(Argv
[LoopCounter
], L
" ") == NULL
) {
2109 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2110 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2111 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2112 if (ValueSize
== 0) {
2113 StrnCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
], CurrentValueSize
/sizeof(CHAR16
) - 1);
2115 StrnCat(CurrentItemPackage
->Value
, L
" ", CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2116 StrnCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
], CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2118 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2121 // the parameter has spaces. must be quoted.
2123 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
) + sizeof(CHAR16
) + sizeof(CHAR16
);
2124 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2125 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2126 if (ValueSize
== 0) {
2127 StrnCpy(CurrentItemPackage
->Value
, L
"\"", CurrentValueSize
/sizeof(CHAR16
) - 1);
2128 StrnCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
], CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2129 StrnCat(CurrentItemPackage
->Value
, L
"\"", CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2131 StrnCat(CurrentItemPackage
->Value
, L
" ", CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2132 StrnCat(CurrentItemPackage
->Value
, L
"\"", CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2133 StrnCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
], CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2134 StrnCat(CurrentItemPackage
->Value
, L
"\"", CurrentValueSize
/sizeof(CHAR16
) - 1 - StrLen(CurrentItemPackage
->Value
));
2136 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2139 if (GetItemValue
== 0) {
2140 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2142 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2144 // add this one as a non-flag
2147 TempPointer
= Argv
[LoopCounter
];
2148 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2149 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2150 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2154 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2155 if (CurrentItemPackage
== NULL
) {
2156 ShellCommandLineFreeVarList(*CheckPackage
);
2157 *CheckPackage
= NULL
;
2158 return (EFI_OUT_OF_RESOURCES
);
2160 CurrentItemPackage
->Name
= NULL
;
2161 CurrentItemPackage
->Type
= TypePosition
;
2162 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2163 if (CurrentItemPackage
->Value
== NULL
) {
2164 ShellCommandLineFreeVarList(*CheckPackage
);
2165 *CheckPackage
= NULL
;
2166 return (EFI_OUT_OF_RESOURCES
);
2168 CurrentItemPackage
->OriginalPosition
= Count
++;
2169 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2172 // this was a non-recognised flag... error!
2174 if (ProblemParam
!= NULL
) {
2175 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2177 ShellCommandLineFreeVarList(*CheckPackage
);
2178 *CheckPackage
= NULL
;
2179 return (EFI_VOLUME_CORRUPTED
);
2182 if (GetItemValue
!= 0) {
2184 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2187 // support for AutoPageBreak
2189 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2190 ShellSetPageBreakMode(TRUE
);
2192 return (EFI_SUCCESS
);
2196 Checks the command line arguments passed against the list of valid ones.
2197 Optionally removes NULL values first.
2199 If no initialization is required, then return RETURN_SUCCESS.
2201 @param[in] CheckList The pointer to list of parameters to check.
2202 @param[out] CheckPackage The package of checked values.
2203 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2204 the paramater that caused failure.
2205 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2206 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2208 @retval EFI_SUCCESS The operation completed sucessfully.
2209 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2210 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2211 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2212 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2213 of the command line arguments was returned in
2214 ProblemParam if provided.
2215 @retval EFI_NOT_FOUND A argument required a value that was missing.
2216 The invalid command line argument was returned in
2217 ProblemParam if provided.
2221 ShellCommandLineParseEx (
2222 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2223 OUT LIST_ENTRY
**CheckPackage
,
2224 OUT CHAR16
**ProblemParam OPTIONAL
,
2225 IN BOOLEAN AutoPageBreak
,
2226 IN BOOLEAN AlwaysAllowNumbers
2230 // ASSERT that CheckList and CheckPackage aren't NULL
2232 ASSERT(CheckList
!= NULL
);
2233 ASSERT(CheckPackage
!= NULL
);
2236 // Check for UEFI Shell 2.0 protocols
2238 if (gEfiShellParametersProtocol
!= NULL
) {
2239 return (InternalCommandLineParse(CheckList
,
2243 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2244 gEfiShellParametersProtocol
->Argc
,
2245 AlwaysAllowNumbers
));
2249 // ASSERT That EFI Shell is not required
2251 ASSERT (mEfiShellInterface
!= NULL
);
2252 return (InternalCommandLineParse(CheckList
,
2256 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2257 mEfiShellInterface
->Argc
,
2258 AlwaysAllowNumbers
));
2262 Frees shell variable list that was returned from ShellCommandLineParse.
2264 This function will free all the memory that was used for the CheckPackage
2265 list of postprocessed shell arguments.
2267 this function has no return value.
2269 if CheckPackage is NULL, then return
2271 @param CheckPackage the list to de-allocate
2275 ShellCommandLineFreeVarList (
2276 IN LIST_ENTRY
*CheckPackage
2282 // check for CheckPackage == NULL
2284 if (CheckPackage
== NULL
) {
2289 // for each node in the list
2291 for ( Node
= GetFirstNode(CheckPackage
)
2292 ; !IsListEmpty(CheckPackage
)
2293 ; Node
= GetFirstNode(CheckPackage
)
2296 // Remove it from the list
2298 RemoveEntryList(Node
);
2301 // if it has a name free the name
2303 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2304 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2308 // if it has a value free the value
2310 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2311 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2315 // free the node structure
2317 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2320 // free the list head node
2322 FreePool(CheckPackage
);
2325 Checks for presence of a flag parameter
2327 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2329 if CheckPackage is NULL then return FALSE.
2330 if KeyString is NULL then ASSERT()
2332 @param CheckPackage The package of parsed command line arguments
2333 @param KeyString the Key of the command line argument to check for
2335 @retval TRUE the flag is on the command line
2336 @retval FALSE the flag is not on the command line
2340 ShellCommandLineGetFlag (
2341 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2342 IN CONST CHAR16
* CONST KeyString
2349 // return FALSE for no package or KeyString is NULL
2351 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2356 // enumerate through the list of parametrs
2358 for ( Node
= GetFirstNode(CheckPackage
)
2359 ; !IsNull (CheckPackage
, Node
)
2360 ; Node
= GetNextNode(CheckPackage
, Node
)
2363 // If the Name matches, return TRUE (and there may be NULL name)
2365 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2367 // If Type is TypeStart then only compare the begining of the strings
2369 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2370 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2374 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2375 if (TempString
!= NULL
) {
2376 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2377 FreePool(TempString
);
2380 FreePool(TempString
);
2382 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2390 Returns value from command line argument.
2392 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2394 If CheckPackage is NULL, then return NULL.
2396 @param[in] CheckPackage The package of parsed command line arguments.
2397 @param[in] KeyString The Key of the command line argument to check for.
2399 @retval NULL The flag is not on the command line.
2400 @retval !=NULL The pointer to unicode string of the value.
2404 ShellCommandLineGetValue (
2405 IN CONST LIST_ENTRY
*CheckPackage
,
2406 IN CHAR16
*KeyString
2413 // return NULL for no package or KeyString is NULL
2415 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2420 // enumerate through the list of parametrs
2422 for ( Node
= GetFirstNode(CheckPackage
)
2423 ; !IsNull (CheckPackage
, Node
)
2424 ; Node
= GetNextNode(CheckPackage
, Node
)
2427 // If the Name matches, return TRUE (and there may be NULL name)
2429 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2431 // If Type is TypeStart then only compare the begining of the strings
2433 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2434 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2435 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2438 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2439 if (TempString
!= NULL
) {
2440 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2441 FreePool(TempString
);
2442 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2444 FreePool(TempString
);
2446 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2447 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2455 Returns raw value from command line argument.
2457 Raw value parameters are in the form of "value" in a specific position in the list.
2459 If CheckPackage is NULL, then return NULL.
2461 @param[in] CheckPackage The package of parsed command line arguments.
2462 @param[in] Position The position of the value.
2464 @retval NULL The flag is not on the command line.
2465 @retval !=NULL The pointer to unicode string of the value.
2469 ShellCommandLineGetRawValue (
2470 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2477 // check for CheckPackage == NULL
2479 if (CheckPackage
== NULL
) {
2484 // enumerate through the list of parametrs
2486 for ( Node
= GetFirstNode(CheckPackage
)
2487 ; !IsNull (CheckPackage
, Node
)
2488 ; Node
= GetNextNode(CheckPackage
, Node
)
2491 // If the position matches, return the value
2493 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2494 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2501 returns the number of command line value parameters that were parsed.
2503 this will not include flags.
2505 @param[in] CheckPackage The package of parsed command line arguments.
2507 @retval (UINTN)-1 No parsing has ocurred
2508 @return other The number of value parameters found
2512 ShellCommandLineGetCount(
2513 IN CONST LIST_ENTRY
*CheckPackage
2519 if (CheckPackage
== NULL
) {
2522 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2523 ; !IsNull (CheckPackage
, Node1
)
2524 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2526 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2534 Determins if a parameter is duplicated.
2536 If Param is not NULL then it will point to a callee allocated string buffer
2537 with the parameter value if a duplicate is found.
2539 If CheckPackage is NULL, then ASSERT.
2541 @param[in] CheckPackage The package of parsed command line arguments.
2542 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2544 @retval EFI_SUCCESS No parameters were duplicated.
2545 @retval EFI_DEVICE_ERROR A duplicate was found.
2549 ShellCommandLineCheckDuplicate (
2550 IN CONST LIST_ENTRY
*CheckPackage
,
2557 ASSERT(CheckPackage
!= NULL
);
2559 for ( Node1
= GetFirstNode(CheckPackage
)
2560 ; !IsNull (CheckPackage
, Node1
)
2561 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2563 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2564 ; !IsNull (CheckPackage
, Node2
)
2565 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2567 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) {
2568 if (Param
!= NULL
) {
2570 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2572 return (EFI_DEVICE_ERROR
);
2576 return (EFI_SUCCESS
);
2580 This is a find and replace function. Upon successful return the NewString is a copy of
2581 SourceString with each instance of FindTarget replaced with ReplaceWith.
2583 If SourceString and NewString overlap the behavior is undefined.
2585 If the string would grow bigger than NewSize it will halt and return error.
2587 @param[in] SourceString The string with source buffer.
2588 @param[in, out] NewString The string with resultant buffer.
2589 @param[in] NewSize The size in bytes of NewString.
2590 @param[in] FindTarget The string to look for.
2591 @param[in] ReplaceWith The string to replace FindTarget with.
2592 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2593 immediately before it.
2594 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2596 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2597 @retval EFI_INVALID_PARAMETER NewString was NULL.
2598 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2599 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2600 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2601 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2602 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2603 the new string (truncation occurred).
2604 @retval EFI_SUCCESS The string was successfully copied with replacement.
2608 ShellCopySearchAndReplace(
2609 IN CHAR16 CONST
*SourceString
,
2610 IN OUT CHAR16
*NewString
,
2612 IN CONST CHAR16
*FindTarget
,
2613 IN CONST CHAR16
*ReplaceWith
,
2614 IN CONST BOOLEAN SkipPreCarrot
,
2615 IN CONST BOOLEAN ParameterReplacing
2621 if ( (SourceString
== NULL
)
2622 || (NewString
== NULL
)
2623 || (FindTarget
== NULL
)
2624 || (ReplaceWith
== NULL
)
2625 || (StrLen(FindTarget
) < 1)
2626 || (StrLen(SourceString
) < 1)
2628 return (EFI_INVALID_PARAMETER
);
2631 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2632 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2634 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2635 if (Replace
!= NULL
) {
2636 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2639 if (Replace
== NULL
) {
2640 return (EFI_OUT_OF_RESOURCES
);
2642 NewString
= ZeroMem(NewString
, NewSize
);
2643 while (*SourceString
!= CHAR_NULL
) {
2645 // if we find the FindTarget and either Skip == FALSE or Skip and we
2646 // dont have a carrot do a replace...
2648 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2649 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2651 SourceString
+= StrLen(FindTarget
);
2652 Size
= StrSize(NewString
);
2653 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2655 return (EFI_BUFFER_TOO_SMALL
);
2657 StrnCat(NewString
, Replace
, NewSize
/sizeof(CHAR16
) - 1 - StrLen(NewString
));
2659 Size
= StrSize(NewString
);
2660 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2662 return (EFI_BUFFER_TOO_SMALL
);
2664 StrnCat(NewString
, SourceString
, 1);
2669 return (EFI_SUCCESS
);
2673 Internal worker function to output a string.
2675 This function will output a string to the correct StdOut.
2677 @param[in] String The string to print out.
2679 @retval EFI_SUCCESS The operation was sucessful.
2680 @retval !EFI_SUCCESS The operation failed.
2685 IN CONST CHAR16
*String
2689 Size
= StrSize(String
) - sizeof(CHAR16
);
2691 return (EFI_SUCCESS
);
2693 if (gEfiShellParametersProtocol
!= NULL
) {
2694 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2696 if (mEfiShellInterface
!= NULL
) {
2697 if (mEfiShellInterface
->RedirArgc
== 0) {
2699 // Divide in half for old shell. Must be string length not size.
2701 Size
/=2; // Divide in half only when no redirection.
2703 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2706 return (EFI_UNSUPPORTED
);
2710 Print at a specific location on the screen.
2712 This function will move the cursor to a given screen location and print the specified string
2714 If -1 is specified for either the Row or Col the current screen location for BOTH
2717 if either Row or Col is out of range for the current console, then ASSERT
2718 if Format is NULL, then ASSERT
2720 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2721 the following additional flags:
2722 %N - Set output attribute to normal
2723 %H - Set output attribute to highlight
2724 %E - Set output attribute to error
2725 %B - Set output attribute to blue color
2726 %V - Set output attribute to green color
2728 Note: The background color is controlled by the shell command cls.
2730 @param[in] Col the column to print at
2731 @param[in] Row the row to print at
2732 @param[in] Format the format string
2733 @param[in] Marker the marker for the variable argument list
2735 @return EFI_SUCCESS The operation was successful.
2736 @return EFI_DEVICE_ERROR The console device reported an error.
2740 InternalShellPrintWorker(
2741 IN INT32 Col OPTIONAL
,
2742 IN INT32 Row OPTIONAL
,
2743 IN CONST CHAR16
*Format
,
2748 CHAR16
*ResumeLocation
;
2749 CHAR16
*FormatWalker
;
2750 UINTN OriginalAttribute
;
2751 CHAR16
*mPostReplaceFormat
;
2752 CHAR16
*mPostReplaceFormat2
;
2754 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2755 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2757 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2758 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2759 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2760 return (EFI_OUT_OF_RESOURCES
);
2763 Status
= EFI_SUCCESS
;
2764 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2767 // Back and forth each time fixing up 1 of our flags...
2769 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2770 ASSERT_EFI_ERROR(Status
);
2771 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2772 ASSERT_EFI_ERROR(Status
);
2773 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2774 ASSERT_EFI_ERROR(Status
);
2775 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2776 ASSERT_EFI_ERROR(Status
);
2777 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2778 ASSERT_EFI_ERROR(Status
);
2781 // Use the last buffer from replacing to print from...
2783 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2785 if (Col
!= -1 && Row
!= -1) {
2786 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2789 FormatWalker
= mPostReplaceFormat2
;
2790 while (*FormatWalker
!= CHAR_NULL
) {
2792 // Find the next attribute change request
2794 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2795 if (ResumeLocation
!= NULL
) {
2796 *ResumeLocation
= CHAR_NULL
;
2799 // print the current FormatWalker string
2801 if (StrLen(FormatWalker
)>0) {
2802 Status
= InternalPrintTo(FormatWalker
);
2803 if (EFI_ERROR(Status
)) {
2809 // update the attribute
2811 if (ResumeLocation
!= NULL
) {
2812 if (*(ResumeLocation
-1) == L
'^') {
2814 // Move cursor back 1 position to overwrite the ^
2816 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2819 // Print a simple '%' symbol
2821 Status
= InternalPrintTo(L
"%");
2822 ResumeLocation
= ResumeLocation
- 1;
2824 switch (*(ResumeLocation
+1)) {
2826 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2829 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2832 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2835 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2838 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2842 // Print a simple '%' symbol
2844 Status
= InternalPrintTo(L
"%");
2845 if (EFI_ERROR(Status
)) {
2848 ResumeLocation
= ResumeLocation
- 1;
2854 // reset to normal now...
2860 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2862 FormatWalker
= ResumeLocation
+ 2;
2865 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2867 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2868 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2873 Print at a specific location on the screen.
2875 This function will move the cursor to a given screen location and print the specified string.
2877 If -1 is specified for either the Row or Col the current screen location for BOTH
2880 If either Row or Col is out of range for the current console, then ASSERT.
2881 If Format is NULL, then ASSERT.
2883 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2884 the following additional flags:
2885 %N - Set output attribute to normal
2886 %H - Set output attribute to highlight
2887 %E - Set output attribute to error
2888 %B - Set output attribute to blue color
2889 %V - Set output attribute to green color
2891 Note: The background color is controlled by the shell command cls.
2893 @param[in] Col the column to print at
2894 @param[in] Row the row to print at
2895 @param[in] Format the format string
2896 @param[in] ... The variable argument list.
2898 @return EFI_SUCCESS The printing was successful.
2899 @return EFI_DEVICE_ERROR The console device reported an error.
2904 IN INT32 Col OPTIONAL
,
2905 IN INT32 Row OPTIONAL
,
2906 IN CONST CHAR16
*Format
,
2912 if (Format
== NULL
) {
2913 return (EFI_INVALID_PARAMETER
);
2915 VA_START (Marker
, Format
);
2916 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2922 Print at a specific location on the screen.
2924 This function will move the cursor to a given screen location and print the specified string.
2926 If -1 is specified for either the Row or Col the current screen location for BOTH
2929 If either Row or Col is out of range for the current console, then ASSERT.
2930 If Format is NULL, then ASSERT.
2932 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2933 the following additional flags:
2934 %N - Set output attribute to normal.
2935 %H - Set output attribute to highlight.
2936 %E - Set output attribute to error.
2937 %B - Set output attribute to blue color.
2938 %V - Set output attribute to green color.
2940 Note: The background color is controlled by the shell command cls.
2942 @param[in] Col The column to print at.
2943 @param[in] Row The row to print at.
2944 @param[in] Language The language of the string to retrieve. If this parameter
2945 is NULL, then the current platform language is used.
2946 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2947 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2948 @param[in] ... The variable argument list.
2950 @return EFI_SUCCESS The printing was successful.
2951 @return EFI_DEVICE_ERROR The console device reported an error.
2956 IN INT32 Col OPTIONAL
,
2957 IN INT32 Row OPTIONAL
,
2958 IN CONST CHAR8
*Language OPTIONAL
,
2959 IN CONST EFI_STRING_ID HiiFormatStringId
,
2960 IN CONST EFI_HANDLE HiiFormatHandle
,
2965 CHAR16
*HiiFormatString
;
2968 VA_START (Marker
, HiiFormatHandle
);
2969 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2970 ASSERT(HiiFormatString
!= NULL
);
2972 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2974 SHELL_FREE_NON_NULL(HiiFormatString
);
2981 Function to determine if a given filename represents a file or a directory.
2983 @param[in] DirName Path to directory to test.
2985 @retval EFI_SUCCESS The Path represents a directory
2986 @retval EFI_NOT_FOUND The Path does not represent a directory
2987 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2988 @return The path failed to open
2993 IN CONST CHAR16
*DirName
2997 SHELL_FILE_HANDLE Handle
;
2998 CHAR16
*TempLocation
;
2999 CHAR16
*TempLocation2
;
3001 ASSERT(DirName
!= NULL
);
3004 TempLocation
= NULL
;
3006 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3007 if (EFI_ERROR(Status
)) {
3009 // try good logic first.
3011 if (gEfiShellProtocol
!= NULL
) {
3012 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3013 if (TempLocation
== NULL
) {
3014 ShellCloseFile(&Handle
);
3015 return (EFI_OUT_OF_RESOURCES
);
3017 TempLocation2
= StrStr(TempLocation
, L
":");
3018 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3019 *(TempLocation2
+1) = CHAR_NULL
;
3021 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3022 FreePool(TempLocation
);
3023 return (EFI_SUCCESS
);
3025 FreePool(TempLocation
);
3028 // probably a map name?!?!!?
3030 TempLocation
= StrStr(DirName
, L
"\\");
3031 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3032 return (EFI_SUCCESS
);
3038 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3039 ShellCloseFile(&Handle
);
3040 return (EFI_SUCCESS
);
3042 ShellCloseFile(&Handle
);
3043 return (EFI_NOT_FOUND
);
3047 Function to determine if a given filename represents a file.
3049 @param[in] Name Path to file to test.
3051 @retval EFI_SUCCESS The Path represents a file.
3052 @retval EFI_NOT_FOUND The Path does not represent a file.
3053 @retval other The path failed to open.
3058 IN CONST CHAR16
*Name
3062 SHELL_FILE_HANDLE Handle
;
3064 ASSERT(Name
!= NULL
);
3068 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3069 if (EFI_ERROR(Status
)) {
3073 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3074 ShellCloseFile(&Handle
);
3075 return (EFI_SUCCESS
);
3077 ShellCloseFile(&Handle
);
3078 return (EFI_NOT_FOUND
);
3082 Function to determine if a given filename represents a file.
3084 This will search the CWD and then the Path.
3086 If Name is NULL, then ASSERT.
3088 @param[in] Name Path to file to test.
3090 @retval EFI_SUCCESS The Path represents a file.
3091 @retval EFI_NOT_FOUND The Path does not represent a file.
3092 @retval other The path failed to open.
3097 IN CONST CHAR16
*Name
3103 if (!EFI_ERROR(ShellIsFile(Name
))) {
3104 return (EFI_SUCCESS
);
3107 NewName
= ShellFindFilePath(Name
);
3108 if (NewName
== NULL
) {
3109 return (EFI_NOT_FOUND
);
3111 Status
= ShellIsFile(NewName
);
3117 Function return the number converted from a hex representation of a number.
3119 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3120 result. Use ShellConvertStringToUint64 instead.
3122 @param[in] String String representation of a number.
3124 @return The unsigned integer result of the conversion.
3125 @retval (UINTN)(-1) An error occured.
3130 IN CONST CHAR16
*String
3135 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3136 return ((UINTN
)RetVal
);
3139 return ((UINTN
)(-1));
3143 Function to determine whether a string is decimal or hex representation of a number
3144 and return the number converted from the string. Spaces are always skipped.
3146 @param[in] String String representation of a number
3149 @retval (UINTN)(-1) An error ocurred.
3154 IN CONST CHAR16
*String
3162 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3166 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3167 return ((UINTN
)RetVal
);
3169 return ((UINTN
)(-1));
3173 Safely append with automatic string resizing given length of Destination and
3174 desired length of copy from Source.
3176 append the first D characters of Source to the end of Destination, where D is
3177 the lesser of Count and the StrLen() of Source. If appending those D characters
3178 will fit within Destination (whose Size is given as CurrentSize) and
3179 still leave room for a NULL terminator, then those characters are appended,
3180 starting at the original terminating NULL of Destination, and a new terminating
3183 If appending D characters onto Destination will result in a overflow of the size
3184 given in CurrentSize the string will be grown such that the copy can be performed
3185 and CurrentSize will be updated to the new size.
3187 If Source is NULL, there is nothing to append, just return the current buffer in
3190 if Destination is NULL, then ASSERT()
3191 if Destination's current length (including NULL terminator) is already more then
3192 CurrentSize, then ASSERT()
3194 @param[in, out] Destination The String to append onto
3195 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3196 return possibly the new size (still in bytes). if NULL
3197 then allocate whatever is needed.
3198 @param[in] Source The String to append from
3199 @param[in] Count Maximum number of characters to append. if 0 then
3202 @return Destination return the resultant string.
3207 IN OUT CHAR16
**Destination
,
3208 IN OUT UINTN
*CurrentSize
,
3209 IN CONST CHAR16
*Source
,
3213 UINTN DestinationStartSize
;
3219 ASSERT(Destination
!= NULL
);
3222 // If there's nothing to do then just return Destination
3224 if (Source
== NULL
) {
3225 return (*Destination
);
3229 // allow for un-initialized pointers, based on size being 0
3231 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3232 *Destination
= NULL
;
3236 // allow for NULL pointers address as Destination
3238 if (*Destination
!= NULL
) {
3239 ASSERT(CurrentSize
!= 0);
3240 DestinationStartSize
= StrSize(*Destination
);
3241 ASSERT(DestinationStartSize
<= *CurrentSize
);
3243 DestinationStartSize
= 0;
3244 // ASSERT(*CurrentSize == 0);
3248 // Append all of Source?
3251 Count
= StrLen(Source
);
3255 // Test and grow if required
3257 if (CurrentSize
!= NULL
) {
3258 NewSize
= *CurrentSize
;
3259 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3260 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3261 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3263 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3264 *CurrentSize
= NewSize
;
3267 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
3271 // Now use standard StrnCat on a big enough buffer
3273 if (*Destination
== NULL
) {
3276 return StrnCat(*Destination
, Source
, Count
);
3280 Prompt the user and return the resultant answer to the requestor.
3282 This function will display the requested question on the shell prompt and then
3283 wait for an apropriate answer to be input from the console.
3285 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3286 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3288 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3291 In either case *Response must be callee freed if Response was not NULL;
3293 @param Type What type of question is asked. This is used to filter the input
3294 to prevent invalid answers to question.
3295 @param Prompt Pointer to string prompt to use to request input.
3296 @param Response Pointer to Response which will be populated upon return.
3298 @retval EFI_SUCCESS The operation was sucessful.
3299 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3300 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3301 @return other The operation failed.
3305 ShellPromptForResponse (
3306 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3307 IN CHAR16
*Prompt OPTIONAL
,
3308 IN OUT VOID
**Response OPTIONAL
3314 SHELL_PROMPT_RESPONSE
*Resp
;
3318 Status
= EFI_UNSUPPORTED
;
3322 if (Type
!= ShellPromptResponseTypeFreeform
) {
3323 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3325 return (EFI_OUT_OF_RESOURCES
);
3330 case ShellPromptResponseTypeQuitContinue
:
3331 if (Prompt
!= NULL
) {
3332 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3335 // wait for valid response
3337 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3338 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3339 if (EFI_ERROR(Status
)) {
3342 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3343 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3344 *Resp
= ShellPromptResponseQuit
;
3346 *Resp
= ShellPromptResponseContinue
;
3349 case ShellPromptResponseTypeYesNoCancel
:
3350 if (Prompt
!= NULL
) {
3351 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3354 // wait for valid response
3356 *Resp
= ShellPromptResponseMax
;
3357 while (*Resp
== ShellPromptResponseMax
) {
3358 if (ShellGetExecutionBreakFlag()) {
3359 Status
= EFI_ABORTED
;
3362 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3363 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3364 if (EFI_ERROR(Status
)) {
3367 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3368 switch (Key
.UnicodeChar
) {
3371 *Resp
= ShellPromptResponseYes
;
3375 *Resp
= ShellPromptResponseNo
;
3379 *Resp
= ShellPromptResponseCancel
;
3383 break; case ShellPromptResponseTypeYesNoAllCancel
:
3384 if (Prompt
!= NULL
) {
3385 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3388 // wait for valid response
3390 *Resp
= ShellPromptResponseMax
;
3391 while (*Resp
== ShellPromptResponseMax
) {
3392 if (ShellGetExecutionBreakFlag()) {
3393 Status
= EFI_ABORTED
;
3396 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3397 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3398 if (EFI_ERROR(Status
)) {
3401 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3402 switch (Key
.UnicodeChar
) {
3405 *Resp
= ShellPromptResponseYes
;
3409 *Resp
= ShellPromptResponseNo
;
3413 *Resp
= ShellPromptResponseAll
;
3417 *Resp
= ShellPromptResponseCancel
;
3422 case ShellPromptResponseTypeEnterContinue
:
3423 case ShellPromptResponseTypeAnyKeyContinue
:
3424 if (Prompt
!= NULL
) {
3425 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3428 // wait for valid response
3430 *Resp
= ShellPromptResponseMax
;
3431 while (*Resp
== ShellPromptResponseMax
) {
3432 if (ShellGetExecutionBreakFlag()) {
3433 Status
= EFI_ABORTED
;
3436 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3437 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3438 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3439 if (EFI_ERROR(Status
)) {
3442 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3443 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3444 *Resp
= ShellPromptResponseContinue
;
3448 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3449 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3450 ASSERT_EFI_ERROR(Status
);
3451 *Resp
= ShellPromptResponseContinue
;
3456 case ShellPromptResponseTypeYesNo
:
3457 if (Prompt
!= NULL
) {
3458 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3461 // wait for valid response
3463 *Resp
= ShellPromptResponseMax
;
3464 while (*Resp
== ShellPromptResponseMax
) {
3465 if (ShellGetExecutionBreakFlag()) {
3466 Status
= EFI_ABORTED
;
3469 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3470 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3471 if (EFI_ERROR(Status
)) {
3474 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3475 switch (Key
.UnicodeChar
) {
3478 *Resp
= ShellPromptResponseYes
;
3482 *Resp
= ShellPromptResponseNo
;
3487 case ShellPromptResponseTypeFreeform
:
3488 if (Prompt
!= NULL
) {
3489 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3492 if (ShellGetExecutionBreakFlag()) {
3493 Status
= EFI_ABORTED
;
3496 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3497 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3498 if (EFI_ERROR(Status
)) {
3501 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3502 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3505 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3506 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3510 // This is the location to add new prompt types.
3511 // If your new type loops remember to add ExecutionBreak support.
3517 if (Response
!= NULL
) {
3520 } else if (Buffer
!= NULL
) {
3527 if (Buffer
!= NULL
) {
3532 ShellPrintEx(-1, -1, L
"\r\n");
3537 Prompt the user and return the resultant answer to the requestor.
3539 This function is the same as ShellPromptForResponse, except that the prompt is
3540 automatically pulled from HII.
3542 @param Type What type of question is asked. This is used to filter the input
3543 to prevent invalid answers to question.
3544 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3545 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3546 @param Response Pointer to Response which will be populated upon return.
3548 @retval EFI_SUCCESS the operation was sucessful.
3549 @return other the operation failed.
3551 @sa ShellPromptForResponse
3555 ShellPromptForResponseHii (
3556 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3557 IN CONST EFI_STRING_ID HiiFormatStringId
,
3558 IN CONST EFI_HANDLE HiiFormatHandle
,
3559 IN OUT VOID
**Response
3565 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3566 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3572 Function to determin if an entire string is a valid number.
3574 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3576 @param[in] String The string to evaluate.
3577 @param[in] ForceHex TRUE - always assume hex.
3578 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3579 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3581 @retval TRUE It is all numeric (dec/hex) characters.
3582 @retval FALSE There is a non-numeric character.
3586 InternalShellIsHexOrDecimalNumber (
3587 IN CONST CHAR16
*String
,
3588 IN CONST BOOLEAN ForceHex
,
3589 IN CONST BOOLEAN StopAtSpace
,
3590 IN CONST BOOLEAN TimeNumbers
3596 // chop off a single negative sign
3598 if (String
!= NULL
&& *String
== L
'-') {
3602 if (String
== NULL
) {
3607 // chop leading zeroes
3609 while(String
!= NULL
&& *String
== L
'0'){
3613 // allow '0x' or '0X', but not 'x' or 'X'
3615 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3616 if (*(String
-1) != L
'0') {
3618 // we got an x without a preceeding 0
3624 } else if (ForceHex
) {
3631 // loop through the remaining characters and use the lib function
3633 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3634 if (TimeNumbers
&& (String
[0] == L
':')) {
3638 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3642 if (!ShellIsDecimalDigitCharacter(*String
)) {
3652 Function to determine if a given filename exists.
3654 @param[in] Name Path to test.
3656 @retval EFI_SUCCESS The Path represents a file.
3657 @retval EFI_NOT_FOUND The Path does not represent a file.
3658 @retval other The path failed to open.
3663 IN CONST CHAR16
*Name
3667 EFI_SHELL_FILE_INFO
*List
;
3669 ASSERT(Name
!= NULL
);
3672 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3673 if (EFI_ERROR(Status
)) {
3677 ShellCloseFileMetaArg(&List
);
3679 return (EFI_SUCCESS
);
3683 Convert a Unicode character to upper case only if
3684 it maps to a valid small-case ASCII character.
3686 This internal function only deal with Unicode character
3687 which maps to a valid small-case ASCII character, i.e.
3688 L'a' to L'z'. For other Unicode character, the input character
3689 is returned directly.
3691 @param Char The character to convert.
3693 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3694 @retval Unchanged Otherwise.
3699 InternalShellCharToUpper (
3703 if (Char
>= L
'a' && Char
<= L
'z') {
3704 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3711 Convert a Unicode character to numerical value.
3713 This internal function only deal with Unicode character
3714 which maps to a valid hexadecimal ASII character, i.e.
3715 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3716 Unicode character, the value returned does not make sense.
3718 @param Char The character to convert.
3720 @return The numerical value converted.
3725 InternalShellHexCharToUintn (
3729 if (ShellIsDecimalDigitCharacter (Char
)) {
3733 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3737 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3739 This function returns a value of type UINTN by interpreting the contents
3740 of the Unicode string specified by String as a hexadecimal number.
3741 The format of the input Unicode string String is:
3743 [spaces][zeros][x][hexadecimal digits].
3745 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3746 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3747 If "x" appears in the input string, it must be prefixed with at least one 0.
3748 The function will ignore the pad space, which includes spaces or tab characters,
3749 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3750 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3751 first valid hexadecimal digit. Then, the function stops at the first character that is
3752 a not a valid hexadecimal character or NULL, whichever one comes first.
3754 If String has only pad spaces, then zero is returned.
3755 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3756 then zero is returned.
3758 @param[in] String A pointer to a Null-terminated Unicode string.
3759 @param[out] Value Upon a successful return the value of the conversion.
3760 @param[in] StopAtSpace FALSE to skip spaces.
3762 @retval EFI_SUCCESS The conversion was successful.
3763 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3764 @retval EFI_DEVICE_ERROR An overflow occured.
3768 InternalShellStrHexToUint64 (
3769 IN CONST CHAR16
*String
,
3771 IN CONST BOOLEAN StopAtSpace
3776 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3777 return (EFI_INVALID_PARAMETER
);
3781 // Ignore the pad spaces (space or tab)
3783 while ((*String
== L
' ') || (*String
== L
'\t')) {
3788 // Ignore leading Zeros after the spaces
3790 while (*String
== L
'0') {
3794 if (InternalShellCharToUpper (*String
) == L
'X') {
3795 if (*(String
- 1) != L
'0') {
3807 // Skip spaces if requested
3809 while (StopAtSpace
&& *String
== L
' ') {
3813 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3815 // If the Hex Number represented by String overflows according
3816 // to the range defined by UINTN, then ASSERT().
3818 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3819 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3820 return (EFI_DEVICE_ERROR
);
3823 Result
= (LShiftU64(Result
, 4));
3824 Result
+= InternalShellHexCharToUintn (*String
);
3828 // stop at spaces if requested
3830 if (StopAtSpace
&& *String
== L
' ') {
3836 return (EFI_SUCCESS
);
3840 Convert a Null-terminated Unicode decimal string to a value of
3843 This function returns a value of type UINT64 by interpreting the contents
3844 of the Unicode string specified by String as a decimal number. The format
3845 of the input Unicode string String is:
3847 [spaces] [decimal digits].
3849 The valid decimal digit character is in the range [0-9]. The
3850 function will ignore the pad space, which includes spaces or
3851 tab characters, before [decimal digits]. The running zero in the
3852 beginning of [decimal digits] will be ignored. Then, the function
3853 stops at the first character that is a not a valid decimal character
3854 or a Null-terminator, whichever one comes first.
3856 If String has only pad spaces, then 0 is returned.
3857 If String has no pad spaces or valid decimal digits,
3860 @param[in] String A pointer to a Null-terminated Unicode string.
3861 @param[out] Value Upon a successful return the value of the conversion.
3862 @param[in] StopAtSpace FALSE to skip spaces.
3864 @retval EFI_SUCCESS The conversion was successful.
3865 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3866 @retval EFI_DEVICE_ERROR An overflow occured.
3870 InternalShellStrDecimalToUint64 (
3871 IN CONST CHAR16
*String
,
3873 IN CONST BOOLEAN StopAtSpace
3878 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3879 return (EFI_INVALID_PARAMETER
);
3883 // Ignore the pad spaces (space or tab)
3885 while ((*String
== L
' ') || (*String
== L
'\t')) {
3890 // Ignore leading Zeros after the spaces
3892 while (*String
== L
'0') {
3899 // Skip spaces if requested
3901 while (StopAtSpace
&& *String
== L
' ') {
3904 while (ShellIsDecimalDigitCharacter (*String
)) {
3906 // If the number represented by String overflows according
3907 // to the range defined by UINT64, then ASSERT().
3910 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3911 return (EFI_DEVICE_ERROR
);
3914 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3918 // Stop at spaces if requested
3920 if (StopAtSpace
&& *String
== L
' ') {
3927 return (EFI_SUCCESS
);
3931 Function to verify and convert a string to its numerical value.
3933 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3935 @param[in] String The string to evaluate.
3936 @param[out] Value Upon a successful return the value of the conversion.
3937 @param[in] ForceHex TRUE - always assume hex.
3938 @param[in] StopAtSpace FALSE to skip spaces.
3940 @retval EFI_SUCCESS The conversion was successful.
3941 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3942 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3946 ShellConvertStringToUint64(
3947 IN CONST CHAR16
*String
,
3949 IN CONST BOOLEAN ForceHex
,
3950 IN CONST BOOLEAN StopAtSpace
3954 CONST CHAR16
*Walker
;
3960 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3963 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3964 return (EFI_INVALID_PARAMETER
);
3967 return (EFI_INVALID_PARAMETER
);
3972 // Chop off leading spaces
3974 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
3977 // make sure we have something left that is numeric.
3979 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
3980 return (EFI_INVALID_PARAMETER
);
3984 // do the conversion.
3986 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
3987 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
3989 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
3992 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
3993 return (EFI_NOT_FOUND
);
3996 if (Value
!= NULL
) {
4004 Function to determin if an entire string is a valid number.
4006 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4008 @param[in] String The string to evaluate.
4009 @param[in] ForceHex TRUE - always assume hex.
4010 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4012 @retval TRUE It is all numeric (dec/hex) characters.
4013 @retval FALSE There is a non-numeric character.
4017 ShellIsHexOrDecimalNumber (
4018 IN CONST CHAR16
*String
,
4019 IN CONST BOOLEAN ForceHex
,
4020 IN CONST BOOLEAN StopAtSpace
4023 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4030 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4031 buffer. The returned buffer must be callee freed.
4033 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4034 maintained and not changed for all operations with the same file.
4036 @param[in] Handle SHELL_FILE_HANDLE to read from.
4037 @param[in, out] Ascii Boolean value for indicating whether the file is
4038 Ascii (TRUE) or UCS2 (FALSE).
4040 @return The line of text from the file.
4041 @retval NULL There was not enough memory available.
4043 @sa ShellFileHandleReadLine
4047 ShellFileHandleReturnLine(
4048 IN SHELL_FILE_HANDLE Handle
,
4049 IN OUT BOOLEAN
*Ascii
4059 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4060 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4061 RetVal
= AllocateZeroPool(Size
);
4062 if (RetVal
== NULL
) {
4065 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4068 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4076 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4078 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4079 maintained and not changed for all operations with the same file.
4081 @param[in] Handle SHELL_FILE_HANDLE to read from.
4082 @param[in, out] Buffer The pointer to buffer to read into.
4083 @param[in, out] Size The pointer to number of bytes in Buffer.
4084 @param[in] Truncate If the buffer is large enough, this has no effect.
4085 If the buffer is is too small and Truncate is TRUE,
4086 the line will be truncated.
4087 If the buffer is is too small and Truncate is FALSE,
4088 then no read will occur.
4090 @param[in, out] Ascii Boolean value for indicating whether the file is
4091 Ascii (TRUE) or UCS2 (FALSE).
4093 @retval EFI_SUCCESS The operation was successful. The line is stored in
4095 @retval EFI_INVALID_PARAMETER Handle was NULL.
4096 @retval EFI_INVALID_PARAMETER Size was NULL.
4097 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4098 Size was updated to the minimum space required.
4102 ShellFileHandleReadLine(
4103 IN SHELL_FILE_HANDLE Handle
,
4104 IN OUT CHAR16
*Buffer
,
4106 IN BOOLEAN Truncate
,
4107 IN OUT BOOLEAN
*Ascii
4114 UINT64 OriginalFilePosition
;
4120 return (EFI_INVALID_PARAMETER
);
4122 if (Buffer
== NULL
) {
4125 *Buffer
= CHAR_NULL
;
4127 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4128 if (OriginalFilePosition
== 0) {
4129 CharSize
= sizeof(CHAR16
);
4130 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4131 ASSERT_EFI_ERROR(Status
);
4132 if (CharBuffer
== gUnicodeFileTag
) {
4136 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4140 for (CountSoFar
= 0;;CountSoFar
++){
4143 CharSize
= sizeof(CHAR8
);
4145 CharSize
= sizeof(CHAR16
);
4147 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4148 if ( EFI_ERROR(Status
)
4150 || (CharBuffer
== L
'\n' && !(*Ascii
))
4151 || (CharBuffer
== '\n' && *Ascii
)
4156 // if we have space save it...
4158 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4159 ASSERT(Buffer
!= NULL
);
4160 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4161 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4166 // if we ran out of space tell when...
4168 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4169 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4171 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4173 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4175 return (EFI_BUFFER_TOO_SMALL
);
4177 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4178 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4185 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4187 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4188 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4189 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4190 the help content only.
4191 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4192 @retval EFI_NOT_FOUND The help data could not be found.
4193 @retval EFI_SUCCESS The operation was successful.
4198 IN CONST CHAR16
*CommandToGetHelpOn
,
4199 IN CONST CHAR16
*SectionToGetHelpOn
,
4200 IN BOOLEAN PrintCommandText
4209 // Get the string to print based
4211 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4214 // make sure we got a valid string
4216 if (EFI_ERROR(Status
)){
4219 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4220 return EFI_NOT_FOUND
;
4224 // Chop off trailing stuff we dont need
4226 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4227 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4231 // Print this out to the console
4233 if (PrintCommandText
) {
4234 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4236 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4239 SHELL_FREE_NON_NULL(OutText
);
4245 Function to delete a file by name
4247 @param[in] FileName Pointer to file name to delete.
4249 @retval EFI_SUCCESS the file was deleted sucessfully
4250 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4252 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4253 @retval EFI_NOT_FOUND The specified file could not be found on the
4254 device or the file system could not be found
4256 @retval EFI_NO_MEDIA The device has no medium.
4257 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4258 medium is no longer supported.
4259 @retval EFI_DEVICE_ERROR The device reported an error.
4260 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4261 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4262 @retval EFI_ACCESS_DENIED The file was opened read only.
4263 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4265 @retval other The file failed to open
4269 ShellDeleteFileByName(
4270 IN CONST CHAR16
*FileName
4274 SHELL_FILE_HANDLE FileHandle
;
4276 Status
= ShellFileExists(FileName
);
4278 if (Status
== EFI_SUCCESS
){
4279 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4280 if (Status
== EFI_SUCCESS
){
4281 Status
= ShellDeleteFile(&FileHandle
);
4290 Cleans off all the quotes in the string.
4292 @param[in] OriginalString pointer to the string to be cleaned.
4293 @param[out] CleanString The new string with all quotes removed.
4294 Memory allocated in the function and free
4297 @retval EFI_SUCCESS The operation was successful.
4301 InternalShellStripQuotes (
4302 IN CONST CHAR16
*OriginalString
,
4303 OUT CHAR16
**CleanString
4308 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4309 return EFI_INVALID_PARAMETER
;
4312 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4313 if (*CleanString
== NULL
) {
4314 return EFI_OUT_OF_RESOURCES
;
4317 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4318 if (*Walker
== L
'\"') {
4319 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));