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
;
679 // ASSERT if FileName is NULL
681 ASSERT(FileName
!= NULL
);
683 if (FileName
== NULL
) {
684 return (EFI_INVALID_PARAMETER
);
687 if (gEfiShellProtocol
!= NULL
) {
688 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
691 // Create only a directory
693 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
694 return ShellCreateDirectory(FileName
, FileHandle
);
698 // Create the directory to create the file in
700 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
701 if (FileName
== NULL
) {
702 return (EFI_OUT_OF_RESOURCES
);
704 PathCleanUpDirectories (FileNameCopy
);
705 if (PathRemoveLastItem (FileNameCopy
)) {
706 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy
, FileHandle
))) {
707 ShellCloseFile (FileHandle
);
710 SHELL_FREE_NON_NULL (FileNameCopy
);
714 // Use UEFI Shell 2.0 method to create the file
716 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
719 if (StrCmp(FileName
, L
"NUL") != 0 && !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
720 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
721 ASSERT(FileInfo
!= NULL
);
722 FileInfo
->Attribute
= Attributes
;
723 Status2
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
725 if (EFI_ERROR (Status2
)) {
726 gEfiShellProtocol
->CloseFile(*FileHandle
);
733 // Using EFI Shell version
734 // this means convert name to path and call that function
735 // since this will use EFI method again that will open it.
737 ASSERT(mEfiShellEnvironment2
!= NULL
);
738 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
739 if (FilePath
!= NULL
) {
740 return (ShellOpenFileByDevicePath(&FilePath
,
746 return (EFI_DEVICE_ERROR
);
749 This function create a directory
751 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
752 otherwise, the Filehandle is NULL. If the directory already existed, this
753 function opens the existing directory.
755 @param DirectoryName pointer to directory name
756 @param FileHandle pointer to the file handle.
758 @retval EFI_SUCCESS The information was set.
759 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
760 @retval EFI_UNSUPPORTED Could not open the file path.
761 @retval EFI_NOT_FOUND The specified file could not be found on the
762 device or the file system could not be found
764 @retval EFI_NO_MEDIA The device has no medium.
765 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
766 medium is no longer supported.
767 @retval EFI_DEVICE_ERROR The device reported an error.
768 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
769 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
770 @retval EFI_ACCESS_DENIED The file was opened read only.
771 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
773 @retval EFI_VOLUME_FULL The volume is full.
774 @sa ShellOpenFileByName
778 ShellCreateDirectory(
779 IN CONST CHAR16
*DirectoryName
,
780 OUT SHELL_FILE_HANDLE
*FileHandle
783 if (gEfiShellProtocol
!= NULL
) {
785 // Use UEFI Shell 2.0 method
787 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
792 return (ShellOpenFileByName(DirectoryName
,
794 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
801 This function reads information from an opened file.
803 If FileHandle is not a directory, the function reads the requested number of
804 bytes from the file at the file's current position and returns them in Buffer.
805 If the read goes beyond the end of the file, the read length is truncated to the
806 end of the file. The file's current position is increased by the number of bytes
807 returned. If FileHandle is a directory, the function reads the directory entry
808 at the file's current position and returns the entry in Buffer. If the Buffer
809 is not large enough to hold the current directory entry, then
810 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
811 BufferSize is set to be the size of the buffer needed to read the entry. On
812 success, the current position is updated to the next directory entry. If there
813 are no more directory entries, the read returns a zero-length buffer.
814 EFI_FILE_INFO is the structure returned as the directory entry.
816 @param FileHandle the opened file handle
817 @param BufferSize on input the size of buffer in bytes. on return
818 the number of bytes written.
819 @param Buffer the buffer to put read data into.
821 @retval EFI_SUCCESS Data was read.
822 @retval EFI_NO_MEDIA The device has no media.
823 @retval EFI_DEVICE_ERROR The device reported an error.
824 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
825 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
832 IN SHELL_FILE_HANDLE FileHandle
,
833 IN OUT UINTN
*BufferSize
,
837 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
842 Write data to a file.
844 This function writes the specified number of bytes to the file at the current
845 file position. The current file position is advanced the actual number of bytes
846 written, which is returned in BufferSize. Partial writes only occur when there
847 has been a data error during the write attempt (such as "volume space full").
848 The file is automatically grown to hold the data if required. Direct writes to
849 opened directories are not supported.
851 @param FileHandle The opened file for writing
852 @param BufferSize on input the number of bytes in Buffer. On output
853 the number of bytes written.
854 @param Buffer the buffer containing data to write is stored.
856 @retval EFI_SUCCESS Data was written.
857 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
858 @retval EFI_NO_MEDIA The device has no media.
859 @retval EFI_DEVICE_ERROR The device reported an error.
860 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
861 @retval EFI_WRITE_PROTECTED The device is write-protected.
862 @retval EFI_ACCESS_DENIED The file was open for read only.
863 @retval EFI_VOLUME_FULL The volume is full.
868 IN SHELL_FILE_HANDLE FileHandle
,
869 IN OUT UINTN
*BufferSize
,
873 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
877 Close an open file handle.
879 This function closes a specified file handle. All "dirty" cached file data is
880 flushed to the device, and the file is closed. In all cases the handle is
883 @param FileHandle the file handle to close.
885 @retval EFI_SUCCESS the file handle was closed sucessfully.
890 IN SHELL_FILE_HANDLE
*FileHandle
893 return (FileFunctionMap
.CloseFile(*FileHandle
));
897 Delete a file and close the handle
899 This function closes and deletes a file. In all cases the file handle is closed.
900 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
901 returned, but the handle is still closed.
903 @param FileHandle the file handle to delete
905 @retval EFI_SUCCESS the file was closed sucessfully
906 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
908 @retval INVALID_PARAMETER One of the parameters has an invalid value.
913 IN SHELL_FILE_HANDLE
*FileHandle
916 return (FileFunctionMap
.DeleteFile(*FileHandle
));
920 Set the current position in a file.
922 This function sets the current file position for the handle to the position
923 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
924 absolute positioning is supported, and seeking past the end of the file is
925 allowed (a subsequent write would grow the file). Seeking to position
926 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
927 If FileHandle is a directory, the only position that may be set is zero. This
928 has the effect of starting the read process of the directory entries over.
930 @param FileHandle The file handle on which the position is being set
931 @param Position Byte position from begining of file
933 @retval EFI_SUCCESS Operation completed sucessfully.
934 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
936 @retval INVALID_PARAMETER One of the parameters has an invalid value.
940 ShellSetFilePosition (
941 IN SHELL_FILE_HANDLE FileHandle
,
945 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
949 Gets a file's current position
951 This function retrieves the current file position for the file handle. For
952 directories, the current file position has no meaning outside of the file
953 system driver and as such the operation is not supported. An error is returned
954 if FileHandle is a directory.
956 @param FileHandle The open file handle on which to get the position.
957 @param Position Byte position from begining of file.
959 @retval EFI_SUCCESS the operation completed sucessfully.
960 @retval INVALID_PARAMETER One of the parameters has an invalid value.
961 @retval EFI_UNSUPPORTED the request is not valid on directories.
965 ShellGetFilePosition (
966 IN SHELL_FILE_HANDLE FileHandle
,
970 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
973 Flushes data on a file
975 This function flushes all modified data associated with a file to a device.
977 @param FileHandle The file handle on which to flush data
979 @retval EFI_SUCCESS The data was flushed.
980 @retval EFI_NO_MEDIA The device has no media.
981 @retval EFI_DEVICE_ERROR The device reported an error.
982 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
983 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
984 @retval EFI_ACCESS_DENIED The file was opened for read only.
989 IN SHELL_FILE_HANDLE FileHandle
992 return (FileFunctionMap
.FlushFile(FileHandle
));
995 /** Retrieve first entry from a directory.
997 This function takes an open directory handle and gets information from the
998 first entry in the directory. A buffer is allocated to contain
999 the information and a pointer to the buffer is returned in *Buffer. The
1000 caller can use ShellFindNextFile() to get subsequent directory entries.
1002 The buffer will be freed by ShellFindNextFile() when the last directory
1003 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1004 when finished with it.
1006 @param[in] DirHandle The file handle of the directory to search.
1007 @param[out] Buffer The pointer to the buffer for the file's information.
1009 @retval EFI_SUCCESS Found the first file.
1010 @retval EFI_NOT_FOUND Cannot find the directory.
1011 @retval EFI_NO_MEDIA The device has no media.
1012 @retval EFI_DEVICE_ERROR The device reported an error.
1013 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1014 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1019 ShellFindFirstFile (
1020 IN SHELL_FILE_HANDLE DirHandle
,
1021 OUT EFI_FILE_INFO
**Buffer
1025 // pass to file handle lib
1027 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1029 /** Retrieve next entries from a directory.
1031 To use this function, the caller must first call the ShellFindFirstFile()
1032 function to get the first directory entry. Subsequent directory entries are
1033 retrieved by using the ShellFindNextFile() function. This function can
1034 be called several times to get each entry from the directory. If the call of
1035 ShellFindNextFile() retrieved the last directory entry, the next call of
1036 this function will set *NoFile to TRUE and free the buffer.
1038 @param[in] DirHandle The file handle of the directory.
1039 @param[out] Buffer The pointer to buffer for file's information.
1040 @param[out] NoFile The pointer to boolean when last file is found.
1042 @retval EFI_SUCCESS Found the next file, or reached last file
1043 @retval EFI_NO_MEDIA The device has no media.
1044 @retval EFI_DEVICE_ERROR The device reported an error.
1045 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1050 IN SHELL_FILE_HANDLE DirHandle
,
1051 OUT EFI_FILE_INFO
*Buffer
,
1056 // pass to file handle lib
1058 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1061 Retrieve the size of a file.
1063 if FileHandle is NULL then ASSERT()
1064 if Size is NULL then ASSERT()
1066 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1069 @param FileHandle file handle from which size is retrieved
1070 @param Size pointer to size
1072 @retval EFI_SUCCESS operation was completed sucessfully
1073 @retval EFI_DEVICE_ERROR cannot access the file
1078 IN SHELL_FILE_HANDLE FileHandle
,
1082 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1085 Retrieves the status of the break execution flag
1087 this function is useful to check whether the application is being asked to halt by the shell.
1089 @retval TRUE the execution break is enabled
1090 @retval FALSE the execution break is not enabled
1094 ShellGetExecutionBreakFlag(
1099 // Check for UEFI Shell 2.0 protocols
1101 if (gEfiShellProtocol
!= NULL
) {
1104 // We are using UEFI Shell 2.0; see if the event has been triggered
1106 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1113 // using EFI Shell; call the function to check
1115 if (mEfiShellEnvironment2
!= NULL
) {
1116 return (mEfiShellEnvironment2
->GetExecutionBreak());
1122 return the value of an environment variable
1124 this function gets the value of the environment variable set by the
1125 ShellSetEnvironmentVariable function
1127 @param EnvKey The key name of the environment variable.
1129 @retval NULL the named environment variable does not exist.
1130 @return != NULL pointer to the value of the environment variable
1134 ShellGetEnvironmentVariable (
1135 IN CONST CHAR16
*EnvKey
1139 // Check for UEFI Shell 2.0 protocols
1141 if (gEfiShellProtocol
!= NULL
) {
1142 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1146 // Check for EFI shell
1148 if (mEfiShellEnvironment2
!= NULL
) {
1149 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1155 set the value of an environment variable
1157 This function changes the current value of the specified environment variable. If the
1158 environment variable exists and the Value is an empty string, then the environment
1159 variable is deleted. If the environment variable exists and the Value is not an empty
1160 string, then the value of the environment variable is changed. If the environment
1161 variable does not exist and the Value is an empty string, there is no action. If the
1162 environment variable does not exist and the Value is a non-empty string, then the
1163 environment variable is created and assigned the specified value.
1165 This is not supported pre-UEFI Shell 2.0.
1167 @param EnvKey The key name of the environment variable.
1168 @param EnvVal The Value of the environment variable
1169 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1171 @retval EFI_SUCCESS the operation was completed sucessfully
1172 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1176 ShellSetEnvironmentVariable (
1177 IN CONST CHAR16
*EnvKey
,
1178 IN CONST CHAR16
*EnvVal
,
1183 // Check for UEFI Shell 2.0 protocols
1185 if (gEfiShellProtocol
!= NULL
) {
1186 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1190 // This feature does not exist under EFI shell
1192 return (EFI_UNSUPPORTED
);
1196 Cause the shell to parse and execute a command line.
1198 This function creates a nested instance of the shell and executes the specified
1199 command (CommandLine) with the specified environment (Environment). Upon return,
1200 the status code returned by the specified command is placed in StatusCode.
1201 If Environment is NULL, then the current environment is used and all changes made
1202 by the commands executed will be reflected in the current environment. If the
1203 Environment is non-NULL, then the changes made will be discarded.
1204 The CommandLine is executed from the current working directory on the current
1207 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1208 environment. The values pointed to by the parameters will be unchanged by the
1209 ShellExecute() function. The Output parameter has no effect in a
1210 UEFI Shell 2.0 environment.
1212 @param[in] ParentHandle The parent image starting the operation.
1213 @param[in] CommandLine The pointer to a NULL terminated command line.
1214 @param[in] Output True to display debug output. False to hide it.
1215 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1216 in the form "x=y". If NULL, the current set is used.
1217 @param[out] Status The status of the run command line.
1219 @retval EFI_SUCCESS The operation completed sucessfully. Status
1220 contains the status code returned.
1221 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1222 @retval EFI_OUT_OF_RESOURCES Out of resources.
1223 @retval EFI_UNSUPPORTED The operation is not allowed.
1228 IN EFI_HANDLE
*ParentHandle
,
1229 IN CHAR16
*CommandLine OPTIONAL
,
1230 IN BOOLEAN Output OPTIONAL
,
1231 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1232 OUT EFI_STATUS
*Status OPTIONAL
1235 EFI_STATUS CmdStatus
;
1237 // Check for UEFI Shell 2.0 protocols
1239 if (gEfiShellProtocol
!= NULL
) {
1241 // Call UEFI Shell 2.0 version (not using Output parameter)
1243 return (gEfiShellProtocol
->Execute(ParentHandle
,
1245 EnvironmentVariables
,
1250 // Check for EFI shell
1252 if (mEfiShellEnvironment2
!= NULL
) {
1254 // Call EFI Shell version.
1255 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1257 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1261 // No Status output parameter so just use the returned status
1263 if (Status
!= NULL
) {
1264 *Status
= CmdStatus
;
1267 // If there was an error, we can't tell if it was from the command or from
1268 // the Execute() function, so we'll just assume the shell ran successfully
1269 // and the error came from the command.
1274 return (EFI_UNSUPPORTED
);
1278 Retreives the current directory path
1280 If the DeviceName is NULL, it returns the current device's current directory
1281 name. If the DeviceName is not NULL, it returns the current directory name
1284 Note that the current directory string should exclude the tailing backslash character.
1286 @param DeviceName the name of the drive to get directory on
1288 @retval NULL the directory does not exist
1289 @return != NULL the directory
1293 ShellGetCurrentDir (
1294 IN CHAR16
* CONST DeviceName OPTIONAL
1298 // Check for UEFI Shell 2.0 protocols
1300 if (gEfiShellProtocol
!= NULL
) {
1301 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1305 // Check for EFI shell
1307 if (mEfiShellEnvironment2
!= NULL
) {
1308 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1314 sets (enabled or disabled) the page break mode
1316 when page break mode is enabled the screen will stop scrolling
1317 and wait for operator input before scrolling a subsequent screen.
1319 @param CurrentState TRUE to enable and FALSE to disable
1323 ShellSetPageBreakMode (
1324 IN BOOLEAN CurrentState
1328 // check for enabling
1330 if (CurrentState
!= 0x00) {
1332 // check for UEFI Shell 2.0
1334 if (gEfiShellProtocol
!= NULL
) {
1336 // Enable with UEFI 2.0 Shell
1338 gEfiShellProtocol
->EnablePageBreak();
1342 // Check for EFI shell
1344 if (mEfiShellEnvironment2
!= NULL
) {
1346 // Enable with EFI Shell
1348 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1354 // check for UEFI Shell 2.0
1356 if (gEfiShellProtocol
!= NULL
) {
1358 // Disable with UEFI 2.0 Shell
1360 gEfiShellProtocol
->DisablePageBreak();
1364 // Check for EFI shell
1366 if (mEfiShellEnvironment2
!= NULL
) {
1368 // Disable with EFI Shell
1370 mEfiShellEnvironment2
->DisablePageBreak ();
1378 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1379 /// This allows for the struct to be populated.
1386 SHELL_FILE_HANDLE Handle
;
1387 EFI_FILE_INFO
*Info
;
1388 } EFI_SHELL_FILE_INFO_NO_CONST
;
1391 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1393 if OldStyleFileList is NULL then ASSERT()
1395 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1396 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1397 the ShellCloseFileMetaArg function.
1399 @param[in] FileList the EFI shell list type
1400 @param[in, out] ListHead the list to add to
1402 @retval the resultant head of the double linked new format list;
1406 InternalShellConvertFileListType (
1407 IN LIST_ENTRY
*FileList
,
1408 IN OUT LIST_ENTRY
*ListHead
1411 SHELL_FILE_ARG
*OldInfo
;
1413 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1418 ASSERT(FileList
!= NULL
);
1419 ASSERT(ListHead
!= NULL
);
1422 // enumerate through each member of the old list and copy
1424 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1425 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1426 ASSERT(OldInfo
!= NULL
);
1429 // Skip ones that failed to open...
1431 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1436 // make sure the old list was valid
1438 ASSERT(OldInfo
->Info
!= NULL
);
1439 ASSERT(OldInfo
->FullName
!= NULL
);
1440 ASSERT(OldInfo
->FileName
!= NULL
);
1443 // allocate a new EFI_SHELL_FILE_INFO object
1445 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1446 if (NewInfo
== NULL
) {
1447 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1453 // copy the simple items
1455 NewInfo
->Handle
= OldInfo
->Handle
;
1456 NewInfo
->Status
= OldInfo
->Status
;
1458 // old shell checks for 0 not NULL
1459 OldInfo
->Handle
= 0;
1462 // allocate new space to copy strings and structure
1464 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1465 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1466 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1469 // make sure all the memory allocations were sucessful
1471 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1473 // Free the partially allocated new node
1475 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1476 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1477 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1478 SHELL_FREE_NON_NULL(NewInfo
);
1481 // Free the previously converted stuff
1483 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1489 // add that to the list
1491 InsertTailList(ListHead
, &NewInfo
->Link
);
1496 Opens a group of files based on a path.
1498 This function uses the Arg to open all the matching files. Each matched
1499 file has a SHELL_FILE_INFO structure to record the file information. These
1500 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1501 structures from ListHead to access each file. This function supports wildcards
1502 and will process '?' and '*' as such. the list must be freed with a call to
1503 ShellCloseFileMetaArg().
1505 If you are NOT appending to an existing list *ListHead must be NULL. If
1506 *ListHead is NULL then it must be callee freed.
1508 @param Arg pointer to path string
1509 @param OpenMode mode to open files with
1510 @param ListHead head of linked list of results
1512 @retval EFI_SUCCESS the operation was sucessful and the list head
1513 contains the list of opened files
1514 @return != EFI_SUCCESS the operation failed
1516 @sa InternalShellConvertFileListType
1520 ShellOpenFileMetaArg (
1523 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1527 LIST_ENTRY mOldStyleFileList
;
1528 CHAR16
*CleanFilePathStr
;
1531 // ASSERT that Arg and ListHead are not NULL
1533 ASSERT(Arg
!= NULL
);
1534 ASSERT(ListHead
!= NULL
);
1536 CleanFilePathStr
= NULL
;
1538 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1539 if (EFI_ERROR (Status
)) {
1544 // Check for UEFI Shell 2.0 protocols
1546 if (gEfiShellProtocol
!= NULL
) {
1547 if (*ListHead
== NULL
) {
1548 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1549 if (*ListHead
== NULL
) {
1550 FreePool(CleanFilePathStr
);
1551 return (EFI_OUT_OF_RESOURCES
);
1553 InitializeListHead(&((*ListHead
)->Link
));
1555 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1558 if (EFI_ERROR(Status
)) {
1559 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1561 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1563 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1564 FreePool(*ListHead
);
1565 FreePool(CleanFilePathStr
);
1567 return (EFI_NOT_FOUND
);
1569 FreePool(CleanFilePathStr
);
1574 // Check for EFI shell
1576 if (mEfiShellEnvironment2
!= NULL
) {
1578 // make sure the list head is initialized
1580 InitializeListHead(&mOldStyleFileList
);
1583 // Get the EFI Shell list of files
1585 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1586 if (EFI_ERROR(Status
)) {
1588 FreePool(CleanFilePathStr
);
1592 if (*ListHead
== NULL
) {
1593 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1594 if (*ListHead
== NULL
) {
1595 FreePool(CleanFilePathStr
);
1596 return (EFI_OUT_OF_RESOURCES
);
1598 InitializeListHead(&((*ListHead
)->Link
));
1602 // Convert that to equivalent of UEFI Shell 2.0 structure
1604 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1607 // Free the EFI Shell version that was converted.
1609 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1611 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1612 FreePool(*ListHead
);
1614 Status
= EFI_NOT_FOUND
;
1616 FreePool(CleanFilePathStr
);
1620 FreePool(CleanFilePathStr
);
1621 return (EFI_UNSUPPORTED
);
1624 Free the linked list returned from ShellOpenFileMetaArg.
1626 if ListHead is NULL then ASSERT().
1628 @param ListHead the pointer to free.
1630 @retval EFI_SUCCESS the operation was sucessful.
1634 ShellCloseFileMetaArg (
1635 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1641 // ASSERT that ListHead is not NULL
1643 ASSERT(ListHead
!= NULL
);
1646 // Check for UEFI Shell 2.0 protocols
1648 if (gEfiShellProtocol
!= NULL
) {
1649 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1650 } else if (mEfiShellEnvironment2
!= NULL
) {
1652 // Since this is EFI Shell version we need to free our internally made copy
1655 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1656 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1657 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1658 RemoveEntryList(Node
);
1659 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1660 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1661 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1662 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1663 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1665 SHELL_FREE_NON_NULL(*ListHead
);
1669 return (EFI_UNSUPPORTED
);
1673 Find a file by searching the CWD and then the path.
1675 If FileName is NULL then ASSERT.
1677 If the return value is not NULL then the memory must be caller freed.
1679 @param FileName Filename string.
1681 @retval NULL the file was not found
1682 @return !NULL the full path to the file.
1687 IN CONST CHAR16
*FileName
1691 SHELL_FILE_HANDLE Handle
;
1695 CONST CHAR16
*Walker
;
1702 // First make sure its not an absolute path.
1704 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1705 if (!EFI_ERROR(Status
)){
1706 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1707 ASSERT(RetVal
== NULL
);
1708 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1709 ShellCloseFile(&Handle
);
1712 ShellCloseFile(&Handle
);
1716 Path
= ShellGetEnvironmentVariable(L
"cwd");
1718 Size
= StrSize(Path
) + sizeof(CHAR16
);
1719 Size
+= StrSize(FileName
);
1720 TestPath
= AllocateZeroPool(Size
);
1721 if (TestPath
== NULL
) {
1724 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), Path
);
1725 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1726 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1727 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1728 if (!EFI_ERROR(Status
)){
1729 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1730 ASSERT(RetVal
== NULL
);
1731 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1732 ShellCloseFile(&Handle
);
1736 ShellCloseFile(&Handle
);
1741 Path
= ShellGetEnvironmentVariable(L
"path");
1743 Size
= StrSize(Path
)+sizeof(CHAR16
);
1744 Size
+= StrSize(FileName
);
1745 TestPath
= AllocateZeroPool(Size
);
1746 if (TestPath
== NULL
) {
1749 Walker
= (CHAR16
*)Path
;
1751 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1752 if (TestPath
!= NULL
) {
1753 TempChar
= StrStr(TestPath
, L
";");
1754 if (TempChar
!= NULL
) {
1755 *TempChar
= CHAR_NULL
;
1757 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1758 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1760 if (FileName
[0] == L
'\\') {
1763 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1764 if (StrStr(Walker
, L
";") != NULL
) {
1765 Walker
= StrStr(Walker
, L
";") + 1;
1769 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1770 if (!EFI_ERROR(Status
)){
1771 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1772 ASSERT(RetVal
== NULL
);
1773 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1774 ShellCloseFile(&Handle
);
1777 ShellCloseFile(&Handle
);
1781 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1788 Find a file by searching the CWD and then the path with a variable set of file
1789 extensions. If the file is not found it will append each extension in the list
1790 in the order provided and return the first one that is successful.
1792 If FileName is NULL, then ASSERT.
1793 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1795 If the return value is not NULL then the memory must be caller freed.
1797 @param[in] FileName Filename string.
1798 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1800 @retval NULL The file was not found.
1801 @retval !NULL The path to the file.
1805 ShellFindFilePathEx (
1806 IN CONST CHAR16
*FileName
,
1807 IN CONST CHAR16
*FileExtension
1812 CONST CHAR16
*ExtensionWalker
;
1817 ASSERT(FileName
!= NULL
);
1818 if (FileExtension
== NULL
) {
1819 return (ShellFindFilePath(FileName
));
1821 RetVal
= ShellFindFilePath(FileName
);
1822 if (RetVal
!= NULL
) {
1825 Size
= StrSize(FileName
);
1826 Size
+= StrSize(FileExtension
);
1827 TestPath
= AllocateZeroPool(Size
);
1828 if (TestPath
== NULL
) {
1831 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1832 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1833 if (ExtensionWalker
!= NULL
) {
1834 StrCatS(TestPath
, Size
/sizeof(CHAR16
), ExtensionWalker
);
1836 TempChar
= StrStr(TestPath
, L
";");
1837 if (TempChar
!= NULL
) {
1838 *TempChar
= CHAR_NULL
;
1840 RetVal
= ShellFindFilePath(TestPath
);
1841 if (RetVal
!= NULL
) {
1844 ASSERT(ExtensionWalker
!= NULL
);
1845 TempChar2
= StrStr(ExtensionWalker
, L
";");
1854 SHELL_PARAM_TYPE Type
;
1856 UINTN OriginalPosition
;
1857 } SHELL_PARAM_PACKAGE
;
1860 Checks the list of valid arguments and returns TRUE if the item was found. If the
1861 return value is TRUE then the type parameter is set also.
1863 if CheckList is NULL then ASSERT();
1864 if Name is NULL then ASSERT();
1865 if Type is NULL then ASSERT();
1867 @param Name pointer to Name of parameter found
1868 @param CheckList List to check against
1869 @param Type pointer to type of parameter if it was found
1871 @retval TRUE the Parameter was found. Type is valid.
1872 @retval FALSE the Parameter was not found. Type is not valid.
1876 InternalIsOnCheckList (
1877 IN CONST CHAR16
*Name
,
1878 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1879 OUT SHELL_PARAM_TYPE
*Type
1882 SHELL_PARAM_ITEM
*TempListItem
;
1886 // ASSERT that all 3 pointer parameters aren't NULL
1888 ASSERT(CheckList
!= NULL
);
1889 ASSERT(Type
!= NULL
);
1890 ASSERT(Name
!= NULL
);
1893 // question mark and page break mode are always supported
1895 if ((StrCmp(Name
, L
"-?") == 0) ||
1896 (StrCmp(Name
, L
"-b") == 0)
1903 // Enumerate through the list
1905 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1907 // If the Type is TypeStart only check the first characters of the passed in param
1908 // If it matches set the type and return TRUE
1910 if (TempListItem
->Type
== TypeStart
) {
1911 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1912 *Type
= TempListItem
->Type
;
1916 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1917 if (TempString
!= NULL
) {
1918 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1919 *Type
= TempListItem
->Type
;
1920 FreePool(TempString
);
1923 FreePool(TempString
);
1925 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1926 *Type
= TempListItem
->Type
;
1934 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1936 @param[in] Name pointer to Name of parameter found
1937 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1938 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1940 @retval TRUE the Parameter is a flag.
1941 @retval FALSE the Parameter not a flag.
1946 IN CONST CHAR16
*Name
,
1947 IN CONST BOOLEAN AlwaysAllowNumbers
,
1948 IN CONST BOOLEAN TimeNumbers
1952 // ASSERT that Name isn't NULL
1954 ASSERT(Name
!= NULL
);
1957 // If we accept numbers then dont return TRUE. (they will be values)
1959 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
1964 // If the Name has a /, +, or - as the first character return TRUE
1966 if ((Name
[0] == L
'/') ||
1967 (Name
[0] == L
'-') ||
1976 Checks the command line arguments passed against the list of valid ones.
1978 If no initialization is required, then return RETURN_SUCCESS.
1980 @param[in] CheckList pointer to list of parameters to check
1981 @param[out] CheckPackage pointer to pointer to list checked values
1982 @param[out] ProblemParam optional pointer to pointer to unicode string for
1983 the paramater that caused failure. If used then the
1984 caller is responsible for freeing the memory.
1985 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1986 @param[in] Argv pointer to array of parameters
1987 @param[in] Argc Count of parameters in Argv
1988 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
1990 @retval EFI_SUCCESS The operation completed sucessfully.
1991 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1992 @retval EFI_INVALID_PARAMETER A parameter was invalid
1993 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1994 duplicated. the duplicated command line argument
1995 was returned in ProblemParam if provided.
1996 @retval EFI_NOT_FOUND a argument required a value that was missing.
1997 the invalid command line argument was returned in
1998 ProblemParam if provided.
2002 InternalCommandLineParse (
2003 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2004 OUT LIST_ENTRY
**CheckPackage
,
2005 OUT CHAR16
**ProblemParam OPTIONAL
,
2006 IN BOOLEAN AutoPageBreak
,
2007 IN CONST CHAR16
**Argv
,
2009 IN BOOLEAN AlwaysAllowNumbers
2013 SHELL_PARAM_TYPE CurrentItemType
;
2014 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2018 CONST CHAR16
*TempPointer
;
2019 UINTN CurrentValueSize
;
2021 CurrentItemPackage
= NULL
;
2027 // If there is only 1 item we dont need to do anything
2030 *CheckPackage
= NULL
;
2031 return (EFI_SUCCESS
);
2037 ASSERT(CheckList
!= NULL
);
2038 ASSERT(Argv
!= NULL
);
2041 // initialize the linked list
2043 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2044 if (*CheckPackage
== NULL
) {
2045 return (EFI_OUT_OF_RESOURCES
);
2048 InitializeListHead(*CheckPackage
);
2051 // loop through each of the arguments
2053 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2054 if (Argv
[LoopCounter
] == NULL
) {
2056 // do nothing for NULL argv
2058 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2060 // We might have leftover if last parameter didnt have optional value
2062 if (GetItemValue
!= 0) {
2064 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2069 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2070 if (CurrentItemPackage
== NULL
) {
2071 ShellCommandLineFreeVarList(*CheckPackage
);
2072 *CheckPackage
= NULL
;
2073 return (EFI_OUT_OF_RESOURCES
);
2075 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2076 if (CurrentItemPackage
->Name
== NULL
) {
2077 ShellCommandLineFreeVarList(*CheckPackage
);
2078 *CheckPackage
= NULL
;
2079 return (EFI_OUT_OF_RESOURCES
);
2081 CurrentItemPackage
->Type
= CurrentItemType
;
2082 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2083 CurrentItemPackage
->Value
= NULL
;
2086 // Does this flag require a value
2088 switch (CurrentItemPackage
->Type
) {
2090 // possibly trigger the next loop(s) to populate the value of this item
2097 case TypeDoubleValue
:
2102 GetItemValue
= (UINTN
)(-1);
2107 // this item has no value expected; we are done
2109 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2110 ASSERT(GetItemValue
== 0);
2113 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, (BOOLEAN
)(CurrentItemPackage
->Type
== TypeTimeValue
))) {
2115 // get the item VALUE for a previous flag
2117 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2118 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2119 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2120 if (ValueSize
== 0) {
2121 StrCpyS( CurrentItemPackage
->Value
,
2122 CurrentValueSize
/sizeof(CHAR16
),
2126 StrCatS( CurrentItemPackage
->Value
,
2127 CurrentValueSize
/sizeof(CHAR16
),
2130 StrCatS( CurrentItemPackage
->Value
,
2131 CurrentValueSize
/sizeof(CHAR16
),
2135 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2138 if (GetItemValue
== 0) {
2139 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2141 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2143 // add this one as a non-flag
2146 TempPointer
= Argv
[LoopCounter
];
2147 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2148 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2149 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2153 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2154 if (CurrentItemPackage
== NULL
) {
2155 ShellCommandLineFreeVarList(*CheckPackage
);
2156 *CheckPackage
= NULL
;
2157 return (EFI_OUT_OF_RESOURCES
);
2159 CurrentItemPackage
->Name
= NULL
;
2160 CurrentItemPackage
->Type
= TypePosition
;
2161 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2162 if (CurrentItemPackage
->Value
== NULL
) {
2163 ShellCommandLineFreeVarList(*CheckPackage
);
2164 *CheckPackage
= NULL
;
2165 return (EFI_OUT_OF_RESOURCES
);
2167 CurrentItemPackage
->OriginalPosition
= Count
++;
2168 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2171 // this was a non-recognised flag... error!
2173 if (ProblemParam
!= NULL
) {
2174 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2176 ShellCommandLineFreeVarList(*CheckPackage
);
2177 *CheckPackage
= NULL
;
2178 return (EFI_VOLUME_CORRUPTED
);
2181 if (GetItemValue
!= 0) {
2183 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2186 // support for AutoPageBreak
2188 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2189 ShellSetPageBreakMode(TRUE
);
2191 return (EFI_SUCCESS
);
2195 Checks the command line arguments passed against the list of valid ones.
2196 Optionally removes NULL values first.
2198 If no initialization is required, then return RETURN_SUCCESS.
2200 @param[in] CheckList The pointer to list of parameters to check.
2201 @param[out] CheckPackage The package of checked values.
2202 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2203 the paramater that caused failure.
2204 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2205 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2207 @retval EFI_SUCCESS The operation completed sucessfully.
2208 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2209 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2210 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2211 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2212 of the command line arguments was returned in
2213 ProblemParam if provided.
2214 @retval EFI_NOT_FOUND A argument required a value that was missing.
2215 The invalid command line argument was returned in
2216 ProblemParam if provided.
2220 ShellCommandLineParseEx (
2221 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2222 OUT LIST_ENTRY
**CheckPackage
,
2223 OUT CHAR16
**ProblemParam OPTIONAL
,
2224 IN BOOLEAN AutoPageBreak
,
2225 IN BOOLEAN AlwaysAllowNumbers
2229 // ASSERT that CheckList and CheckPackage aren't NULL
2231 ASSERT(CheckList
!= NULL
);
2232 ASSERT(CheckPackage
!= NULL
);
2235 // Check for UEFI Shell 2.0 protocols
2237 if (gEfiShellParametersProtocol
!= NULL
) {
2238 return (InternalCommandLineParse(CheckList
,
2242 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2243 gEfiShellParametersProtocol
->Argc
,
2244 AlwaysAllowNumbers
));
2248 // ASSERT That EFI Shell is not required
2250 ASSERT (mEfiShellInterface
!= NULL
);
2251 return (InternalCommandLineParse(CheckList
,
2255 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2256 mEfiShellInterface
->Argc
,
2257 AlwaysAllowNumbers
));
2261 Frees shell variable list that was returned from ShellCommandLineParse.
2263 This function will free all the memory that was used for the CheckPackage
2264 list of postprocessed shell arguments.
2266 this function has no return value.
2268 if CheckPackage is NULL, then return
2270 @param CheckPackage the list to de-allocate
2274 ShellCommandLineFreeVarList (
2275 IN LIST_ENTRY
*CheckPackage
2281 // check for CheckPackage == NULL
2283 if (CheckPackage
== NULL
) {
2288 // for each node in the list
2290 for ( Node
= GetFirstNode(CheckPackage
)
2291 ; !IsListEmpty(CheckPackage
)
2292 ; Node
= GetFirstNode(CheckPackage
)
2295 // Remove it from the list
2297 RemoveEntryList(Node
);
2300 // if it has a name free the name
2302 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2303 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2307 // if it has a value free the value
2309 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2310 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2314 // free the node structure
2316 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2319 // free the list head node
2321 FreePool(CheckPackage
);
2324 Checks for presence of a flag parameter
2326 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2328 if CheckPackage is NULL then return FALSE.
2329 if KeyString is NULL then ASSERT()
2331 @param CheckPackage The package of parsed command line arguments
2332 @param KeyString the Key of the command line argument to check for
2334 @retval TRUE the flag is on the command line
2335 @retval FALSE the flag is not on the command line
2339 ShellCommandLineGetFlag (
2340 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2341 IN CONST CHAR16
* CONST KeyString
2348 // return FALSE for no package or KeyString is NULL
2350 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2355 // enumerate through the list of parametrs
2357 for ( Node
= GetFirstNode(CheckPackage
)
2358 ; !IsNull (CheckPackage
, Node
)
2359 ; Node
= GetNextNode(CheckPackage
, Node
)
2362 // If the Name matches, return TRUE (and there may be NULL name)
2364 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2366 // If Type is TypeStart then only compare the begining of the strings
2368 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2369 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2373 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2374 if (TempString
!= NULL
) {
2375 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2376 FreePool(TempString
);
2379 FreePool(TempString
);
2381 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2389 Returns value from command line argument.
2391 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2393 If CheckPackage is NULL, then return NULL.
2395 @param[in] CheckPackage The package of parsed command line arguments.
2396 @param[in] KeyString The Key of the command line argument to check for.
2398 @retval NULL The flag is not on the command line.
2399 @retval !=NULL The pointer to unicode string of the value.
2403 ShellCommandLineGetValue (
2404 IN CONST LIST_ENTRY
*CheckPackage
,
2405 IN CHAR16
*KeyString
2412 // return NULL for no package or KeyString is NULL
2414 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2419 // enumerate through the list of parametrs
2421 for ( Node
= GetFirstNode(CheckPackage
)
2422 ; !IsNull (CheckPackage
, Node
)
2423 ; Node
= GetNextNode(CheckPackage
, Node
)
2426 // If the Name matches, return TRUE (and there may be NULL name)
2428 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2430 // If Type is TypeStart then only compare the begining of the strings
2432 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2433 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2434 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2437 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2438 if (TempString
!= NULL
) {
2439 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2440 FreePool(TempString
);
2441 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2443 FreePool(TempString
);
2445 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2446 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2454 Returns raw value from command line argument.
2456 Raw value parameters are in the form of "value" in a specific position in the list.
2458 If CheckPackage is NULL, then return NULL.
2460 @param[in] CheckPackage The package of parsed command line arguments.
2461 @param[in] Position The position of the value.
2463 @retval NULL The flag is not on the command line.
2464 @retval !=NULL The pointer to unicode string of the value.
2468 ShellCommandLineGetRawValue (
2469 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2476 // check for CheckPackage == NULL
2478 if (CheckPackage
== NULL
) {
2483 // enumerate through the list of parametrs
2485 for ( Node
= GetFirstNode(CheckPackage
)
2486 ; !IsNull (CheckPackage
, Node
)
2487 ; Node
= GetNextNode(CheckPackage
, Node
)
2490 // If the position matches, return the value
2492 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2493 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2500 returns the number of command line value parameters that were parsed.
2502 this will not include flags.
2504 @param[in] CheckPackage The package of parsed command line arguments.
2506 @retval (UINTN)-1 No parsing has ocurred
2507 @return other The number of value parameters found
2511 ShellCommandLineGetCount(
2512 IN CONST LIST_ENTRY
*CheckPackage
2518 if (CheckPackage
== NULL
) {
2521 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2522 ; !IsNull (CheckPackage
, Node1
)
2523 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2525 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2533 Determines if a parameter is duplicated.
2535 If Param is not NULL then it will point to a callee allocated string buffer
2536 with the parameter value if a duplicate is found.
2538 If CheckPackage is NULL, then ASSERT.
2540 @param[in] CheckPackage The package of parsed command line arguments.
2541 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2543 @retval EFI_SUCCESS No parameters were duplicated.
2544 @retval EFI_DEVICE_ERROR A duplicate was found.
2548 ShellCommandLineCheckDuplicate (
2549 IN CONST LIST_ENTRY
*CheckPackage
,
2556 ASSERT(CheckPackage
!= NULL
);
2558 for ( Node1
= GetFirstNode(CheckPackage
)
2559 ; !IsNull (CheckPackage
, Node1
)
2560 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2562 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2563 ; !IsNull (CheckPackage
, Node2
)
2564 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2566 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) {
2567 if (Param
!= NULL
) {
2569 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2571 return (EFI_DEVICE_ERROR
);
2575 return (EFI_SUCCESS
);
2579 This is a find and replace function. Upon successful return the NewString is a copy of
2580 SourceString with each instance of FindTarget replaced with ReplaceWith.
2582 If SourceString and NewString overlap the behavior is undefined.
2584 If the string would grow bigger than NewSize it will halt and return error.
2586 @param[in] SourceString The string with source buffer.
2587 @param[in, out] NewString The string with resultant buffer.
2588 @param[in] NewSize The size in bytes of NewString.
2589 @param[in] FindTarget The string to look for.
2590 @param[in] ReplaceWith The string to replace FindTarget with.
2591 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2592 immediately before it.
2593 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2595 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2596 @retval EFI_INVALID_PARAMETER NewString was NULL.
2597 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2598 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2599 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2600 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2601 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2602 the new string (truncation occurred).
2603 @retval EFI_SUCCESS The string was successfully copied with replacement.
2607 ShellCopySearchAndReplace(
2608 IN CHAR16 CONST
*SourceString
,
2609 IN OUT CHAR16
*NewString
,
2611 IN CONST CHAR16
*FindTarget
,
2612 IN CONST CHAR16
*ReplaceWith
,
2613 IN CONST BOOLEAN SkipPreCarrot
,
2614 IN CONST BOOLEAN ParameterReplacing
2620 if ( (SourceString
== NULL
)
2621 || (NewString
== NULL
)
2622 || (FindTarget
== NULL
)
2623 || (ReplaceWith
== NULL
)
2624 || (StrLen(FindTarget
) < 1)
2625 || (StrLen(SourceString
) < 1)
2627 return (EFI_INVALID_PARAMETER
);
2630 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2631 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2633 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2634 if (Replace
!= NULL
) {
2635 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2638 if (Replace
== NULL
) {
2639 return (EFI_OUT_OF_RESOURCES
);
2641 NewString
= ZeroMem(NewString
, NewSize
);
2642 while (*SourceString
!= CHAR_NULL
) {
2644 // if we find the FindTarget and either Skip == FALSE or Skip and we
2645 // dont have a carrot do a replace...
2647 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2648 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2650 SourceString
+= StrLen(FindTarget
);
2651 Size
= StrSize(NewString
);
2652 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2654 return (EFI_BUFFER_TOO_SMALL
);
2656 StrCatS(NewString
, NewSize
/sizeof(CHAR16
), Replace
);
2658 Size
= StrSize(NewString
);
2659 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2661 return (EFI_BUFFER_TOO_SMALL
);
2663 StrnCatS(NewString
, NewSize
/sizeof(CHAR16
), SourceString
, 1);
2668 return (EFI_SUCCESS
);
2672 Internal worker function to output a string.
2674 This function will output a string to the correct StdOut.
2676 @param[in] String The string to print out.
2678 @retval EFI_SUCCESS The operation was sucessful.
2679 @retval !EFI_SUCCESS The operation failed.
2684 IN CONST CHAR16
*String
2688 Size
= StrSize(String
) - sizeof(CHAR16
);
2690 return (EFI_SUCCESS
);
2692 if (gEfiShellParametersProtocol
!= NULL
) {
2693 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2695 if (mEfiShellInterface
!= NULL
) {
2696 if (mEfiShellInterface
->RedirArgc
== 0) {
2698 // Divide in half for old shell. Must be string length not size.
2700 Size
/=2; // Divide in half only when no redirection.
2702 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2705 return (EFI_UNSUPPORTED
);
2709 Print at a specific location on the screen.
2711 This function will move the cursor to a given screen location and print the specified string
2713 If -1 is specified for either the Row or Col the current screen location for BOTH
2716 if either Row or Col is out of range for the current console, then ASSERT
2717 if Format is NULL, then ASSERT
2719 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2720 the following additional flags:
2721 %N - Set output attribute to normal
2722 %H - Set output attribute to highlight
2723 %E - Set output attribute to error
2724 %B - Set output attribute to blue color
2725 %V - Set output attribute to green color
2727 Note: The background color is controlled by the shell command cls.
2729 @param[in] Col the column to print at
2730 @param[in] Row the row to print at
2731 @param[in] Format the format string
2732 @param[in] Marker the marker for the variable argument list
2734 @return EFI_SUCCESS The operation was successful.
2735 @return EFI_DEVICE_ERROR The console device reported an error.
2739 InternalShellPrintWorker(
2740 IN INT32 Col OPTIONAL
,
2741 IN INT32 Row OPTIONAL
,
2742 IN CONST CHAR16
*Format
,
2747 CHAR16
*ResumeLocation
;
2748 CHAR16
*FormatWalker
;
2749 UINTN OriginalAttribute
;
2750 CHAR16
*mPostReplaceFormat
;
2751 CHAR16
*mPostReplaceFormat2
;
2753 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2754 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2756 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2757 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2758 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2759 return (EFI_OUT_OF_RESOURCES
);
2762 Status
= EFI_SUCCESS
;
2763 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2766 // Back and forth each time fixing up 1 of our flags...
2768 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2769 ASSERT_EFI_ERROR(Status
);
2770 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2771 ASSERT_EFI_ERROR(Status
);
2772 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2773 ASSERT_EFI_ERROR(Status
);
2774 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2775 ASSERT_EFI_ERROR(Status
);
2776 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2777 ASSERT_EFI_ERROR(Status
);
2780 // Use the last buffer from replacing to print from...
2782 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2784 if (Col
!= -1 && Row
!= -1) {
2785 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2788 FormatWalker
= mPostReplaceFormat2
;
2789 while (*FormatWalker
!= CHAR_NULL
) {
2791 // Find the next attribute change request
2793 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2794 if (ResumeLocation
!= NULL
) {
2795 *ResumeLocation
= CHAR_NULL
;
2798 // print the current FormatWalker string
2800 if (StrLen(FormatWalker
)>0) {
2801 Status
= InternalPrintTo(FormatWalker
);
2802 if (EFI_ERROR(Status
)) {
2808 // update the attribute
2810 if (ResumeLocation
!= NULL
) {
2811 if (*(ResumeLocation
-1) == L
'^') {
2813 // Move cursor back 1 position to overwrite the ^
2815 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2818 // Print a simple '%' symbol
2820 Status
= InternalPrintTo(L
"%");
2821 ResumeLocation
= ResumeLocation
- 1;
2823 switch (*(ResumeLocation
+1)) {
2825 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2828 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2831 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2834 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2837 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2841 // Print a simple '%' symbol
2843 Status
= InternalPrintTo(L
"%");
2844 if (EFI_ERROR(Status
)) {
2847 ResumeLocation
= ResumeLocation
- 1;
2853 // reset to normal now...
2859 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2861 FormatWalker
= ResumeLocation
+ 2;
2864 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2866 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2867 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2872 Print at a specific location on the screen.
2874 This function will move the cursor to a given screen location and print the specified string.
2876 If -1 is specified for either the Row or Col the current screen location for BOTH
2879 If either Row or Col is out of range for the current console, then ASSERT.
2880 If Format is NULL, then ASSERT.
2882 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2883 the following additional flags:
2884 %N - Set output attribute to normal
2885 %H - Set output attribute to highlight
2886 %E - Set output attribute to error
2887 %B - Set output attribute to blue color
2888 %V - Set output attribute to green color
2890 Note: The background color is controlled by the shell command cls.
2892 @param[in] Col the column to print at
2893 @param[in] Row the row to print at
2894 @param[in] Format the format string
2895 @param[in] ... The variable argument list.
2897 @return EFI_SUCCESS The printing was successful.
2898 @return EFI_DEVICE_ERROR The console device reported an error.
2903 IN INT32 Col OPTIONAL
,
2904 IN INT32 Row OPTIONAL
,
2905 IN CONST CHAR16
*Format
,
2911 if (Format
== NULL
) {
2912 return (EFI_INVALID_PARAMETER
);
2914 VA_START (Marker
, Format
);
2915 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2921 Print at a specific location on the screen.
2923 This function will move the cursor to a given screen location and print the specified string.
2925 If -1 is specified for either the Row or Col the current screen location for BOTH
2928 If either Row or Col is out of range for the current console, then ASSERT.
2929 If Format is NULL, then ASSERT.
2931 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2932 the following additional flags:
2933 %N - Set output attribute to normal.
2934 %H - Set output attribute to highlight.
2935 %E - Set output attribute to error.
2936 %B - Set output attribute to blue color.
2937 %V - Set output attribute to green color.
2939 Note: The background color is controlled by the shell command cls.
2941 @param[in] Col The column to print at.
2942 @param[in] Row The row to print at.
2943 @param[in] Language The language of the string to retrieve. If this parameter
2944 is NULL, then the current platform language is used.
2945 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2946 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2947 @param[in] ... The variable argument list.
2949 @return EFI_SUCCESS The printing was successful.
2950 @return EFI_DEVICE_ERROR The console device reported an error.
2955 IN INT32 Col OPTIONAL
,
2956 IN INT32 Row OPTIONAL
,
2957 IN CONST CHAR8
*Language OPTIONAL
,
2958 IN CONST EFI_STRING_ID HiiFormatStringId
,
2959 IN CONST EFI_HANDLE HiiFormatHandle
,
2964 CHAR16
*HiiFormatString
;
2967 VA_START (Marker
, HiiFormatHandle
);
2968 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2969 ASSERT(HiiFormatString
!= NULL
);
2971 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2973 SHELL_FREE_NON_NULL(HiiFormatString
);
2980 Function to determine if a given filename represents a file or a directory.
2982 @param[in] DirName Path to directory to test.
2984 @retval EFI_SUCCESS The Path represents a directory
2985 @retval EFI_NOT_FOUND The Path does not represent a directory
2986 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2987 @return The path failed to open
2992 IN CONST CHAR16
*DirName
2996 SHELL_FILE_HANDLE Handle
;
2997 CHAR16
*TempLocation
;
2998 CHAR16
*TempLocation2
;
3000 ASSERT(DirName
!= NULL
);
3003 TempLocation
= NULL
;
3005 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3006 if (EFI_ERROR(Status
)) {
3008 // try good logic first.
3010 if (gEfiShellProtocol
!= NULL
) {
3011 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3012 if (TempLocation
== NULL
) {
3013 ShellCloseFile(&Handle
);
3014 return (EFI_OUT_OF_RESOURCES
);
3016 TempLocation2
= StrStr(TempLocation
, L
":");
3017 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3018 *(TempLocation2
+1) = CHAR_NULL
;
3020 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3021 FreePool(TempLocation
);
3022 return (EFI_SUCCESS
);
3024 FreePool(TempLocation
);
3027 // probably a map name?!?!!?
3029 TempLocation
= StrStr(DirName
, L
"\\");
3030 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3031 return (EFI_SUCCESS
);
3037 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3038 ShellCloseFile(&Handle
);
3039 return (EFI_SUCCESS
);
3041 ShellCloseFile(&Handle
);
3042 return (EFI_NOT_FOUND
);
3046 Function to determine if a given filename represents a file.
3048 @param[in] Name Path to file to test.
3050 @retval EFI_SUCCESS The Path represents a file.
3051 @retval EFI_NOT_FOUND The Path does not represent a file.
3052 @retval other The path failed to open.
3057 IN CONST CHAR16
*Name
3061 SHELL_FILE_HANDLE Handle
;
3063 ASSERT(Name
!= NULL
);
3067 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3068 if (EFI_ERROR(Status
)) {
3072 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3073 ShellCloseFile(&Handle
);
3074 return (EFI_SUCCESS
);
3076 ShellCloseFile(&Handle
);
3077 return (EFI_NOT_FOUND
);
3081 Function to determine if a given filename represents a file.
3083 This will search the CWD and then the Path.
3085 If Name is NULL, then ASSERT.
3087 @param[in] Name Path to file to test.
3089 @retval EFI_SUCCESS The Path represents a file.
3090 @retval EFI_NOT_FOUND The Path does not represent a file.
3091 @retval other The path failed to open.
3096 IN CONST CHAR16
*Name
3102 if (!EFI_ERROR(ShellIsFile(Name
))) {
3103 return (EFI_SUCCESS
);
3106 NewName
= ShellFindFilePath(Name
);
3107 if (NewName
== NULL
) {
3108 return (EFI_NOT_FOUND
);
3110 Status
= ShellIsFile(NewName
);
3116 Function return the number converted from a hex representation of a number.
3118 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3119 result. Use ShellConvertStringToUint64 instead.
3121 @param[in] String String representation of a number.
3123 @return The unsigned integer result of the conversion.
3124 @retval (UINTN)(-1) An error occured.
3129 IN CONST CHAR16
*String
3134 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3135 return ((UINTN
)RetVal
);
3138 return ((UINTN
)(-1));
3142 Function to determine whether a string is decimal or hex representation of a number
3143 and return the number converted from the string. Spaces are always skipped.
3145 @param[in] String String representation of a number
3148 @retval (UINTN)(-1) An error ocurred.
3153 IN CONST CHAR16
*String
3161 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3165 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3166 return ((UINTN
)RetVal
);
3168 return ((UINTN
)(-1));
3172 Safely append with automatic string resizing given length of Destination and
3173 desired length of copy from Source.
3175 append the first D characters of Source to the end of Destination, where D is
3176 the lesser of Count and the StrLen() of Source. If appending those D characters
3177 will fit within Destination (whose Size is given as CurrentSize) and
3178 still leave room for a NULL terminator, then those characters are appended,
3179 starting at the original terminating NULL of Destination, and a new terminating
3182 If appending D characters onto Destination will result in a overflow of the size
3183 given in CurrentSize the string will be grown such that the copy can be performed
3184 and CurrentSize will be updated to the new size.
3186 If Source is NULL, there is nothing to append, just return the current buffer in
3189 if Destination is NULL, then ASSERT()
3190 if Destination's current length (including NULL terminator) is already more then
3191 CurrentSize, then ASSERT()
3193 @param[in, out] Destination The String to append onto
3194 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3195 return possibly the new size (still in bytes). if NULL
3196 then allocate whatever is needed.
3197 @param[in] Source The String to append from
3198 @param[in] Count Maximum number of characters to append. if 0 then
3201 @return Destination return the resultant string.
3206 IN OUT CHAR16
**Destination
,
3207 IN OUT UINTN
*CurrentSize
,
3208 IN CONST CHAR16
*Source
,
3212 UINTN DestinationStartSize
;
3218 ASSERT(Destination
!= NULL
);
3221 // If there's nothing to do then just return Destination
3223 if (Source
== NULL
) {
3224 return (*Destination
);
3228 // allow for un-initialized pointers, based on size being 0
3230 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3231 *Destination
= NULL
;
3235 // allow for NULL pointers address as Destination
3237 if (*Destination
!= NULL
) {
3238 ASSERT(CurrentSize
!= 0);
3239 DestinationStartSize
= StrSize(*Destination
);
3240 ASSERT(DestinationStartSize
<= *CurrentSize
);
3242 DestinationStartSize
= 0;
3243 // ASSERT(*CurrentSize == 0);
3247 // Append all of Source?
3250 Count
= StrLen(Source
);
3254 // Test and grow if required
3256 if (CurrentSize
!= NULL
) {
3257 NewSize
= *CurrentSize
;
3258 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3259 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3260 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3262 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3263 *CurrentSize
= NewSize
;
3266 NewSize
= (Count
+1)*sizeof(CHAR16
);
3267 *Destination
= AllocateZeroPool(NewSize
);
3271 // Now use standard StrnCat on a big enough buffer
3273 if (*Destination
== NULL
) {
3277 StrnCatS(*Destination
, NewSize
/sizeof(CHAR16
), Source
, Count
);
3278 return *Destination
;
3282 Prompt the user and return the resultant answer to the requestor.
3284 This function will display the requested question on the shell prompt and then
3285 wait for an apropriate answer to be input from the console.
3287 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3288 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3290 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3293 In either case *Response must be callee freed if Response was not NULL;
3295 @param Type What type of question is asked. This is used to filter the input
3296 to prevent invalid answers to question.
3297 @param Prompt Pointer to string prompt to use to request input.
3298 @param Response Pointer to Response which will be populated upon return.
3300 @retval EFI_SUCCESS The operation was sucessful.
3301 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3302 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3303 @return other The operation failed.
3307 ShellPromptForResponse (
3308 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3309 IN CHAR16
*Prompt OPTIONAL
,
3310 IN OUT VOID
**Response OPTIONAL
3316 SHELL_PROMPT_RESPONSE
*Resp
;
3320 Status
= EFI_UNSUPPORTED
;
3324 if (Type
!= ShellPromptResponseTypeFreeform
) {
3325 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3327 return (EFI_OUT_OF_RESOURCES
);
3332 case ShellPromptResponseTypeQuitContinue
:
3333 if (Prompt
!= NULL
) {
3334 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3337 // wait for valid response
3339 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3340 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3341 if (EFI_ERROR(Status
)) {
3344 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3345 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3346 *Resp
= ShellPromptResponseQuit
;
3348 *Resp
= ShellPromptResponseContinue
;
3351 case ShellPromptResponseTypeYesNoCancel
:
3352 if (Prompt
!= NULL
) {
3353 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3356 // wait for valid response
3358 *Resp
= ShellPromptResponseMax
;
3359 while (*Resp
== ShellPromptResponseMax
) {
3360 if (ShellGetExecutionBreakFlag()) {
3361 Status
= EFI_ABORTED
;
3364 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3365 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3366 if (EFI_ERROR(Status
)) {
3369 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3370 switch (Key
.UnicodeChar
) {
3373 *Resp
= ShellPromptResponseYes
;
3377 *Resp
= ShellPromptResponseNo
;
3381 *Resp
= ShellPromptResponseCancel
;
3385 break; case ShellPromptResponseTypeYesNoAllCancel
:
3386 if (Prompt
!= NULL
) {
3387 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3390 // wait for valid response
3392 *Resp
= ShellPromptResponseMax
;
3393 while (*Resp
== ShellPromptResponseMax
) {
3394 if (ShellGetExecutionBreakFlag()) {
3395 Status
= EFI_ABORTED
;
3398 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3399 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3400 if (EFI_ERROR(Status
)) {
3403 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3404 switch (Key
.UnicodeChar
) {
3407 *Resp
= ShellPromptResponseYes
;
3411 *Resp
= ShellPromptResponseNo
;
3415 *Resp
= ShellPromptResponseAll
;
3419 *Resp
= ShellPromptResponseCancel
;
3424 case ShellPromptResponseTypeEnterContinue
:
3425 case ShellPromptResponseTypeAnyKeyContinue
:
3426 if (Prompt
!= NULL
) {
3427 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3430 // wait for valid response
3432 *Resp
= ShellPromptResponseMax
;
3433 while (*Resp
== ShellPromptResponseMax
) {
3434 if (ShellGetExecutionBreakFlag()) {
3435 Status
= EFI_ABORTED
;
3438 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3439 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3440 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3441 if (EFI_ERROR(Status
)) {
3444 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3445 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3446 *Resp
= ShellPromptResponseContinue
;
3450 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3451 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3452 ASSERT_EFI_ERROR(Status
);
3453 *Resp
= ShellPromptResponseContinue
;
3458 case ShellPromptResponseTypeYesNo
:
3459 if (Prompt
!= NULL
) {
3460 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3463 // wait for valid response
3465 *Resp
= ShellPromptResponseMax
;
3466 while (*Resp
== ShellPromptResponseMax
) {
3467 if (ShellGetExecutionBreakFlag()) {
3468 Status
= EFI_ABORTED
;
3471 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3472 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3473 if (EFI_ERROR(Status
)) {
3476 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3477 switch (Key
.UnicodeChar
) {
3480 *Resp
= ShellPromptResponseYes
;
3484 *Resp
= ShellPromptResponseNo
;
3489 case ShellPromptResponseTypeFreeform
:
3490 if (Prompt
!= NULL
) {
3491 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3494 if (ShellGetExecutionBreakFlag()) {
3495 Status
= EFI_ABORTED
;
3498 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3499 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3500 if (EFI_ERROR(Status
)) {
3503 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3504 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3507 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3508 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3512 // This is the location to add new prompt types.
3513 // If your new type loops remember to add ExecutionBreak support.
3519 if (Response
!= NULL
) {
3522 } else if (Buffer
!= NULL
) {
3529 if (Buffer
!= NULL
) {
3534 ShellPrintEx(-1, -1, L
"\r\n");
3539 Prompt the user and return the resultant answer to the requestor.
3541 This function is the same as ShellPromptForResponse, except that the prompt is
3542 automatically pulled from HII.
3544 @param Type What type of question is asked. This is used to filter the input
3545 to prevent invalid answers to question.
3546 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3547 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3548 @param Response Pointer to Response which will be populated upon return.
3550 @retval EFI_SUCCESS the operation was sucessful.
3551 @return other the operation failed.
3553 @sa ShellPromptForResponse
3557 ShellPromptForResponseHii (
3558 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3559 IN CONST EFI_STRING_ID HiiFormatStringId
,
3560 IN CONST EFI_HANDLE HiiFormatHandle
,
3561 IN OUT VOID
**Response
3567 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3568 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3574 Function to determin if an entire string is a valid number.
3576 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3578 @param[in] String The string to evaluate.
3579 @param[in] ForceHex TRUE - always assume hex.
3580 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3581 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3583 @retval TRUE It is all numeric (dec/hex) characters.
3584 @retval FALSE There is a non-numeric character.
3588 InternalShellIsHexOrDecimalNumber (
3589 IN CONST CHAR16
*String
,
3590 IN CONST BOOLEAN ForceHex
,
3591 IN CONST BOOLEAN StopAtSpace
,
3592 IN CONST BOOLEAN TimeNumbers
3598 // chop off a single negative sign
3600 if (String
!= NULL
&& *String
== L
'-') {
3604 if (String
== NULL
) {
3609 // chop leading zeroes
3611 while(String
!= NULL
&& *String
== L
'0'){
3615 // allow '0x' or '0X', but not 'x' or 'X'
3617 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3618 if (*(String
-1) != L
'0') {
3620 // we got an x without a preceeding 0
3626 } else if (ForceHex
) {
3633 // loop through the remaining characters and use the lib function
3635 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3636 if (TimeNumbers
&& (String
[0] == L
':')) {
3640 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3644 if (!ShellIsDecimalDigitCharacter(*String
)) {
3654 Function to determine if a given filename exists.
3656 @param[in] Name Path to test.
3658 @retval EFI_SUCCESS The Path represents a file.
3659 @retval EFI_NOT_FOUND The Path does not represent a file.
3660 @retval other The path failed to open.
3665 IN CONST CHAR16
*Name
3669 EFI_SHELL_FILE_INFO
*List
;
3671 ASSERT(Name
!= NULL
);
3674 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3675 if (EFI_ERROR(Status
)) {
3679 ShellCloseFileMetaArg(&List
);
3681 return (EFI_SUCCESS
);
3685 Convert a Unicode character to upper case only if
3686 it maps to a valid small-case ASCII character.
3688 This internal function only deal with Unicode character
3689 which maps to a valid small-case ASCII character, i.e.
3690 L'a' to L'z'. For other Unicode character, the input character
3691 is returned directly.
3693 @param Char The character to convert.
3695 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3696 @retval Unchanged Otherwise.
3701 InternalShellCharToUpper (
3705 if (Char
>= L
'a' && Char
<= L
'z') {
3706 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3713 Convert a Unicode character to numerical value.
3715 This internal function only deal with Unicode character
3716 which maps to a valid hexadecimal ASII character, i.e.
3717 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3718 Unicode character, the value returned does not make sense.
3720 @param Char The character to convert.
3722 @return The numerical value converted.
3727 InternalShellHexCharToUintn (
3731 if (ShellIsDecimalDigitCharacter (Char
)) {
3735 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3739 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3741 This function returns a value of type UINT64 by interpreting the contents
3742 of the Unicode string specified by String as a hexadecimal number.
3743 The format of the input Unicode string String is:
3745 [spaces][zeros][x][hexadecimal digits].
3747 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3748 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3749 If "x" appears in the input string, it must be prefixed with at least one 0.
3750 The function will ignore the pad space, which includes spaces or tab characters,
3751 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3752 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3753 first valid hexadecimal digit. Then, the function stops at the first character that is
3754 a not a valid hexadecimal character or NULL, whichever one comes first.
3756 If String has only pad spaces, then zero is returned.
3757 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3758 then zero is returned.
3760 @param[in] String A pointer to a Null-terminated Unicode string.
3761 @param[out] Value Upon a successful return the value of the conversion.
3762 @param[in] StopAtSpace FALSE to skip spaces.
3764 @retval EFI_SUCCESS The conversion was successful.
3765 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3766 @retval EFI_DEVICE_ERROR An overflow occured.
3770 InternalShellStrHexToUint64 (
3771 IN CONST CHAR16
*String
,
3773 IN CONST BOOLEAN StopAtSpace
3778 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3779 return (EFI_INVALID_PARAMETER
);
3783 // Ignore the pad spaces (space or tab)
3785 while ((*String
== L
' ') || (*String
== L
'\t')) {
3790 // Ignore leading Zeros after the spaces
3792 while (*String
== L
'0') {
3796 if (InternalShellCharToUpper (*String
) == L
'X') {
3797 if (*(String
- 1) != L
'0') {
3809 // there is a space where there should't be
3811 if (*String
== L
' ') {
3812 return (EFI_INVALID_PARAMETER
);
3815 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3817 // If the Hex Number represented by String overflows according
3818 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3820 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3821 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3822 return (EFI_DEVICE_ERROR
);
3825 Result
= (LShiftU64(Result
, 4));
3826 Result
+= InternalShellHexCharToUintn (*String
);
3830 // stop at spaces if requested
3832 if (StopAtSpace
&& *String
== L
' ') {
3838 return (EFI_SUCCESS
);
3842 Convert a Null-terminated Unicode decimal string to a value of
3845 This function returns a value of type UINT64 by interpreting the contents
3846 of the Unicode string specified by String as a decimal number. The format
3847 of the input Unicode string String is:
3849 [spaces] [decimal digits].
3851 The valid decimal digit character is in the range [0-9]. The
3852 function will ignore the pad space, which includes spaces or
3853 tab characters, before [decimal digits]. The running zero in the
3854 beginning of [decimal digits] will be ignored. Then, the function
3855 stops at the first character that is a not a valid decimal character
3856 or a Null-terminator, whichever one comes first.
3858 If String has only pad spaces, then 0 is returned.
3859 If String has no pad spaces or valid decimal digits,
3862 @param[in] String A pointer to a Null-terminated Unicode string.
3863 @param[out] Value Upon a successful return the value of the conversion.
3864 @param[in] StopAtSpace FALSE to skip spaces.
3866 @retval EFI_SUCCESS The conversion was successful.
3867 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3868 @retval EFI_DEVICE_ERROR An overflow occured.
3872 InternalShellStrDecimalToUint64 (
3873 IN CONST CHAR16
*String
,
3875 IN CONST BOOLEAN StopAtSpace
3880 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3881 return (EFI_INVALID_PARAMETER
);
3885 // Ignore the pad spaces (space or tab)
3887 while ((*String
== L
' ') || (*String
== L
'\t')) {
3892 // Ignore leading Zeros after the spaces
3894 while (*String
== L
'0') {
3901 // Stop upon space if requested
3902 // (if the whole value was 0)
3904 if (StopAtSpace
&& *String
== L
' ') {
3906 return (EFI_SUCCESS
);
3909 while (ShellIsDecimalDigitCharacter (*String
)) {
3911 // If the number represented by String overflows according
3912 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3915 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3916 return (EFI_DEVICE_ERROR
);
3919 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3923 // Stop at spaces if requested
3925 if (StopAtSpace
&& *String
== L
' ') {
3932 return (EFI_SUCCESS
);
3936 Function to verify and convert a string to its numerical value.
3938 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3940 @param[in] String The string to evaluate.
3941 @param[out] Value Upon a successful return the value of the conversion.
3942 @param[in] ForceHex TRUE - always assume hex.
3943 @param[in] StopAtSpace FALSE to skip spaces.
3945 @retval EFI_SUCCESS The conversion was successful.
3946 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3947 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3951 ShellConvertStringToUint64(
3952 IN CONST CHAR16
*String
,
3954 IN CONST BOOLEAN ForceHex
,
3955 IN CONST BOOLEAN StopAtSpace
3959 CONST CHAR16
*Walker
;
3965 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3968 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3969 return (EFI_INVALID_PARAMETER
);
3972 return (EFI_INVALID_PARAMETER
);
3977 // Chop off leading spaces
3979 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
3982 // make sure we have something left that is numeric.
3984 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
3985 return (EFI_INVALID_PARAMETER
);
3989 // do the conversion.
3991 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
3992 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
3994 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
3997 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
3998 return (EFI_NOT_FOUND
);
4001 if (Value
!= NULL
) {
4009 Function to determin if an entire string is a valid number.
4011 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4013 @param[in] String The string to evaluate.
4014 @param[in] ForceHex TRUE - always assume hex.
4015 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4017 @retval TRUE It is all numeric (dec/hex) characters.
4018 @retval FALSE There is a non-numeric character.
4022 ShellIsHexOrDecimalNumber (
4023 IN CONST CHAR16
*String
,
4024 IN CONST BOOLEAN ForceHex
,
4025 IN CONST BOOLEAN StopAtSpace
4028 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4035 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4036 buffer. The returned buffer must be callee freed.
4038 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4039 maintained and not changed for all operations with the same file.
4041 @param[in] Handle SHELL_FILE_HANDLE to read from.
4042 @param[in, out] Ascii Boolean value for indicating whether the file is
4043 Ascii (TRUE) or UCS2 (FALSE).
4045 @return The line of text from the file.
4046 @retval NULL There was not enough memory available.
4048 @sa ShellFileHandleReadLine
4052 ShellFileHandleReturnLine(
4053 IN SHELL_FILE_HANDLE Handle
,
4054 IN OUT BOOLEAN
*Ascii
4064 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4065 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4066 RetVal
= AllocateZeroPool(Size
);
4067 if (RetVal
== NULL
) {
4070 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4073 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4081 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4083 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4084 maintained and not changed for all operations with the same file.
4086 @param[in] Handle SHELL_FILE_HANDLE to read from.
4087 @param[in, out] Buffer The pointer to buffer to read into.
4088 @param[in, out] Size The pointer to number of bytes in Buffer.
4089 @param[in] Truncate If the buffer is large enough, this has no effect.
4090 If the buffer is is too small and Truncate is TRUE,
4091 the line will be truncated.
4092 If the buffer is is too small and Truncate is FALSE,
4093 then no read will occur.
4095 @param[in, out] Ascii Boolean value for indicating whether the file is
4096 Ascii (TRUE) or UCS2 (FALSE).
4098 @retval EFI_SUCCESS The operation was successful. The line is stored in
4100 @retval EFI_INVALID_PARAMETER Handle was NULL.
4101 @retval EFI_INVALID_PARAMETER Size was NULL.
4102 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4103 Size was updated to the minimum space required.
4107 ShellFileHandleReadLine(
4108 IN SHELL_FILE_HANDLE Handle
,
4109 IN OUT CHAR16
*Buffer
,
4111 IN BOOLEAN Truncate
,
4112 IN OUT BOOLEAN
*Ascii
4119 UINT64 OriginalFilePosition
;
4125 return (EFI_INVALID_PARAMETER
);
4127 if (Buffer
== NULL
) {
4130 *Buffer
= CHAR_NULL
;
4132 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4133 if (OriginalFilePosition
== 0) {
4134 CharSize
= sizeof(CHAR16
);
4135 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4136 ASSERT_EFI_ERROR(Status
);
4137 if (CharBuffer
== gUnicodeFileTag
) {
4141 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4145 for (CountSoFar
= 0;;CountSoFar
++){
4148 CharSize
= sizeof(CHAR8
);
4150 CharSize
= sizeof(CHAR16
);
4152 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4153 if ( EFI_ERROR(Status
)
4155 || (CharBuffer
== L
'\n' && !(*Ascii
))
4156 || (CharBuffer
== '\n' && *Ascii
)
4161 // if we have space save it...
4163 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4164 ASSERT(Buffer
!= NULL
);
4165 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4166 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4171 // if we ran out of space tell when...
4173 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4174 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4176 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4178 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4180 return (EFI_BUFFER_TOO_SMALL
);
4182 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4183 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4190 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4192 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4193 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4194 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4195 the help content only.
4196 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4197 @retval EFI_NOT_FOUND The help data could not be found.
4198 @retval EFI_SUCCESS The operation was successful.
4203 IN CONST CHAR16
*CommandToGetHelpOn
,
4204 IN CONST CHAR16
*SectionToGetHelpOn
,
4205 IN BOOLEAN PrintCommandText
4214 // Get the string to print based
4216 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4219 // make sure we got a valid string
4221 if (EFI_ERROR(Status
)){
4224 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4225 return EFI_NOT_FOUND
;
4229 // Chop off trailing stuff we dont need
4231 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4232 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4236 // Print this out to the console
4238 if (PrintCommandText
) {
4239 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4241 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4244 SHELL_FREE_NON_NULL(OutText
);
4250 Function to delete a file by name
4252 @param[in] FileName Pointer to file name to delete.
4254 @retval EFI_SUCCESS the file was deleted sucessfully
4255 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4257 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4258 @retval EFI_NOT_FOUND The specified file could not be found on the
4259 device or the file system could not be found
4261 @retval EFI_NO_MEDIA The device has no medium.
4262 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4263 medium is no longer supported.
4264 @retval EFI_DEVICE_ERROR The device reported an error.
4265 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4266 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4267 @retval EFI_ACCESS_DENIED The file was opened read only.
4268 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4270 @retval other The file failed to open
4274 ShellDeleteFileByName(
4275 IN CONST CHAR16
*FileName
4279 SHELL_FILE_HANDLE FileHandle
;
4281 Status
= ShellFileExists(FileName
);
4283 if (Status
== EFI_SUCCESS
){
4284 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4285 if (Status
== EFI_SUCCESS
){
4286 Status
= ShellDeleteFile(&FileHandle
);
4295 Cleans off all the quotes in the string.
4297 @param[in] OriginalString pointer to the string to be cleaned.
4298 @param[out] CleanString The new string with all quotes removed.
4299 Memory allocated in the function and free
4302 @retval EFI_SUCCESS The operation was successful.
4306 InternalShellStripQuotes (
4307 IN CONST CHAR16
*OriginalString
,
4308 OUT CHAR16
**CleanString
4313 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4314 return EFI_INVALID_PARAMETER
;
4317 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4318 if (*CleanString
== NULL
) {
4319 return EFI_OUT_OF_RESOURCES
;
4322 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4323 if (*Walker
== L
'\"') {
4324 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));