2 Provides interface to shell functionality for shell commands and applications.
4 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
5 Copyright 2016 Dell Inc.
6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "UefiShellLib.h"
18 #include <Library/SortLib.h>
19 #include <Library/BaseLib.h>
24 SHELL_PARAM_ITEM EmptyParamList
[] = {
27 SHELL_PARAM_ITEM SfoParamList
[] = {
31 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
32 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
33 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
34 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
35 EFI_HANDLE mEfiShellEnvironment2Handle
;
36 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
37 EFI_UNICODE_COLLATION_PROTOCOL
*mUnicodeCollationProtocol
;
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.
93 IN EFI_HANDLE ImageHandle
103 Status
= gBS
->OpenProtocol(ImageHandle
,
104 &gEfiShellEnvironment2Guid
,
105 (VOID
**)&mEfiShellEnvironment2
,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
111 // look for the mEfiShellEnvironment2 protocol at a higher level
113 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
115 // figure out how big of a buffer we need.
117 Status
= gBS
->LocateHandle (ByProtocol
,
118 &gEfiShellEnvironment2Guid
,
119 NULL
, // ignored for ByProtocol
124 // maybe it's not there???
126 if (Status
== EFI_BUFFER_TOO_SMALL
) {
127 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
128 if (Buffer
== NULL
) {
129 return (EFI_OUT_OF_RESOURCES
);
131 Status
= gBS
->LocateHandle (ByProtocol
,
132 &gEfiShellEnvironment2Guid
,
133 NULL
, // ignored for ByProtocol
138 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
140 // now parse the list of returned handles
142 Status
= EFI_NOT_FOUND
;
143 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
144 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
145 &gEfiShellEnvironment2Guid
,
146 (VOID
**)&mEfiShellEnvironment2
,
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL
151 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
152 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
153 Status
= EFI_SUCCESS
;
159 if (Buffer
!= NULL
) {
166 Function to do most of the work of the constructor. Allows for calling
167 multiple times without complete re-initialization.
169 @param[in] ImageHandle A copy of the ImageHandle.
170 @param[in] SystemTable A pointer to the SystemTable for the application.
172 @retval EFI_SUCCESS The operationw as successful.
175 ShellLibConstructorWorker (
176 IN EFI_HANDLE ImageHandle
,
177 IN EFI_SYSTEM_TABLE
*SystemTable
182 if (gEfiShellProtocol
== NULL
) {
184 // UEFI 2.0 shell interfaces (used preferentially)
186 Status
= gBS
->OpenProtocol (
188 &gEfiShellProtocolGuid
,
189 (VOID
**)&gEfiShellProtocol
,
192 EFI_OPEN_PROTOCOL_GET_PROTOCOL
194 if (EFI_ERROR (Status
)) {
196 // Search for the shell protocol
198 Status
= gBS
->LocateProtocol (
199 &gEfiShellProtocolGuid
,
201 (VOID
**)&gEfiShellProtocol
203 if (EFI_ERROR (Status
)) {
204 gEfiShellProtocol
= NULL
;
209 if (gEfiShellParametersProtocol
== NULL
) {
210 Status
= gBS
->OpenProtocol (
212 &gEfiShellParametersProtocolGuid
,
213 (VOID
**)&gEfiShellParametersProtocol
,
216 EFI_OPEN_PROTOCOL_GET_PROTOCOL
218 if (EFI_ERROR (Status
)) {
219 gEfiShellParametersProtocol
= NULL
;
223 if (gEfiShellProtocol
== NULL
) {
225 // Moved to seperate function due to complexity
227 Status
= ShellFindSE2(ImageHandle
);
229 if (EFI_ERROR(Status
)) {
230 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
231 mEfiShellEnvironment2
= NULL
;
233 Status
= gBS
->OpenProtocol(ImageHandle
,
234 &gEfiShellInterfaceGuid
,
235 (VOID
**)&mEfiShellInterface
,
238 EFI_OPEN_PROTOCOL_GET_PROTOCOL
240 if (EFI_ERROR(Status
)) {
241 mEfiShellInterface
= NULL
;
246 // Getting either EDK Shell's ShellEnvironment2 and ShellInterface protocol
247 // or UEFI Shell's Shell protocol.
248 // When ShellLib is linked to a driver producing DynamicCommand protocol,
249 // ShellParameters protocol is set by DynamicCommand.Handler().
251 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
252 (gEfiShellProtocol
!= NULL
)
254 if (gEfiShellProtocol
!= NULL
) {
255 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
256 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
257 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
258 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
259 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
260 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
261 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
262 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
263 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
264 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
266 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
267 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
268 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
269 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
270 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
271 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
272 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
273 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
274 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
275 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
277 return (EFI_SUCCESS
);
279 return (EFI_NOT_FOUND
);
282 Constructor for the Shell library.
284 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
286 @param ImageHandle the image handle of the process
287 @param SystemTable the EFI System Table pointer
289 @retval EFI_SUCCESS the initialization was complete sucessfully
290 @return others an error ocurred during initialization
294 ShellLibConstructor (
295 IN EFI_HANDLE ImageHandle
,
296 IN EFI_SYSTEM_TABLE
*SystemTable
299 mEfiShellEnvironment2
= NULL
;
300 gEfiShellProtocol
= NULL
;
301 gEfiShellParametersProtocol
= NULL
;
302 mEfiShellInterface
= NULL
;
303 mEfiShellEnvironment2Handle
= NULL
;
304 mUnicodeCollationProtocol
= NULL
;
307 // verify that auto initialize is not set false
309 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
310 return (EFI_SUCCESS
);
313 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
317 Destructor for the library. free any resources.
319 @param[in] ImageHandle A copy of the ImageHandle.
320 @param[in] SystemTable A pointer to the SystemTable for the application.
322 @retval EFI_SUCCESS The operation was successful.
323 @return An error from the CloseProtocol function.
328 IN EFI_HANDLE ImageHandle
,
329 IN EFI_SYSTEM_TABLE
*SystemTable
334 if (mEfiShellEnvironment2
!= NULL
) {
335 Status
= gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
336 &gEfiShellEnvironment2Guid
,
339 if (!EFI_ERROR (Status
)) {
340 mEfiShellEnvironment2
= NULL
;
341 mEfiShellEnvironment2Handle
= NULL
;
344 if (mEfiShellInterface
!= NULL
) {
345 Status
= gBS
->CloseProtocol(ImageHandle
,
346 &gEfiShellInterfaceGuid
,
349 if (!EFI_ERROR (Status
)) {
350 mEfiShellInterface
= NULL
;
353 if (gEfiShellProtocol
!= NULL
) {
354 Status
= gBS
->CloseProtocol(ImageHandle
,
355 &gEfiShellProtocolGuid
,
358 if (!EFI_ERROR (Status
)) {
359 gEfiShellProtocol
= NULL
;
362 if (gEfiShellParametersProtocol
!= NULL
) {
363 Status
= gBS
->CloseProtocol(ImageHandle
,
364 &gEfiShellParametersProtocolGuid
,
367 if (!EFI_ERROR (Status
)) {
368 gEfiShellParametersProtocol
= NULL
;
372 return (EFI_SUCCESS
);
376 This function causes the shell library to initialize itself. If the shell library
377 is already initialized it will de-initialize all the current protocol poitners and
378 re-populate them again.
380 When the library is used with PcdShellLibAutoInitialize set to true this function
381 will return EFI_SUCCESS and perform no actions.
383 This function is intended for internal access for shell commands only.
385 @retval EFI_SUCCESS the initialization was complete sucessfully
397 // if auto initialize is not false then skip
399 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
400 return (EFI_SUCCESS
);
404 // deinit the current stuff
406 Status
= ShellLibDestructor (gImageHandle
, gST
);
407 ASSERT_EFI_ERROR (Status
);
410 // init the new stuff
412 return (ShellLibConstructorWorker(gImageHandle
, gST
));
416 This function will retrieve the information about the file for the handle
417 specified and store it in allocated pool memory.
419 This function allocates a buffer to store the file's information. It is the
420 caller's responsibility to free the buffer
422 @param FileHandle The file handle of the file for which information is
425 @retval NULL information could not be retrieved.
427 @return the information about the file
432 IN SHELL_FILE_HANDLE FileHandle
435 return (FileFunctionMap
.GetFileInfo(FileHandle
));
439 This function sets the information about the file for the opened handle
442 @param[in] FileHandle The file handle of the file for which information
445 @param[in] FileInfo The information to set.
447 @retval EFI_SUCCESS The information was set.
448 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
449 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
450 @retval EFI_NO_MEDIA The device has no medium.
451 @retval EFI_DEVICE_ERROR The device reported an error.
452 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
453 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
454 @retval EFI_ACCESS_DENIED The file was opened read only.
455 @retval EFI_VOLUME_FULL The volume is full.
460 IN SHELL_FILE_HANDLE FileHandle
,
461 IN EFI_FILE_INFO
*FileInfo
464 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
468 This function will open a file or directory referenced by DevicePath.
470 This function opens a file with the open mode according to the file path. The
471 Attributes is valid only for EFI_FILE_MODE_CREATE.
473 @param FilePath on input the device path to the file. On output
474 the remaining device path.
475 @param FileHandle pointer to the file handle.
476 @param OpenMode the mode to open the file with.
477 @param Attributes the file's file attributes.
479 @retval EFI_SUCCESS The information was set.
480 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
481 @retval EFI_UNSUPPORTED Could not open the file path.
482 @retval EFI_NOT_FOUND The specified file could not be found on the
483 device or the file system could not be found on
485 @retval EFI_NO_MEDIA The device has no medium.
486 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
487 medium is no longer supported.
488 @retval EFI_DEVICE_ERROR The device reported an error.
489 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
490 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
491 @retval EFI_ACCESS_DENIED The file was opened read only.
492 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
494 @retval EFI_VOLUME_FULL The volume is full.
498 ShellOpenFileByDevicePath(
499 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
500 OUT SHELL_FILE_HANDLE
*FileHandle
,
507 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
508 EFI_FILE_PROTOCOL
*Handle1
;
509 EFI_FILE_PROTOCOL
*Handle2
;
510 CHAR16
*FnafPathName
;
512 EFI_HANDLE DeviceHandle
;
514 if (FilePath
== NULL
|| FileHandle
== NULL
) {
515 return (EFI_INVALID_PARAMETER
);
519 // which shell interface should we use
521 if (gEfiShellProtocol
!= NULL
) {
523 // use UEFI Shell 2.0 method.
525 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
526 if (FileName
== NULL
) {
527 return (EFI_INVALID_PARAMETER
);
529 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
536 // use old shell method.
538 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
541 if (EFI_ERROR (Status
)) {
544 Status
= gBS
->OpenProtocol(DeviceHandle
,
545 &gEfiSimpleFileSystemProtocolGuid
,
546 (VOID
**)&EfiSimpleFileSystemProtocol
,
549 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
550 if (EFI_ERROR (Status
)) {
553 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
554 if (EFI_ERROR (Status
)) {
560 // go down directories one node at a time.
562 while (!IsDevicePathEnd (*FilePath
)) {
564 // For file system access each node should be a file path component
566 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
567 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
570 return (EFI_INVALID_PARAMETER
);
573 // Open this file path node
579 // File Name Alignment Fix (FNAF)
580 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
581 // The structure pointed to by FilePath may be not CHAR16 aligned.
582 // This code copies the potentially unaligned PathName data from the
583 // FilePath structure to the aligned FnafPathName for use in the
584 // calls to Handl2->Open.
588 // Determine length of PathName, in bytes.
590 PathLen
= DevicePathNodeLength (*FilePath
) - SIZE_OF_FILEPATH_DEVICE_PATH
;
593 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
594 // Copy bytes from possibly unaligned location to aligned location
596 FnafPathName
= AllocateCopyPool(PathLen
, (UINT8
*)((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
);
597 if (FnafPathName
== NULL
) {
598 return EFI_OUT_OF_RESOURCES
;
602 // Try to test opening an existing file
604 Status
= Handle2
->Open (
608 OpenMode
&~EFI_FILE_MODE_CREATE
,
613 // see if the error was that it needs to be created
615 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
616 Status
= Handle2
->Open (
626 // Free the alignment buffer
628 FreePool(FnafPathName
);
631 // Close the last node
633 Handle2
->Close (Handle2
);
635 if (EFI_ERROR(Status
)) {
642 *FilePath
= NextDevicePathNode (*FilePath
);
646 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
648 *FileHandle
= (VOID
*)Handle1
;
649 return (EFI_SUCCESS
);
653 This function will open a file or directory referenced by filename.
655 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
656 otherwise, the Filehandle is NULL. The Attributes is valid only for
657 EFI_FILE_MODE_CREATE.
659 if FileName is NULL then ASSERT()
661 @param FileName pointer to file name
662 @param FileHandle pointer to the file handle.
663 @param OpenMode the mode to open the file with.
664 @param Attributes the file's file attributes.
666 @retval EFI_SUCCESS The information was set.
667 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
668 @retval EFI_UNSUPPORTED Could not open the file path.
669 @retval EFI_NOT_FOUND The specified file could not be found on the
670 device or the file system could not be found
672 @retval EFI_NO_MEDIA The device has no medium.
673 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
674 medium is no longer supported.
675 @retval EFI_DEVICE_ERROR The device reported an error.
676 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
677 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
678 @retval EFI_ACCESS_DENIED The file was opened read only.
679 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
681 @retval EFI_VOLUME_FULL The volume is full.
686 IN CONST CHAR16
*FileName
,
687 OUT SHELL_FILE_HANDLE
*FileHandle
,
692 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
694 EFI_FILE_INFO
*FileInfo
;
695 CHAR16
*FileNameCopy
;
699 // ASSERT if FileName is NULL
701 ASSERT(FileName
!= NULL
);
703 if (FileName
== NULL
) {
704 return (EFI_INVALID_PARAMETER
);
707 if (gEfiShellProtocol
!= NULL
) {
708 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
711 // Create only a directory
713 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
714 return ShellCreateDirectory(FileName
, FileHandle
);
718 // Create the directory to create the file in
720 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
721 if (FileNameCopy
== NULL
) {
722 return (EFI_OUT_OF_RESOURCES
);
724 PathCleanUpDirectories (FileNameCopy
);
725 if (PathRemoveLastItem (FileNameCopy
)) {
726 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy
, FileHandle
))) {
727 ShellCloseFile (FileHandle
);
730 SHELL_FREE_NON_NULL (FileNameCopy
);
734 // Use UEFI Shell 2.0 method to create the file
736 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
739 if (EFI_ERROR(Status
)) {
743 if (mUnicodeCollationProtocol
== NULL
) {
744 Status
= gBS
->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&mUnicodeCollationProtocol
);
745 if (EFI_ERROR (Status
)) {
746 gEfiShellProtocol
->CloseFile (*FileHandle
);
751 if ((mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NUL") != 0) &&
752 (mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NULL") != 0) &&
753 !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
754 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
755 ASSERT(FileInfo
!= NULL
);
756 FileInfo
->Attribute
= Attributes
;
757 Status2
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
759 if (EFI_ERROR (Status2
)) {
760 gEfiShellProtocol
->CloseFile(*FileHandle
);
767 // Using EFI Shell version
768 // this means convert name to path and call that function
769 // since this will use EFI method again that will open it.
771 ASSERT(mEfiShellEnvironment2
!= NULL
);
772 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
773 if (FilePath
!= NULL
) {
774 return (ShellOpenFileByDevicePath(&FilePath
,
779 return (EFI_DEVICE_ERROR
);
782 This function create a directory
784 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
785 otherwise, the Filehandle is NULL. If the directory already existed, this
786 function opens the existing directory.
788 @param DirectoryName pointer to directory name
789 @param FileHandle pointer to the file handle.
791 @retval EFI_SUCCESS The information was set.
792 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
793 @retval EFI_UNSUPPORTED Could not open the file path.
794 @retval EFI_NOT_FOUND The specified file could not be found on the
795 device or the file system could not be found
797 @retval EFI_NO_MEDIA The device has no medium.
798 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
799 medium is no longer supported.
800 @retval EFI_DEVICE_ERROR The device reported an error.
801 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
802 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
803 @retval EFI_ACCESS_DENIED The file was opened read only.
804 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
806 @retval EFI_VOLUME_FULL The volume is full.
807 @sa ShellOpenFileByName
811 ShellCreateDirectory(
812 IN CONST CHAR16
*DirectoryName
,
813 OUT SHELL_FILE_HANDLE
*FileHandle
816 if (gEfiShellProtocol
!= NULL
) {
818 // Use UEFI Shell 2.0 method
820 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
825 return (ShellOpenFileByName(DirectoryName
,
827 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
834 This function reads information from an opened file.
836 If FileHandle is not a directory, the function reads the requested number of
837 bytes from the file at the file's current position and returns them in Buffer.
838 If the read goes beyond the end of the file, the read length is truncated to the
839 end of the file. The file's current position is increased by the number of bytes
840 returned. If FileHandle is a directory, the function reads the directory entry
841 at the file's current position and returns the entry in Buffer. If the Buffer
842 is not large enough to hold the current directory entry, then
843 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
844 BufferSize is set to be the size of the buffer needed to read the entry. On
845 success, the current position is updated to the next directory entry. If there
846 are no more directory entries, the read returns a zero-length buffer.
847 EFI_FILE_INFO is the structure returned as the directory entry.
849 @param FileHandle the opened file handle
850 @param BufferSize on input the size of buffer in bytes. on return
851 the number of bytes written.
852 @param Buffer the buffer to put read data into.
854 @retval EFI_SUCCESS Data was read.
855 @retval EFI_NO_MEDIA The device has no media.
856 @retval EFI_DEVICE_ERROR The device reported an error.
857 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
858 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
865 IN SHELL_FILE_HANDLE FileHandle
,
866 IN OUT UINTN
*BufferSize
,
870 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
875 Write data to a file.
877 This function writes the specified number of bytes to the file at the current
878 file position. The current file position is advanced the actual number of bytes
879 written, which is returned in BufferSize. Partial writes only occur when there
880 has been a data error during the write attempt (such as "volume space full").
881 The file is automatically grown to hold the data if required. Direct writes to
882 opened directories are not supported.
884 @param FileHandle The opened file for writing
885 @param BufferSize on input the number of bytes in Buffer. On output
886 the number of bytes written.
887 @param Buffer the buffer containing data to write is stored.
889 @retval EFI_SUCCESS Data was written.
890 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
891 @retval EFI_NO_MEDIA The device has no media.
892 @retval EFI_DEVICE_ERROR The device reported an error.
893 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
894 @retval EFI_WRITE_PROTECTED The device is write-protected.
895 @retval EFI_ACCESS_DENIED The file was open for read only.
896 @retval EFI_VOLUME_FULL The volume is full.
901 IN SHELL_FILE_HANDLE FileHandle
,
902 IN OUT UINTN
*BufferSize
,
906 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
910 Close an open file handle.
912 This function closes a specified file handle. All "dirty" cached file data is
913 flushed to the device, and the file is closed. In all cases the handle is
916 @param FileHandle the file handle to close.
918 @retval EFI_SUCCESS the file handle was closed sucessfully.
923 IN SHELL_FILE_HANDLE
*FileHandle
926 return (FileFunctionMap
.CloseFile(*FileHandle
));
930 Delete a file and close the handle
932 This function closes and deletes a file. In all cases the file handle is closed.
933 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
934 returned, but the handle is still closed.
936 @param FileHandle the file handle to delete
938 @retval EFI_SUCCESS the file was closed sucessfully
939 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
941 @retval INVALID_PARAMETER One of the parameters has an invalid value.
946 IN SHELL_FILE_HANDLE
*FileHandle
949 return (FileFunctionMap
.DeleteFile(*FileHandle
));
953 Set the current position in a file.
955 This function sets the current file position for the handle to the position
956 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
957 absolute positioning is supported, and seeking past the end of the file is
958 allowed (a subsequent write would grow the file). Seeking to position
959 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
960 If FileHandle is a directory, the only position that may be set is zero. This
961 has the effect of starting the read process of the directory entries over.
963 @param FileHandle The file handle on which the position is being set
964 @param Position Byte position from begining of file
966 @retval EFI_SUCCESS Operation completed sucessfully.
967 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
969 @retval INVALID_PARAMETER One of the parameters has an invalid value.
973 ShellSetFilePosition (
974 IN SHELL_FILE_HANDLE FileHandle
,
978 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
982 Gets a file's current position
984 This function retrieves the current file position for the file handle. For
985 directories, the current file position has no meaning outside of the file
986 system driver and as such the operation is not supported. An error is returned
987 if FileHandle is a directory.
989 @param FileHandle The open file handle on which to get the position.
990 @param Position Byte position from begining of file.
992 @retval EFI_SUCCESS the operation completed sucessfully.
993 @retval INVALID_PARAMETER One of the parameters has an invalid value.
994 @retval EFI_UNSUPPORTED the request is not valid on directories.
998 ShellGetFilePosition (
999 IN SHELL_FILE_HANDLE FileHandle
,
1000 OUT UINT64
*Position
1003 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
1006 Flushes data on a file
1008 This function flushes all modified data associated with a file to a device.
1010 @param FileHandle The file handle on which to flush data
1012 @retval EFI_SUCCESS The data was flushed.
1013 @retval EFI_NO_MEDIA The device has no media.
1014 @retval EFI_DEVICE_ERROR The device reported an error.
1015 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1016 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1017 @retval EFI_ACCESS_DENIED The file was opened for read only.
1022 IN SHELL_FILE_HANDLE FileHandle
1025 return (FileFunctionMap
.FlushFile(FileHandle
));
1028 /** Retrieve first entry from a directory.
1030 This function takes an open directory handle and gets information from the
1031 first entry in the directory. A buffer is allocated to contain
1032 the information and a pointer to the buffer is returned in *Buffer. The
1033 caller can use ShellFindNextFile() to get subsequent directory entries.
1035 The buffer will be freed by ShellFindNextFile() when the last directory
1036 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1037 when finished with it.
1039 @param[in] DirHandle The file handle of the directory to search.
1040 @param[out] Buffer The pointer to the buffer for the file's information.
1042 @retval EFI_SUCCESS Found the first file.
1043 @retval EFI_NOT_FOUND Cannot find the directory.
1044 @retval EFI_NO_MEDIA The device has no media.
1045 @retval EFI_DEVICE_ERROR The device reported an error.
1046 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1047 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1052 ShellFindFirstFile (
1053 IN SHELL_FILE_HANDLE DirHandle
,
1054 OUT EFI_FILE_INFO
**Buffer
1058 // pass to file handle lib
1060 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1062 /** Retrieve next entries from a directory.
1064 To use this function, the caller must first call the ShellFindFirstFile()
1065 function to get the first directory entry. Subsequent directory entries are
1066 retrieved by using the ShellFindNextFile() function. This function can
1067 be called several times to get each entry from the directory. If the call of
1068 ShellFindNextFile() retrieved the last directory entry, the next call of
1069 this function will set *NoFile to TRUE and free the buffer.
1071 @param[in] DirHandle The file handle of the directory.
1072 @param[out] Buffer The pointer to buffer for file's information.
1073 @param[out] NoFile The pointer to boolean when last file is found.
1075 @retval EFI_SUCCESS Found the next file, or reached last file
1076 @retval EFI_NO_MEDIA The device has no media.
1077 @retval EFI_DEVICE_ERROR The device reported an error.
1078 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1083 IN SHELL_FILE_HANDLE DirHandle
,
1084 OUT EFI_FILE_INFO
*Buffer
,
1089 // pass to file handle lib
1091 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1094 Retrieve the size of a file.
1096 if FileHandle is NULL then ASSERT()
1097 if Size is NULL then ASSERT()
1099 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1102 @param FileHandle file handle from which size is retrieved
1103 @param Size pointer to size
1105 @retval EFI_SUCCESS operation was completed sucessfully
1106 @retval EFI_DEVICE_ERROR cannot access the file
1111 IN SHELL_FILE_HANDLE FileHandle
,
1115 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1118 Retrieves the status of the break execution flag
1120 this function is useful to check whether the application is being asked to halt by the shell.
1122 @retval TRUE the execution break is enabled
1123 @retval FALSE the execution break is not enabled
1127 ShellGetExecutionBreakFlag(
1132 // Check for UEFI Shell 2.0 protocols
1134 if (gEfiShellProtocol
!= NULL
) {
1137 // We are using UEFI Shell 2.0; see if the event has been triggered
1139 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1146 // using EFI Shell; call the function to check
1148 if (mEfiShellEnvironment2
!= NULL
) {
1149 return (mEfiShellEnvironment2
->GetExecutionBreak());
1155 return the value of an environment variable
1157 this function gets the value of the environment variable set by the
1158 ShellSetEnvironmentVariable function
1160 @param EnvKey The key name of the environment variable.
1162 @retval NULL the named environment variable does not exist.
1163 @return != NULL pointer to the value of the environment variable
1167 ShellGetEnvironmentVariable (
1168 IN CONST CHAR16
*EnvKey
1172 // Check for UEFI Shell 2.0 protocols
1174 if (gEfiShellProtocol
!= NULL
) {
1175 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1179 // Check for EFI shell
1181 if (mEfiShellEnvironment2
!= NULL
) {
1182 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1188 set the value of an environment variable
1190 This function changes the current value of the specified environment variable. If the
1191 environment variable exists and the Value is an empty string, then the environment
1192 variable is deleted. If the environment variable exists and the Value is not an empty
1193 string, then the value of the environment variable is changed. If the environment
1194 variable does not exist and the Value is an empty string, there is no action. If the
1195 environment variable does not exist and the Value is a non-empty string, then the
1196 environment variable is created and assigned the specified value.
1198 This is not supported pre-UEFI Shell 2.0.
1200 @param EnvKey The key name of the environment variable.
1201 @param EnvVal The Value of the environment variable
1202 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1204 @retval EFI_SUCCESS the operation was completed sucessfully
1205 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1209 ShellSetEnvironmentVariable (
1210 IN CONST CHAR16
*EnvKey
,
1211 IN CONST CHAR16
*EnvVal
,
1216 // Check for UEFI Shell 2.0 protocols
1218 if (gEfiShellProtocol
!= NULL
) {
1219 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1223 // This feature does not exist under EFI shell
1225 return (EFI_UNSUPPORTED
);
1229 Cause the shell to parse and execute a command line.
1231 This function creates a nested instance of the shell and executes the specified
1232 command (CommandLine) with the specified environment (Environment). Upon return,
1233 the status code returned by the specified command is placed in StatusCode.
1234 If Environment is NULL, then the current environment is used and all changes made
1235 by the commands executed will be reflected in the current environment. If the
1236 Environment is non-NULL, then the changes made will be discarded.
1237 The CommandLine is executed from the current working directory on the current
1240 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1241 environment. The values pointed to by the parameters will be unchanged by the
1242 ShellExecute() function. The Output parameter has no effect in a
1243 UEFI Shell 2.0 environment.
1245 @param[in] ParentHandle The parent image starting the operation.
1246 @param[in] CommandLine The pointer to a NULL terminated command line.
1247 @param[in] Output True to display debug output. False to hide it.
1248 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1249 in the form "x=y". If NULL, the current set is used.
1250 @param[out] Status The status of the run command line.
1252 @retval EFI_SUCCESS The operation completed sucessfully. Status
1253 contains the status code returned.
1254 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1255 @retval EFI_OUT_OF_RESOURCES Out of resources.
1256 @retval EFI_UNSUPPORTED The operation is not allowed.
1261 IN EFI_HANDLE
*ParentHandle
,
1262 IN CHAR16
*CommandLine OPTIONAL
,
1263 IN BOOLEAN Output OPTIONAL
,
1264 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1265 OUT EFI_STATUS
*Status OPTIONAL
1268 EFI_STATUS CmdStatus
;
1270 // Check for UEFI Shell 2.0 protocols
1272 if (gEfiShellProtocol
!= NULL
) {
1274 // Call UEFI Shell 2.0 version (not using Output parameter)
1276 return (gEfiShellProtocol
->Execute(ParentHandle
,
1278 EnvironmentVariables
,
1283 // Check for EFI shell
1285 if (mEfiShellEnvironment2
!= NULL
) {
1287 // Call EFI Shell version.
1288 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1290 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1294 // No Status output parameter so just use the returned status
1296 if (Status
!= NULL
) {
1297 *Status
= CmdStatus
;
1300 // If there was an error, we can't tell if it was from the command or from
1301 // the Execute() function, so we'll just assume the shell ran successfully
1302 // and the error came from the command.
1307 return (EFI_UNSUPPORTED
);
1311 Retreives the current directory path
1313 If the DeviceName is NULL, it returns the current device's current directory
1314 name. If the DeviceName is not NULL, it returns the current directory name
1317 Note that the current directory string should exclude the tailing backslash character.
1319 @param DeviceName the name of the drive to get directory on
1321 @retval NULL the directory does not exist
1322 @return != NULL the directory
1326 ShellGetCurrentDir (
1327 IN CHAR16
* CONST DeviceName OPTIONAL
1331 // Check for UEFI Shell 2.0 protocols
1333 if (gEfiShellProtocol
!= NULL
) {
1334 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1338 // Check for EFI shell
1340 if (mEfiShellEnvironment2
!= NULL
) {
1341 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1347 sets (enabled or disabled) the page break mode
1349 when page break mode is enabled the screen will stop scrolling
1350 and wait for operator input before scrolling a subsequent screen.
1352 @param CurrentState TRUE to enable and FALSE to disable
1356 ShellSetPageBreakMode (
1357 IN BOOLEAN CurrentState
1361 // check for enabling
1363 if (CurrentState
!= 0x00) {
1365 // check for UEFI Shell 2.0
1367 if (gEfiShellProtocol
!= NULL
) {
1369 // Enable with UEFI 2.0 Shell
1371 gEfiShellProtocol
->EnablePageBreak();
1375 // Check for EFI shell
1377 if (mEfiShellEnvironment2
!= NULL
) {
1379 // Enable with EFI Shell
1381 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1387 // check for UEFI Shell 2.0
1389 if (gEfiShellProtocol
!= NULL
) {
1391 // Disable with UEFI 2.0 Shell
1393 gEfiShellProtocol
->DisablePageBreak();
1397 // Check for EFI shell
1399 if (mEfiShellEnvironment2
!= NULL
) {
1401 // Disable with EFI Shell
1403 mEfiShellEnvironment2
->DisablePageBreak ();
1411 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1412 /// This allows for the struct to be populated.
1419 SHELL_FILE_HANDLE Handle
;
1420 EFI_FILE_INFO
*Info
;
1421 } EFI_SHELL_FILE_INFO_NO_CONST
;
1424 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1426 if OldStyleFileList is NULL then ASSERT()
1428 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1429 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1430 the ShellCloseFileMetaArg function.
1432 @param[in] FileList the EFI shell list type
1433 @param[in, out] ListHead the list to add to
1435 @retval the resultant head of the double linked new format list;
1438 InternalShellConvertFileListType (
1439 IN LIST_ENTRY
*FileList
,
1440 IN OUT LIST_ENTRY
*ListHead
1443 SHELL_FILE_ARG
*OldInfo
;
1445 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1450 ASSERT(FileList
!= NULL
);
1451 ASSERT(ListHead
!= NULL
);
1454 // enumerate through each member of the old list and copy
1456 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1457 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1458 ASSERT(OldInfo
!= NULL
);
1461 // Skip ones that failed to open...
1463 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1468 // make sure the old list was valid
1470 ASSERT(OldInfo
->Info
!= NULL
);
1471 ASSERT(OldInfo
->FullName
!= NULL
);
1472 ASSERT(OldInfo
->FileName
!= NULL
);
1475 // allocate a new EFI_SHELL_FILE_INFO object
1477 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1478 if (NewInfo
== NULL
) {
1479 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1485 // copy the simple items
1487 NewInfo
->Handle
= OldInfo
->Handle
;
1488 NewInfo
->Status
= OldInfo
->Status
;
1490 // old shell checks for 0 not NULL
1491 OldInfo
->Handle
= 0;
1494 // allocate new space to copy strings and structure
1496 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1497 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1498 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1501 // make sure all the memory allocations were sucessful
1503 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1505 // Free the partially allocated new node
1507 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1508 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1509 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1510 SHELL_FREE_NON_NULL(NewInfo
);
1513 // Free the previously converted stuff
1515 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1521 // add that to the list
1523 InsertTailList(ListHead
, &NewInfo
->Link
);
1528 Opens a group of files based on a path.
1530 This function uses the Arg to open all the matching files. Each matched
1531 file has a SHELL_FILE_INFO structure to record the file information. These
1532 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1533 structures from ListHead to access each file. This function supports wildcards
1534 and will process '?' and '*' as such. the list must be freed with a call to
1535 ShellCloseFileMetaArg().
1537 If you are NOT appending to an existing list *ListHead must be NULL. If
1538 *ListHead is NULL then it must be callee freed.
1540 @param Arg pointer to path string
1541 @param OpenMode mode to open files with
1542 @param ListHead head of linked list of results
1544 @retval EFI_SUCCESS the operation was sucessful and the list head
1545 contains the list of opened files
1546 @return != EFI_SUCCESS the operation failed
1548 @sa InternalShellConvertFileListType
1552 ShellOpenFileMetaArg (
1555 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1559 LIST_ENTRY mOldStyleFileList
;
1560 CHAR16
*CleanFilePathStr
;
1563 // ASSERT that Arg and ListHead are not NULL
1565 ASSERT(Arg
!= NULL
);
1566 ASSERT(ListHead
!= NULL
);
1568 CleanFilePathStr
= NULL
;
1570 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1571 if (EFI_ERROR (Status
)) {
1576 // Check for UEFI Shell 2.0 protocols
1578 if (gEfiShellProtocol
!= NULL
) {
1579 if (*ListHead
== NULL
) {
1580 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1581 if (*ListHead
== NULL
) {
1582 FreePool(CleanFilePathStr
);
1583 return (EFI_OUT_OF_RESOURCES
);
1585 InitializeListHead(&((*ListHead
)->Link
));
1587 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1590 if (EFI_ERROR(Status
)) {
1591 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1593 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1595 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1596 FreePool(*ListHead
);
1597 FreePool(CleanFilePathStr
);
1599 return (EFI_NOT_FOUND
);
1601 FreePool(CleanFilePathStr
);
1606 // Check for EFI shell
1608 if (mEfiShellEnvironment2
!= NULL
) {
1610 // make sure the list head is initialized
1612 InitializeListHead(&mOldStyleFileList
);
1615 // Get the EFI Shell list of files
1617 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1618 if (EFI_ERROR(Status
)) {
1620 FreePool(CleanFilePathStr
);
1624 if (*ListHead
== NULL
) {
1625 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1626 if (*ListHead
== NULL
) {
1627 FreePool(CleanFilePathStr
);
1628 return (EFI_OUT_OF_RESOURCES
);
1630 InitializeListHead(&((*ListHead
)->Link
));
1634 // Convert that to equivalent of UEFI Shell 2.0 structure
1636 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1639 // Free the EFI Shell version that was converted.
1641 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1643 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1644 FreePool(*ListHead
);
1646 Status
= EFI_NOT_FOUND
;
1648 FreePool(CleanFilePathStr
);
1652 FreePool(CleanFilePathStr
);
1653 return (EFI_UNSUPPORTED
);
1656 Free the linked list returned from ShellOpenFileMetaArg.
1658 if ListHead is NULL then ASSERT().
1660 @param ListHead the pointer to free.
1662 @retval EFI_SUCCESS the operation was sucessful.
1666 ShellCloseFileMetaArg (
1667 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1673 // ASSERT that ListHead is not NULL
1675 ASSERT(ListHead
!= NULL
);
1678 // Check for UEFI Shell 2.0 protocols
1680 if (gEfiShellProtocol
!= NULL
) {
1681 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1682 } else if (mEfiShellEnvironment2
!= NULL
) {
1684 // Since this is EFI Shell version we need to free our internally made copy
1687 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1688 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1689 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1690 RemoveEntryList(Node
);
1691 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1692 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1693 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1694 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1695 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1697 SHELL_FREE_NON_NULL(*ListHead
);
1701 return (EFI_UNSUPPORTED
);
1705 Find a file by searching the CWD and then the path.
1707 If FileName is NULL then ASSERT.
1709 If the return value is not NULL then the memory must be caller freed.
1711 @param FileName Filename string.
1713 @retval NULL the file was not found
1714 @return !NULL the full path to the file.
1719 IN CONST CHAR16
*FileName
1723 SHELL_FILE_HANDLE Handle
;
1727 CONST CHAR16
*Walker
;
1734 // First make sure its not an absolute path.
1736 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1737 if (!EFI_ERROR(Status
)){
1738 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1739 ASSERT(RetVal
== NULL
);
1740 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1741 ShellCloseFile(&Handle
);
1744 ShellCloseFile(&Handle
);
1748 Path
= ShellGetEnvironmentVariable(L
"cwd");
1750 Size
= StrSize(Path
) + sizeof(CHAR16
);
1751 Size
+= StrSize(FileName
);
1752 TestPath
= AllocateZeroPool(Size
);
1753 if (TestPath
== NULL
) {
1756 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), Path
);
1757 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1758 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1759 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1760 if (!EFI_ERROR(Status
)){
1761 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1762 ASSERT(RetVal
== NULL
);
1763 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1764 ShellCloseFile(&Handle
);
1768 ShellCloseFile(&Handle
);
1773 Path
= ShellGetEnvironmentVariable(L
"path");
1775 Size
= StrSize(Path
)+sizeof(CHAR16
);
1776 Size
+= StrSize(FileName
);
1777 TestPath
= AllocateZeroPool(Size
);
1778 if (TestPath
== NULL
) {
1781 Walker
= (CHAR16
*)Path
;
1783 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1784 if (TestPath
!= NULL
) {
1785 TempChar
= StrStr(TestPath
, L
";");
1786 if (TempChar
!= NULL
) {
1787 *TempChar
= CHAR_NULL
;
1789 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1790 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1792 if (FileName
[0] == L
'\\') {
1795 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1796 if (StrStr(Walker
, L
";") != NULL
) {
1797 Walker
= StrStr(Walker
, L
";") + 1;
1801 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1802 if (!EFI_ERROR(Status
)){
1803 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1804 ASSERT(RetVal
== NULL
);
1805 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1806 ShellCloseFile(&Handle
);
1809 ShellCloseFile(&Handle
);
1813 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1820 Find a file by searching the CWD and then the path with a variable set of file
1821 extensions. If the file is not found it will append each extension in the list
1822 in the order provided and return the first one that is successful.
1824 If FileName is NULL, then ASSERT.
1825 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1827 If the return value is not NULL then the memory must be caller freed.
1829 @param[in] FileName Filename string.
1830 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1832 @retval NULL The file was not found.
1833 @retval !NULL The path to the file.
1837 ShellFindFilePathEx (
1838 IN CONST CHAR16
*FileName
,
1839 IN CONST CHAR16
*FileExtension
1844 CONST CHAR16
*ExtensionWalker
;
1849 ASSERT(FileName
!= NULL
);
1850 if (FileExtension
== NULL
) {
1851 return (ShellFindFilePath(FileName
));
1853 RetVal
= ShellFindFilePath(FileName
);
1854 if (RetVal
!= NULL
) {
1857 Size
= StrSize(FileName
);
1858 Size
+= StrSize(FileExtension
);
1859 TestPath
= AllocateZeroPool(Size
);
1860 if (TestPath
== NULL
) {
1863 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1864 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1865 if (ExtensionWalker
!= NULL
) {
1866 StrCatS(TestPath
, Size
/sizeof(CHAR16
), ExtensionWalker
);
1868 TempChar
= StrStr(TestPath
, L
";");
1869 if (TempChar
!= NULL
) {
1870 *TempChar
= CHAR_NULL
;
1872 RetVal
= ShellFindFilePath(TestPath
);
1873 if (RetVal
!= NULL
) {
1876 ASSERT(ExtensionWalker
!= NULL
);
1877 TempChar2
= StrStr(ExtensionWalker
, L
";");
1886 SHELL_PARAM_TYPE Type
;
1888 UINTN OriginalPosition
;
1889 } SHELL_PARAM_PACKAGE
;
1892 Checks the list of valid arguments and returns TRUE if the item was found. If the
1893 return value is TRUE then the type parameter is set also.
1895 if CheckList is NULL then ASSERT();
1896 if Name is NULL then ASSERT();
1897 if Type is NULL then ASSERT();
1899 @param Name pointer to Name of parameter found
1900 @param CheckList List to check against
1901 @param Type pointer to type of parameter if it was found
1903 @retval TRUE the Parameter was found. Type is valid.
1904 @retval FALSE the Parameter was not found. Type is not valid.
1907 InternalIsOnCheckList (
1908 IN CONST CHAR16
*Name
,
1909 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1910 OUT SHELL_PARAM_TYPE
*Type
1913 SHELL_PARAM_ITEM
*TempListItem
;
1917 // ASSERT that all 3 pointer parameters aren't NULL
1919 ASSERT(CheckList
!= NULL
);
1920 ASSERT(Type
!= NULL
);
1921 ASSERT(Name
!= NULL
);
1924 // question mark and page break mode are always supported
1926 if ((StrCmp(Name
, L
"-?") == 0) ||
1927 (StrCmp(Name
, L
"-b") == 0)
1934 // Enumerate through the list
1936 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1938 // If the Type is TypeStart only check the first characters of the passed in param
1939 // If it matches set the type and return TRUE
1941 if (TempListItem
->Type
== TypeStart
) {
1942 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1943 *Type
= TempListItem
->Type
;
1947 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1948 if (TempString
!= NULL
) {
1949 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1950 *Type
= TempListItem
->Type
;
1951 FreePool(TempString
);
1954 FreePool(TempString
);
1956 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1957 *Type
= TempListItem
->Type
;
1965 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1967 @param[in] Name pointer to Name of parameter found
1968 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1969 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1971 @retval TRUE the Parameter is a flag.
1972 @retval FALSE the Parameter not a flag.
1976 IN CONST CHAR16
*Name
,
1977 IN CONST BOOLEAN AlwaysAllowNumbers
,
1978 IN CONST BOOLEAN TimeNumbers
1982 // ASSERT that Name isn't NULL
1984 ASSERT(Name
!= NULL
);
1987 // If we accept numbers then dont return TRUE. (they will be values)
1989 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
1994 // If the Name has a /, +, or - as the first character return TRUE
1996 if ((Name
[0] == L
'/') ||
1997 (Name
[0] == L
'-') ||
2006 Checks the command line arguments passed against the list of valid ones.
2008 If no initialization is required, then return RETURN_SUCCESS.
2010 @param[in] CheckList pointer to list of parameters to check
2011 @param[out] CheckPackage pointer to pointer to list checked values
2012 @param[out] ProblemParam optional pointer to pointer to unicode string for
2013 the paramater that caused failure. If used then the
2014 caller is responsible for freeing the memory.
2015 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2016 @param[in] Argv pointer to array of parameters
2017 @param[in] Argc Count of parameters in Argv
2018 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
2020 @retval EFI_SUCCESS The operation completed sucessfully.
2021 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2022 @retval EFI_INVALID_PARAMETER A parameter was invalid
2023 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2024 duplicated. the duplicated command line argument
2025 was returned in ProblemParam if provided.
2026 @retval EFI_NOT_FOUND a argument required a value that was missing.
2027 the invalid command line argument was returned in
2028 ProblemParam if provided.
2031 InternalCommandLineParse (
2032 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2033 OUT LIST_ENTRY
**CheckPackage
,
2034 OUT CHAR16
**ProblemParam OPTIONAL
,
2035 IN BOOLEAN AutoPageBreak
,
2036 IN CONST CHAR16
**Argv
,
2038 IN BOOLEAN AlwaysAllowNumbers
2042 SHELL_PARAM_TYPE CurrentItemType
;
2043 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2047 CONST CHAR16
*TempPointer
;
2048 UINTN CurrentValueSize
;
2051 CurrentItemPackage
= NULL
;
2057 // If there is only 1 item we dont need to do anything
2060 *CheckPackage
= NULL
;
2061 return (EFI_SUCCESS
);
2067 ASSERT(CheckList
!= NULL
);
2068 ASSERT(Argv
!= NULL
);
2071 // initialize the linked list
2073 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2074 if (*CheckPackage
== NULL
) {
2075 return (EFI_OUT_OF_RESOURCES
);
2078 InitializeListHead(*CheckPackage
);
2081 // loop through each of the arguments
2083 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2084 if (Argv
[LoopCounter
] == NULL
) {
2086 // do nothing for NULL argv
2088 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2090 // We might have leftover if last parameter didnt have optional value
2092 if (GetItemValue
!= 0) {
2094 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2099 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2100 if (CurrentItemPackage
== NULL
) {
2101 ShellCommandLineFreeVarList(*CheckPackage
);
2102 *CheckPackage
= NULL
;
2103 return (EFI_OUT_OF_RESOURCES
);
2105 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2106 if (CurrentItemPackage
->Name
== NULL
) {
2107 ShellCommandLineFreeVarList(*CheckPackage
);
2108 *CheckPackage
= NULL
;
2109 return (EFI_OUT_OF_RESOURCES
);
2111 CurrentItemPackage
->Type
= CurrentItemType
;
2112 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2113 CurrentItemPackage
->Value
= NULL
;
2116 // Does this flag require a value
2118 switch (CurrentItemPackage
->Type
) {
2120 // possibly trigger the next loop(s) to populate the value of this item
2127 case TypeDoubleValue
:
2132 GetItemValue
= (UINTN
)(-1);
2137 // this item has no value expected; we are done
2139 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2140 ASSERT(GetItemValue
== 0);
2143 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, (BOOLEAN
)(CurrentItemPackage
->Type
== TypeTimeValue
))) {
2145 // get the item VALUE for a previous flag
2147 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2148 NewValue
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2149 if (NewValue
== NULL
) {
2150 SHELL_FREE_NON_NULL (CurrentItemPackage
->Value
);
2151 SHELL_FREE_NON_NULL (CurrentItemPackage
);
2152 ShellCommandLineFreeVarList (*CheckPackage
);
2153 *CheckPackage
= NULL
;
2154 return EFI_OUT_OF_RESOURCES
;
2156 CurrentItemPackage
->Value
= NewValue
;
2157 if (ValueSize
== 0) {
2158 StrCpyS( CurrentItemPackage
->Value
,
2159 CurrentValueSize
/sizeof(CHAR16
),
2163 StrCatS( CurrentItemPackage
->Value
,
2164 CurrentValueSize
/sizeof(CHAR16
),
2167 StrCatS( CurrentItemPackage
->Value
,
2168 CurrentValueSize
/sizeof(CHAR16
),
2172 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2175 if (GetItemValue
== 0) {
2176 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2178 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2180 // add this one as a non-flag
2183 TempPointer
= Argv
[LoopCounter
];
2184 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2185 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2186 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2190 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2191 if (CurrentItemPackage
== NULL
) {
2192 ShellCommandLineFreeVarList(*CheckPackage
);
2193 *CheckPackage
= NULL
;
2194 return (EFI_OUT_OF_RESOURCES
);
2196 CurrentItemPackage
->Name
= NULL
;
2197 CurrentItemPackage
->Type
= TypePosition
;
2198 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2199 if (CurrentItemPackage
->Value
== NULL
) {
2200 ShellCommandLineFreeVarList(*CheckPackage
);
2201 *CheckPackage
= NULL
;
2202 return (EFI_OUT_OF_RESOURCES
);
2204 CurrentItemPackage
->OriginalPosition
= Count
++;
2205 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2208 // this was a non-recognised flag... error!
2210 if (ProblemParam
!= NULL
) {
2211 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2213 ShellCommandLineFreeVarList(*CheckPackage
);
2214 *CheckPackage
= NULL
;
2215 return (EFI_VOLUME_CORRUPTED
);
2218 if (GetItemValue
!= 0) {
2220 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2223 // support for AutoPageBreak
2225 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2226 ShellSetPageBreakMode(TRUE
);
2228 return (EFI_SUCCESS
);
2232 Checks the command line arguments passed against the list of valid ones.
2233 Optionally removes NULL values first.
2235 If no initialization is required, then return RETURN_SUCCESS.
2237 @param[in] CheckList The pointer to list of parameters to check.
2238 @param[out] CheckPackage The package of checked values.
2239 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2240 the paramater that caused failure.
2241 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2242 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2244 @retval EFI_SUCCESS The operation completed sucessfully.
2245 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2246 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2247 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2248 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2249 of the command line arguments was returned in
2250 ProblemParam if provided.
2251 @retval EFI_NOT_FOUND A argument required a value that was missing.
2252 The invalid command line argument was returned in
2253 ProblemParam if provided.
2257 ShellCommandLineParseEx (
2258 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2259 OUT LIST_ENTRY
**CheckPackage
,
2260 OUT CHAR16
**ProblemParam OPTIONAL
,
2261 IN BOOLEAN AutoPageBreak
,
2262 IN BOOLEAN AlwaysAllowNumbers
2266 // ASSERT that CheckList and CheckPackage aren't NULL
2268 ASSERT(CheckList
!= NULL
);
2269 ASSERT(CheckPackage
!= NULL
);
2272 // Check for UEFI Shell 2.0 protocols
2274 if (gEfiShellParametersProtocol
!= NULL
) {
2275 return (InternalCommandLineParse(CheckList
,
2279 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2280 gEfiShellParametersProtocol
->Argc
,
2281 AlwaysAllowNumbers
));
2285 // ASSERT That EFI Shell is not required
2287 ASSERT (mEfiShellInterface
!= NULL
);
2288 return (InternalCommandLineParse(CheckList
,
2292 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2293 mEfiShellInterface
->Argc
,
2294 AlwaysAllowNumbers
));
2298 Frees shell variable list that was returned from ShellCommandLineParse.
2300 This function will free all the memory that was used for the CheckPackage
2301 list of postprocessed shell arguments.
2303 this function has no return value.
2305 if CheckPackage is NULL, then return
2307 @param CheckPackage the list to de-allocate
2311 ShellCommandLineFreeVarList (
2312 IN LIST_ENTRY
*CheckPackage
2318 // check for CheckPackage == NULL
2320 if (CheckPackage
== NULL
) {
2325 // for each node in the list
2327 for ( Node
= GetFirstNode(CheckPackage
)
2328 ; !IsListEmpty(CheckPackage
)
2329 ; Node
= GetFirstNode(CheckPackage
)
2332 // Remove it from the list
2334 RemoveEntryList(Node
);
2337 // if it has a name free the name
2339 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2340 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2344 // if it has a value free the value
2346 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2347 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2351 // free the node structure
2353 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2356 // free the list head node
2358 FreePool(CheckPackage
);
2361 Checks for presence of a flag parameter
2363 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2365 if CheckPackage is NULL then return FALSE.
2366 if KeyString is NULL then ASSERT()
2368 @param CheckPackage The package of parsed command line arguments
2369 @param KeyString the Key of the command line argument to check for
2371 @retval TRUE the flag is on the command line
2372 @retval FALSE the flag is not on the command line
2376 ShellCommandLineGetFlag (
2377 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2378 IN CONST CHAR16
* CONST KeyString
2385 // return FALSE for no package or KeyString is NULL
2387 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2392 // enumerate through the list of parametrs
2394 for ( Node
= GetFirstNode(CheckPackage
)
2395 ; !IsNull (CheckPackage
, Node
)
2396 ; Node
= GetNextNode(CheckPackage
, Node
)
2399 // If the Name matches, return TRUE (and there may be NULL name)
2401 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2403 // If Type is TypeStart then only compare the begining of the strings
2405 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2406 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2410 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2411 if (TempString
!= NULL
) {
2412 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2413 FreePool(TempString
);
2416 FreePool(TempString
);
2418 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2426 Returns value from command line argument.
2428 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2430 If CheckPackage is NULL, then return NULL.
2432 @param[in] CheckPackage The package of parsed command line arguments.
2433 @param[in] KeyString The Key of the command line argument to check for.
2435 @retval NULL The flag is not on the command line.
2436 @retval !=NULL The pointer to unicode string of the value.
2440 ShellCommandLineGetValue (
2441 IN CONST LIST_ENTRY
*CheckPackage
,
2442 IN CHAR16
*KeyString
2449 // return NULL for no package or KeyString is NULL
2451 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2456 // enumerate through the list of parametrs
2458 for ( Node
= GetFirstNode(CheckPackage
)
2459 ; !IsNull (CheckPackage
, Node
)
2460 ; Node
= GetNextNode(CheckPackage
, Node
)
2463 // If the Name matches, return TRUE (and there may be NULL name)
2465 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2467 // If Type is TypeStart then only compare the begining of the strings
2469 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2470 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2471 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2474 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2475 if (TempString
!= NULL
) {
2476 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2477 FreePool(TempString
);
2478 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2480 FreePool(TempString
);
2482 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2483 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2491 Returns raw value from command line argument.
2493 Raw value parameters are in the form of "value" in a specific position in the list.
2495 If CheckPackage is NULL, then return NULL.
2497 @param[in] CheckPackage The package of parsed command line arguments.
2498 @param[in] Position The position of the value.
2500 @retval NULL The flag is not on the command line.
2501 @retval !=NULL The pointer to unicode string of the value.
2505 ShellCommandLineGetRawValue (
2506 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2513 // check for CheckPackage == NULL
2515 if (CheckPackage
== NULL
) {
2520 // enumerate through the list of parametrs
2522 for ( Node
= GetFirstNode(CheckPackage
)
2523 ; !IsNull (CheckPackage
, Node
)
2524 ; Node
= GetNextNode(CheckPackage
, Node
)
2527 // If the position matches, return the value
2529 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2530 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2537 returns the number of command line value parameters that were parsed.
2539 this will not include flags.
2541 @param[in] CheckPackage The package of parsed command line arguments.
2543 @retval (UINTN)-1 No parsing has ocurred
2544 @return other The number of value parameters found
2548 ShellCommandLineGetCount(
2549 IN CONST LIST_ENTRY
*CheckPackage
2555 if (CheckPackage
== NULL
) {
2558 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2559 ; !IsNull (CheckPackage
, Node1
)
2560 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2562 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2570 Determines if a parameter is duplicated.
2572 If Param is not NULL then it will point to a callee allocated string buffer
2573 with the parameter value if a duplicate is found.
2575 If CheckPackage is NULL, then ASSERT.
2577 @param[in] CheckPackage The package of parsed command line arguments.
2578 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2580 @retval EFI_SUCCESS No parameters were duplicated.
2581 @retval EFI_DEVICE_ERROR A duplicate was found.
2585 ShellCommandLineCheckDuplicate (
2586 IN CONST LIST_ENTRY
*CheckPackage
,
2593 ASSERT(CheckPackage
!= NULL
);
2595 for ( Node1
= GetFirstNode(CheckPackage
)
2596 ; !IsNull (CheckPackage
, Node1
)
2597 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2599 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2600 ; !IsNull (CheckPackage
, Node2
)
2601 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2603 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) {
2604 if (Param
!= NULL
) {
2606 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2608 return (EFI_DEVICE_ERROR
);
2612 return (EFI_SUCCESS
);
2616 This is a find and replace function. Upon successful return the NewString is a copy of
2617 SourceString with each instance of FindTarget replaced with ReplaceWith.
2619 If SourceString and NewString overlap the behavior is undefined.
2621 If the string would grow bigger than NewSize it will halt and return error.
2623 @param[in] SourceString The string with source buffer.
2624 @param[in, out] NewString The string with resultant buffer.
2625 @param[in] NewSize The size in bytes of NewString.
2626 @param[in] FindTarget The string to look for.
2627 @param[in] ReplaceWith The string to replace FindTarget with.
2628 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2629 immediately before it.
2630 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2632 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2633 @retval EFI_INVALID_PARAMETER NewString was NULL.
2634 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2635 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2636 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2637 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2638 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2639 the new string (truncation occurred).
2640 @retval EFI_SUCCESS The string was successfully copied with replacement.
2644 ShellCopySearchAndReplace(
2645 IN CHAR16 CONST
*SourceString
,
2646 IN OUT CHAR16
*NewString
,
2648 IN CONST CHAR16
*FindTarget
,
2649 IN CONST CHAR16
*ReplaceWith
,
2650 IN CONST BOOLEAN SkipPreCarrot
,
2651 IN CONST BOOLEAN ParameterReplacing
2657 if ( (SourceString
== NULL
)
2658 || (NewString
== NULL
)
2659 || (FindTarget
== NULL
)
2660 || (ReplaceWith
== NULL
)
2661 || (StrLen(FindTarget
) < 1)
2662 || (StrLen(SourceString
) < 1)
2664 return (EFI_INVALID_PARAMETER
);
2667 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2668 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2670 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2671 if (Replace
!= NULL
) {
2672 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2675 if (Replace
== NULL
) {
2676 return (EFI_OUT_OF_RESOURCES
);
2678 NewString
= ZeroMem(NewString
, NewSize
);
2679 while (*SourceString
!= CHAR_NULL
) {
2681 // if we find the FindTarget and either Skip == FALSE or Skip and we
2682 // dont have a carrot do a replace...
2684 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2685 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2687 SourceString
+= StrLen(FindTarget
);
2688 Size
= StrSize(NewString
);
2689 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2691 return (EFI_BUFFER_TOO_SMALL
);
2693 StrCatS(NewString
, NewSize
/sizeof(CHAR16
), Replace
);
2695 Size
= StrSize(NewString
);
2696 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2698 return (EFI_BUFFER_TOO_SMALL
);
2700 StrnCatS(NewString
, NewSize
/sizeof(CHAR16
), SourceString
, 1);
2705 return (EFI_SUCCESS
);
2709 Internal worker function to output a string.
2711 This function will output a string to the correct StdOut.
2713 @param[in] String The string to print out.
2715 @retval EFI_SUCCESS The operation was sucessful.
2716 @retval !EFI_SUCCESS The operation failed.
2720 IN CONST CHAR16
*String
2724 Size
= StrSize(String
) - sizeof(CHAR16
);
2726 return (EFI_SUCCESS
);
2728 if (gEfiShellParametersProtocol
!= NULL
) {
2729 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2731 if (mEfiShellInterface
!= NULL
) {
2732 if (mEfiShellInterface
->RedirArgc
== 0) {
2734 // Divide in half for old shell. Must be string length not size.
2736 Size
/=2; // Divide in half only when no redirection.
2738 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2741 return (EFI_UNSUPPORTED
);
2745 Print at a specific location on the screen.
2747 This function will move the cursor to a given screen location and print the specified string
2749 If -1 is specified for either the Row or Col the current screen location for BOTH
2752 if either Row or Col is out of range for the current console, then ASSERT
2753 if Format is NULL, then ASSERT
2755 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2756 the following additional flags:
2757 %N - Set output attribute to normal
2758 %H - Set output attribute to highlight
2759 %E - Set output attribute to error
2760 %B - Set output attribute to blue color
2761 %V - Set output attribute to green color
2763 Note: The background color is controlled by the shell command cls.
2765 @param[in] Col the column to print at
2766 @param[in] Row the row to print at
2767 @param[in] Format the format string
2768 @param[in] Marker the marker for the variable argument list
2770 @return EFI_SUCCESS The operation was successful.
2771 @return EFI_DEVICE_ERROR The console device reported an error.
2774 InternalShellPrintWorker(
2775 IN INT32 Col OPTIONAL
,
2776 IN INT32 Row OPTIONAL
,
2777 IN CONST CHAR16
*Format
,
2782 CHAR16
*ResumeLocation
;
2783 CHAR16
*FormatWalker
;
2784 UINTN OriginalAttribute
;
2785 CHAR16
*mPostReplaceFormat
;
2786 CHAR16
*mPostReplaceFormat2
;
2788 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2789 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2791 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2792 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2793 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2794 return (EFI_OUT_OF_RESOURCES
);
2797 Status
= EFI_SUCCESS
;
2798 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2801 // Back and forth each time fixing up 1 of our flags...
2803 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2804 ASSERT_EFI_ERROR(Status
);
2805 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2806 ASSERT_EFI_ERROR(Status
);
2807 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2808 ASSERT_EFI_ERROR(Status
);
2809 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2810 ASSERT_EFI_ERROR(Status
);
2811 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2812 ASSERT_EFI_ERROR(Status
);
2815 // Use the last buffer from replacing to print from...
2817 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2819 if (Col
!= -1 && Row
!= -1) {
2820 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2823 FormatWalker
= mPostReplaceFormat2
;
2824 while (*FormatWalker
!= CHAR_NULL
) {
2826 // Find the next attribute change request
2828 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2829 if (ResumeLocation
!= NULL
) {
2830 *ResumeLocation
= CHAR_NULL
;
2833 // print the current FormatWalker string
2835 if (StrLen(FormatWalker
)>0) {
2836 Status
= InternalPrintTo(FormatWalker
);
2837 if (EFI_ERROR(Status
)) {
2843 // update the attribute
2845 if (ResumeLocation
!= NULL
) {
2846 if ((ResumeLocation
!= mPostReplaceFormat2
) && (*(ResumeLocation
-1) == L
'^')) {
2848 // Move cursor back 1 position to overwrite the ^
2850 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2853 // Print a simple '%' symbol
2855 Status
= InternalPrintTo(L
"%");
2856 ResumeLocation
= ResumeLocation
- 1;
2858 switch (*(ResumeLocation
+1)) {
2860 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2863 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2866 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2869 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_LIGHTBLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2872 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_LIGHTGREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2876 // Print a simple '%' symbol
2878 Status
= InternalPrintTo(L
"%");
2879 if (EFI_ERROR(Status
)) {
2882 ResumeLocation
= ResumeLocation
- 1;
2888 // reset to normal now...
2894 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2896 FormatWalker
= ResumeLocation
+ 2;
2899 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2901 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2902 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2907 Print at a specific location on the screen.
2909 This function will move the cursor to a given screen location and print the specified string.
2911 If -1 is specified for either the Row or Col the current screen location for BOTH
2914 If either Row or Col is out of range for the current console, then ASSERT.
2915 If Format is NULL, then ASSERT.
2917 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2918 the following additional flags:
2919 %N - Set output attribute to normal
2920 %H - Set output attribute to highlight
2921 %E - Set output attribute to error
2922 %B - Set output attribute to blue color
2923 %V - Set output attribute to green color
2925 Note: The background color is controlled by the shell command cls.
2927 @param[in] Col the column to print at
2928 @param[in] Row the row to print at
2929 @param[in] Format the format string
2930 @param[in] ... The variable argument list.
2932 @return EFI_SUCCESS The printing was successful.
2933 @return EFI_DEVICE_ERROR The console device reported an error.
2938 IN INT32 Col OPTIONAL
,
2939 IN INT32 Row OPTIONAL
,
2940 IN CONST CHAR16
*Format
,
2946 if (Format
== NULL
) {
2947 return (EFI_INVALID_PARAMETER
);
2949 VA_START (Marker
, Format
);
2950 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2956 Print at a specific location on the screen.
2958 This function will move the cursor to a given screen location and print the specified string.
2960 If -1 is specified for either the Row or Col the current screen location for BOTH
2963 If either Row or Col is out of range for the current console, then ASSERT.
2964 If Format is NULL, then ASSERT.
2966 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2967 the following additional flags:
2968 %N - Set output attribute to normal.
2969 %H - Set output attribute to highlight.
2970 %E - Set output attribute to error.
2971 %B - Set output attribute to blue color.
2972 %V - Set output attribute to green color.
2974 Note: The background color is controlled by the shell command cls.
2976 @param[in] Col The column to print at.
2977 @param[in] Row The row to print at.
2978 @param[in] Language The language of the string to retrieve. If this parameter
2979 is NULL, then the current platform language is used.
2980 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2981 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2982 @param[in] ... The variable argument list.
2984 @return EFI_SUCCESS The printing was successful.
2985 @return EFI_DEVICE_ERROR The console device reported an error.
2990 IN INT32 Col OPTIONAL
,
2991 IN INT32 Row OPTIONAL
,
2992 IN CONST CHAR8
*Language OPTIONAL
,
2993 IN CONST EFI_STRING_ID HiiFormatStringId
,
2994 IN CONST EFI_HANDLE HiiFormatHandle
,
2999 CHAR16
*HiiFormatString
;
3002 RetVal
= EFI_DEVICE_ERROR
;
3004 VA_START (Marker
, HiiFormatHandle
);
3005 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
3006 if (HiiFormatString
!= NULL
) {
3007 RetVal
= InternalShellPrintWorker (Col
, Row
, HiiFormatString
, Marker
);
3008 SHELL_FREE_NON_NULL (HiiFormatString
);
3016 Function to determine if a given filename represents a file or a directory.
3018 @param[in] DirName Path to directory to test.
3020 @retval EFI_SUCCESS The Path represents a directory
3021 @retval EFI_NOT_FOUND The Path does not represent a directory
3022 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3023 @return The path failed to open
3028 IN CONST CHAR16
*DirName
3032 SHELL_FILE_HANDLE Handle
;
3033 CHAR16
*TempLocation
;
3034 CHAR16
*TempLocation2
;
3036 ASSERT(DirName
!= NULL
);
3039 TempLocation
= NULL
;
3041 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3042 if (EFI_ERROR(Status
)) {
3044 // try good logic first.
3046 if (gEfiShellProtocol
!= NULL
) {
3047 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3048 if (TempLocation
== NULL
) {
3049 ShellCloseFile(&Handle
);
3050 return (EFI_OUT_OF_RESOURCES
);
3052 TempLocation2
= StrStr(TempLocation
, L
":");
3053 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3054 *(TempLocation2
+1) = CHAR_NULL
;
3056 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3057 FreePool(TempLocation
);
3058 return (EFI_SUCCESS
);
3060 FreePool(TempLocation
);
3063 // probably a map name?!?!!?
3065 TempLocation
= StrStr(DirName
, L
"\\");
3066 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3067 return (EFI_SUCCESS
);
3073 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3074 ShellCloseFile(&Handle
);
3075 return (EFI_SUCCESS
);
3077 ShellCloseFile(&Handle
);
3078 return (EFI_NOT_FOUND
);
3082 Function to determine if a given filename represents a file.
3084 @param[in] Name Path to file to test.
3086 @retval EFI_SUCCESS The Path represents a file.
3087 @retval EFI_NOT_FOUND The Path does not represent a file.
3088 @retval other The path failed to open.
3093 IN CONST CHAR16
*Name
3097 SHELL_FILE_HANDLE Handle
;
3099 ASSERT(Name
!= NULL
);
3103 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3104 if (EFI_ERROR(Status
)) {
3108 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3109 ShellCloseFile(&Handle
);
3110 return (EFI_SUCCESS
);
3112 ShellCloseFile(&Handle
);
3113 return (EFI_NOT_FOUND
);
3117 Function to determine if a given filename represents a file.
3119 This will search the CWD and then the Path.
3121 If Name is NULL, then ASSERT.
3123 @param[in] Name Path to file to test.
3125 @retval EFI_SUCCESS The Path represents a file.
3126 @retval EFI_NOT_FOUND The Path does not represent a file.
3127 @retval other The path failed to open.
3132 IN CONST CHAR16
*Name
3138 if (!EFI_ERROR(ShellIsFile(Name
))) {
3139 return (EFI_SUCCESS
);
3142 NewName
= ShellFindFilePath(Name
);
3143 if (NewName
== NULL
) {
3144 return (EFI_NOT_FOUND
);
3146 Status
= ShellIsFile(NewName
);
3152 Function return the number converted from a hex representation of a number.
3154 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3155 result. Use ShellConvertStringToUint64 instead.
3157 @param[in] String String representation of a number.
3159 @return The unsigned integer result of the conversion.
3160 @retval (UINTN)(-1) An error occured.
3165 IN CONST CHAR16
*String
3170 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3171 return ((UINTN
)RetVal
);
3174 return ((UINTN
)(-1));
3178 Function to determine whether a string is decimal or hex representation of a number
3179 and return the number converted from the string. Spaces are always skipped.
3181 @param[in] String String representation of a number
3184 @retval (UINTN)(-1) An error ocurred.
3189 IN CONST CHAR16
*String
3197 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3201 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3202 return ((UINTN
)RetVal
);
3204 return ((UINTN
)(-1));
3208 Safely append with automatic string resizing given length of Destination and
3209 desired length of copy from Source.
3211 append the first D characters of Source to the end of Destination, where D is
3212 the lesser of Count and the StrLen() of Source. If appending those D characters
3213 will fit within Destination (whose Size is given as CurrentSize) and
3214 still leave room for a NULL terminator, then those characters are appended,
3215 starting at the original terminating NULL of Destination, and a new terminating
3218 If appending D characters onto Destination will result in a overflow of the size
3219 given in CurrentSize the string will be grown such that the copy can be performed
3220 and CurrentSize will be updated to the new size.
3222 If Source is NULL, there is nothing to append, just return the current buffer in
3225 if Destination is NULL, then ASSERT()
3226 if Destination's current length (including NULL terminator) is already more then
3227 CurrentSize, then ASSERT()
3229 @param[in, out] Destination The String to append onto
3230 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3231 return possibly the new size (still in bytes). if NULL
3232 then allocate whatever is needed.
3233 @param[in] Source The String to append from
3234 @param[in] Count Maximum number of characters to append. if 0 then
3237 @return Destination return the resultant string.
3242 IN OUT CHAR16
**Destination
,
3243 IN OUT UINTN
*CurrentSize
,
3244 IN CONST CHAR16
*Source
,
3248 UINTN DestinationStartSize
;
3254 ASSERT(Destination
!= NULL
);
3257 // If there's nothing to do then just return Destination
3259 if (Source
== NULL
) {
3260 return (*Destination
);
3264 // allow for un-initialized pointers, based on size being 0
3266 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3267 *Destination
= NULL
;
3271 // allow for NULL pointers address as Destination
3273 if (*Destination
!= NULL
) {
3274 ASSERT(CurrentSize
!= 0);
3275 DestinationStartSize
= StrSize(*Destination
);
3276 ASSERT(DestinationStartSize
<= *CurrentSize
);
3278 DestinationStartSize
= 0;
3279 // ASSERT(*CurrentSize == 0);
3283 // Append all of Source?
3286 Count
= StrLen(Source
);
3290 // Test and grow if required
3292 if (CurrentSize
!= NULL
) {
3293 NewSize
= *CurrentSize
;
3294 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3295 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3296 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3298 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3299 *CurrentSize
= NewSize
;
3302 NewSize
= (Count
+1)*sizeof(CHAR16
);
3303 *Destination
= AllocateZeroPool(NewSize
);
3307 // Now use standard StrnCat on a big enough buffer
3309 if (*Destination
== NULL
) {
3313 StrnCatS(*Destination
, NewSize
/sizeof(CHAR16
), Source
, Count
);
3314 return *Destination
;
3318 Prompt the user and return the resultant answer to the requestor.
3320 This function will display the requested question on the shell prompt and then
3321 wait for an appropriate answer to be input from the console.
3323 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3324 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3326 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3329 In either case *Response must be callee freed if Response was not NULL;
3331 @param Type What type of question is asked. This is used to filter the input
3332 to prevent invalid answers to question.
3333 @param Prompt Pointer to string prompt to use to request input.
3334 @param Response Pointer to Response which will be populated upon return.
3336 @retval EFI_SUCCESS The operation was sucessful.
3337 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3338 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3339 @return other The operation failed.
3343 ShellPromptForResponse (
3344 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3345 IN CHAR16
*Prompt OPTIONAL
,
3346 IN OUT VOID
**Response OPTIONAL
3352 SHELL_PROMPT_RESPONSE
*Resp
;
3356 Status
= EFI_UNSUPPORTED
;
3360 if (Type
!= ShellPromptResponseTypeFreeform
) {
3361 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3363 return (EFI_OUT_OF_RESOURCES
);
3368 case ShellPromptResponseTypeQuitContinue
:
3369 if (Prompt
!= NULL
) {
3370 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3373 // wait for valid response
3375 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3376 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3377 if (EFI_ERROR(Status
)) {
3380 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3381 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3382 *Resp
= ShellPromptResponseQuit
;
3384 *Resp
= ShellPromptResponseContinue
;
3387 case ShellPromptResponseTypeYesNoCancel
:
3388 if (Prompt
!= NULL
) {
3389 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3392 // wait for valid response
3394 *Resp
= ShellPromptResponseMax
;
3395 while (*Resp
== ShellPromptResponseMax
) {
3396 if (ShellGetExecutionBreakFlag()) {
3397 Status
= EFI_ABORTED
;
3400 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3401 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3402 if (EFI_ERROR(Status
)) {
3405 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3406 switch (Key
.UnicodeChar
) {
3409 *Resp
= ShellPromptResponseYes
;
3413 *Resp
= ShellPromptResponseNo
;
3417 *Resp
= ShellPromptResponseCancel
;
3422 case ShellPromptResponseTypeYesNoAllCancel
:
3423 if (Prompt
!= NULL
) {
3424 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3427 // wait for valid response
3429 *Resp
= ShellPromptResponseMax
;
3430 while (*Resp
== ShellPromptResponseMax
) {
3431 if (ShellGetExecutionBreakFlag()) {
3432 Status
= EFI_ABORTED
;
3435 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3436 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3437 if (EFI_ERROR(Status
)) {
3441 if (Key
.UnicodeChar
<= 127 && Key
.UnicodeChar
>= 32) {
3442 ShellPrintEx (-1, -1, L
"%c", Key
.UnicodeChar
);
3445 switch (Key
.UnicodeChar
) {
3448 *Resp
= ShellPromptResponseYes
;
3452 *Resp
= ShellPromptResponseNo
;
3456 *Resp
= ShellPromptResponseAll
;
3460 *Resp
= ShellPromptResponseCancel
;
3465 case ShellPromptResponseTypeEnterContinue
:
3466 case ShellPromptResponseTypeAnyKeyContinue
:
3467 if (Prompt
!= NULL
) {
3468 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3471 // wait for valid response
3473 *Resp
= ShellPromptResponseMax
;
3474 while (*Resp
== ShellPromptResponseMax
) {
3475 if (ShellGetExecutionBreakFlag()) {
3476 Status
= EFI_ABORTED
;
3479 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3480 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3481 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3482 if (EFI_ERROR(Status
)) {
3485 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3486 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3487 *Resp
= ShellPromptResponseContinue
;
3491 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3492 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3493 ASSERT_EFI_ERROR(Status
);
3494 *Resp
= ShellPromptResponseContinue
;
3499 case ShellPromptResponseTypeYesNo
:
3500 if (Prompt
!= NULL
) {
3501 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3504 // wait for valid response
3506 *Resp
= ShellPromptResponseMax
;
3507 while (*Resp
== ShellPromptResponseMax
) {
3508 if (ShellGetExecutionBreakFlag()) {
3509 Status
= EFI_ABORTED
;
3512 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3513 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3514 if (EFI_ERROR(Status
)) {
3517 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3518 switch (Key
.UnicodeChar
) {
3521 *Resp
= ShellPromptResponseYes
;
3525 *Resp
= ShellPromptResponseNo
;
3530 case ShellPromptResponseTypeFreeform
:
3531 if (Prompt
!= NULL
) {
3532 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3535 if (ShellGetExecutionBreakFlag()) {
3536 Status
= EFI_ABORTED
;
3539 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3540 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3541 if (EFI_ERROR(Status
)) {
3544 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3545 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3548 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3549 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3553 // This is the location to add new prompt types.
3554 // If your new type loops remember to add ExecutionBreak support.
3560 if (Response
!= NULL
) {
3563 } else if (Buffer
!= NULL
) {
3570 if (Buffer
!= NULL
) {
3575 ShellPrintEx(-1, -1, L
"\r\n");
3580 Prompt the user and return the resultant answer to the requestor.
3582 This function is the same as ShellPromptForResponse, except that the prompt is
3583 automatically pulled from HII.
3585 @param Type What type of question is asked. This is used to filter the input
3586 to prevent invalid answers to question.
3587 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3588 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3589 @param Response Pointer to Response which will be populated upon return.
3591 @retval EFI_SUCCESS the operation was sucessful.
3592 @return other the operation failed.
3594 @sa ShellPromptForResponse
3598 ShellPromptForResponseHii (
3599 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3600 IN CONST EFI_STRING_ID HiiFormatStringId
,
3601 IN CONST EFI_HANDLE HiiFormatHandle
,
3602 IN OUT VOID
**Response
3608 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3609 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3615 Function to determin if an entire string is a valid number.
3617 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3619 @param[in] String The string to evaluate.
3620 @param[in] ForceHex TRUE - always assume hex.
3621 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3622 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3624 @retval TRUE It is all numeric (dec/hex) characters.
3625 @retval FALSE There is a non-numeric character.
3628 InternalShellIsHexOrDecimalNumber (
3629 IN CONST CHAR16
*String
,
3630 IN CONST BOOLEAN ForceHex
,
3631 IN CONST BOOLEAN StopAtSpace
,
3632 IN CONST BOOLEAN TimeNumbers
3636 BOOLEAN LeadingZero
;
3638 if (String
== NULL
) {
3643 // chop off a single negative sign
3645 if (*String
== L
'-') {
3649 if (*String
== CHAR_NULL
) {
3654 // chop leading zeroes
3656 LeadingZero
= FALSE
;
3657 while(*String
== L
'0'){
3662 // allow '0x' or '0X', but not 'x' or 'X'
3664 if (*String
== L
'x' || *String
== L
'X') {
3667 // we got an x without a preceeding 0
3673 } else if (ForceHex
) {
3680 // loop through the remaining characters and use the lib function
3682 for ( ; *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3683 if (TimeNumbers
&& (String
[0] == L
':')) {
3687 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3691 if (!ShellIsDecimalDigitCharacter(*String
)) {
3701 Function to determine if a given filename exists.
3703 @param[in] Name Path to test.
3705 @retval EFI_SUCCESS The Path represents a file.
3706 @retval EFI_NOT_FOUND The Path does not represent a file.
3707 @retval other The path failed to open.
3712 IN CONST CHAR16
*Name
3716 EFI_SHELL_FILE_INFO
*List
;
3718 ASSERT(Name
!= NULL
);
3721 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3722 if (EFI_ERROR(Status
)) {
3726 ShellCloseFileMetaArg(&List
);
3728 return (EFI_SUCCESS
);
3732 Convert a Unicode character to upper case only if
3733 it maps to a valid small-case ASCII character.
3735 This internal function only deal with Unicode character
3736 which maps to a valid small-case ASCII character, i.e.
3737 L'a' to L'z'. For other Unicode character, the input character
3738 is returned directly.
3740 @param Char The character to convert.
3742 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3743 @retval Unchanged Otherwise.
3747 InternalShellCharToUpper (
3751 if (Char
>= L
'a' && Char
<= L
'z') {
3752 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3759 Convert a Unicode character to numerical value.
3761 This internal function only deal with Unicode character
3762 which maps to a valid hexadecimal ASII character, i.e.
3763 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3764 Unicode character, the value returned does not make sense.
3766 @param Char The character to convert.
3768 @return The numerical value converted.
3772 InternalShellHexCharToUintn (
3776 if (ShellIsDecimalDigitCharacter (Char
)) {
3780 return (10 + InternalShellCharToUpper (Char
) - L
'A');
3784 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3786 This function returns a value of type UINT64 by interpreting the contents
3787 of the Unicode string specified by String as a hexadecimal number.
3788 The format of the input Unicode string String is:
3790 [spaces][zeros][x][hexadecimal digits].
3792 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3793 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3794 If "x" appears in the input string, it must be prefixed with at least one 0.
3795 The function will ignore the pad space, which includes spaces or tab characters,
3796 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3797 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3798 first valid hexadecimal digit. Then, the function stops at the first character that is
3799 a not a valid hexadecimal character or NULL, whichever one comes first.
3801 If String has only pad spaces, then zero is returned.
3802 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3803 then zero is returned.
3805 @param[in] String A pointer to a Null-terminated Unicode string.
3806 @param[out] Value Upon a successful return the value of the conversion.
3807 @param[in] StopAtSpace FALSE to skip spaces.
3809 @retval EFI_SUCCESS The conversion was successful.
3810 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3811 @retval EFI_DEVICE_ERROR An overflow occured.
3814 InternalShellStrHexToUint64 (
3815 IN CONST CHAR16
*String
,
3817 IN CONST BOOLEAN StopAtSpace
3822 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3823 return (EFI_INVALID_PARAMETER
);
3827 // Ignore the pad spaces (space or tab)
3829 while ((*String
== L
' ') || (*String
== L
'\t')) {
3834 // Ignore leading Zeros after the spaces
3836 while (*String
== L
'0') {
3840 if (InternalShellCharToUpper (*String
) == L
'X') {
3841 if (*(String
- 1) != L
'0') {
3853 // there is a space where there should't be
3855 if (*String
== L
' ') {
3856 return (EFI_INVALID_PARAMETER
);
3859 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3861 // If the Hex Number represented by String overflows according
3862 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3864 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3865 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3866 return (EFI_DEVICE_ERROR
);
3869 Result
= (LShiftU64(Result
, 4));
3870 Result
+= InternalShellHexCharToUintn (*String
);
3874 // stop at spaces if requested
3876 if (StopAtSpace
&& *String
== L
' ') {
3882 return (EFI_SUCCESS
);
3886 Convert a Null-terminated Unicode decimal string to a value of
3889 This function returns a value of type UINT64 by interpreting the contents
3890 of the Unicode string specified by String as a decimal number. The format
3891 of the input Unicode string String is:
3893 [spaces] [decimal digits].
3895 The valid decimal digit character is in the range [0-9]. The
3896 function will ignore the pad space, which includes spaces or
3897 tab characters, before [decimal digits]. The running zero in the
3898 beginning of [decimal digits] will be ignored. Then, the function
3899 stops at the first character that is a not a valid decimal character
3900 or a Null-terminator, whichever one comes first.
3902 If String has only pad spaces, then 0 is returned.
3903 If String has no pad spaces or valid decimal digits,
3906 @param[in] String A pointer to a Null-terminated Unicode string.
3907 @param[out] Value Upon a successful return the value of the conversion.
3908 @param[in] StopAtSpace FALSE to skip spaces.
3910 @retval EFI_SUCCESS The conversion was successful.
3911 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3912 @retval EFI_DEVICE_ERROR An overflow occured.
3915 InternalShellStrDecimalToUint64 (
3916 IN CONST CHAR16
*String
,
3918 IN CONST BOOLEAN StopAtSpace
3923 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3924 return (EFI_INVALID_PARAMETER
);
3928 // Ignore the pad spaces (space or tab)
3930 while ((*String
== L
' ') || (*String
== L
'\t')) {
3935 // Ignore leading Zeros after the spaces
3937 while (*String
== L
'0') {
3944 // Stop upon space if requested
3945 // (if the whole value was 0)
3947 if (StopAtSpace
&& *String
== L
' ') {
3949 return (EFI_SUCCESS
);
3952 while (ShellIsDecimalDigitCharacter (*String
)) {
3954 // If the number represented by String overflows according
3955 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3958 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3959 return (EFI_DEVICE_ERROR
);
3962 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3966 // Stop at spaces if requested
3968 if (StopAtSpace
&& *String
== L
' ') {
3975 return (EFI_SUCCESS
);
3979 Function to verify and convert a string to its numerical value.
3981 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3983 @param[in] String The string to evaluate.
3984 @param[out] Value Upon a successful return the value of the conversion.
3985 @param[in] ForceHex TRUE - always assume hex.
3986 @param[in] StopAtSpace FALSE to skip spaces.
3988 @retval EFI_SUCCESS The conversion was successful.
3989 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3990 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3994 ShellConvertStringToUint64(
3995 IN CONST CHAR16
*String
,
3997 IN CONST BOOLEAN ForceHex
,
3998 IN CONST BOOLEAN StopAtSpace
4002 CONST CHAR16
*Walker
;
4008 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
4011 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
4012 return (EFI_INVALID_PARAMETER
);
4015 return (EFI_INVALID_PARAMETER
);
4020 // Chop off leading spaces
4022 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
4025 // make sure we have something left that is numeric.
4027 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
4028 return (EFI_INVALID_PARAMETER
);
4032 // do the conversion.
4034 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
4035 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
4037 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
4040 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
4041 return (EFI_NOT_FOUND
);
4044 if (Value
!= NULL
) {
4052 Function to determin if an entire string is a valid number.
4054 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4056 @param[in] String The string to evaluate.
4057 @param[in] ForceHex TRUE - always assume hex.
4058 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4060 @retval TRUE It is all numeric (dec/hex) characters.
4061 @retval FALSE There is a non-numeric character.
4065 ShellIsHexOrDecimalNumber (
4066 IN CONST CHAR16
*String
,
4067 IN CONST BOOLEAN ForceHex
,
4068 IN CONST BOOLEAN StopAtSpace
4071 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4078 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4079 buffer. The returned buffer must be callee freed.
4081 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4082 maintained and not changed for all operations with the same file.
4084 @param[in] Handle SHELL_FILE_HANDLE to read from.
4085 @param[in, out] Ascii Boolean value for indicating whether the file is
4086 Ascii (TRUE) or UCS2 (FALSE).
4088 @return The line of text from the file.
4089 @retval NULL There was not enough memory available.
4091 @sa ShellFileHandleReadLine
4095 ShellFileHandleReturnLine(
4096 IN SHELL_FILE_HANDLE Handle
,
4097 IN OUT BOOLEAN
*Ascii
4107 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4108 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4109 RetVal
= AllocateZeroPool(Size
);
4110 if (RetVal
== NULL
) {
4113 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4116 if (Status
== EFI_END_OF_FILE
&& RetVal
!= NULL
&& *RetVal
!= CHAR_NULL
) {
4117 Status
= EFI_SUCCESS
;
4119 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4127 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4129 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4130 maintained and not changed for all operations with the same file.
4132 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4135 @param[in] Handle SHELL_FILE_HANDLE to read from.
4136 @param[in, out] Buffer The pointer to buffer to read into. If this function
4137 returns EFI_SUCCESS, then on output Buffer will
4138 contain a UCS2 string, even if the file being
4140 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4141 On output, unchanged unless Buffer is too small
4142 to contain the next line of the file. In that
4143 case Size is set to the number of bytes needed
4144 to hold the next line of the file (as a UCS2
4145 string, even if it is an ASCII file).
4146 @param[in] Truncate If the buffer is large enough, this has no effect.
4147 If the buffer is is too small and Truncate is TRUE,
4148 the line will be truncated.
4149 If the buffer is is too small and Truncate is FALSE,
4150 then no read will occur.
4152 @param[in, out] Ascii Boolean value for indicating whether the file is
4153 Ascii (TRUE) or UCS2 (FALSE).
4155 @retval EFI_SUCCESS The operation was successful. The line is stored in
4157 @retval EFI_END_OF_FILE There are no more lines in the file.
4158 @retval EFI_INVALID_PARAMETER Handle was NULL.
4159 @retval EFI_INVALID_PARAMETER Size was NULL.
4160 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4161 Size was updated to the minimum space required.
4165 ShellFileHandleReadLine(
4166 IN SHELL_FILE_HANDLE Handle
,
4167 IN OUT CHAR16
*Buffer
,
4169 IN BOOLEAN Truncate
,
4170 IN OUT BOOLEAN
*Ascii
4177 UINT64 OriginalFilePosition
;
4183 return (EFI_INVALID_PARAMETER
);
4185 if (Buffer
== NULL
) {
4188 *Buffer
= CHAR_NULL
;
4190 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4191 if (OriginalFilePosition
== 0) {
4192 CharSize
= sizeof(CHAR16
);
4193 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4194 ASSERT_EFI_ERROR(Status
);
4195 if (CharBuffer
== gUnicodeFileTag
) {
4199 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4204 CharSize
= sizeof(CHAR8
);
4206 CharSize
= sizeof(CHAR16
);
4208 for (CountSoFar
= 0;;CountSoFar
++){
4210 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4211 if ( EFI_ERROR(Status
)
4213 || (CharBuffer
== L
'\n' && !(*Ascii
))
4214 || (CharBuffer
== '\n' && *Ascii
)
4216 if (CharSize
== 0) {
4217 Status
= EFI_END_OF_FILE
;
4222 // if we have space save it...
4224 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4225 ASSERT(Buffer
!= NULL
);
4226 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4227 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4232 // if we ran out of space tell when...
4234 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4235 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4237 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4239 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4241 return (EFI_BUFFER_TOO_SMALL
);
4243 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4244 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4251 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4253 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4254 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4255 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4256 the help content only.
4257 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4258 @retval EFI_NOT_FOUND The help data could not be found.
4259 @retval EFI_SUCCESS The operation was successful.
4264 IN CONST CHAR16
*CommandToGetHelpOn
,
4265 IN CONST CHAR16
*SectionToGetHelpOn
,
4266 IN BOOLEAN PrintCommandText
4275 // Get the string to print based
4277 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4280 // make sure we got a valid string
4282 if (EFI_ERROR(Status
)){
4285 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4286 return EFI_NOT_FOUND
;
4290 // Chop off trailing stuff we dont need
4292 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4293 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4297 // Print this out to the console
4299 if (PrintCommandText
) {
4300 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4302 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4305 SHELL_FREE_NON_NULL(OutText
);
4311 Function to delete a file by name
4313 @param[in] FileName Pointer to file name to delete.
4315 @retval EFI_SUCCESS the file was deleted sucessfully
4316 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4318 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4319 @retval EFI_NOT_FOUND The specified file could not be found on the
4320 device or the file system could not be found
4322 @retval EFI_NO_MEDIA The device has no medium.
4323 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4324 medium is no longer supported.
4325 @retval EFI_DEVICE_ERROR The device reported an error.
4326 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4327 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4328 @retval EFI_ACCESS_DENIED The file was opened read only.
4329 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4331 @retval other The file failed to open
4335 ShellDeleteFileByName(
4336 IN CONST CHAR16
*FileName
4340 SHELL_FILE_HANDLE FileHandle
;
4342 Status
= ShellFileExists(FileName
);
4344 if (Status
== EFI_SUCCESS
){
4345 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4346 if (Status
== EFI_SUCCESS
){
4347 Status
= ShellDeleteFile(&FileHandle
);
4356 Cleans off all the quotes in the string.
4358 @param[in] OriginalString pointer to the string to be cleaned.
4359 @param[out] CleanString The new string with all quotes removed.
4360 Memory allocated in the function and free
4363 @retval EFI_SUCCESS The operation was successful.
4366 InternalShellStripQuotes (
4367 IN CONST CHAR16
*OriginalString
,
4368 OUT CHAR16
**CleanString
4373 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4374 return EFI_INVALID_PARAMETER
;
4377 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4378 if (*CleanString
== NULL
) {
4379 return EFI_OUT_OF_RESOURCES
;
4382 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4383 if (*Walker
== L
'\"') {
4384 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));