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 - 2016, 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 <ShellBase.h>
19 #include <Library/SortLib.h>
20 #include <Library/BaseLib.h>
22 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
27 SHELL_PARAM_ITEM EmptyParamList
[] = {
30 SHELL_PARAM_ITEM SfoParamList
[] = {
34 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
35 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
36 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
37 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
38 EFI_HANDLE mEfiShellEnvironment2Handle
;
39 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
40 EFI_UNICODE_COLLATION_PROTOCOL
*mUnicodeCollationProtocol
;
43 Check if a Unicode character is a hexadecimal character.
45 This internal function checks if a Unicode character is a
46 numeric character. The valid hexadecimal characters are
47 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
49 @param Char The character to check against.
51 @retval TRUE If the Char is a hexadecmial character.
52 @retval FALSE If the Char is not a hexadecmial character.
57 ShellIsHexaDecimalDigitCharacter (
61 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
65 Check if a Unicode character is a decimal character.
67 This internal function checks if a Unicode character is a
68 decimal character. The valid characters are
72 @param Char The character to check against.
74 @retval TRUE If the Char is a hexadecmial character.
75 @retval FALSE If the Char is not a hexadecmial character.
80 ShellIsDecimalDigitCharacter (
84 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
88 Helper function to find ShellEnvironment2 for constructor.
90 @param[in] ImageHandle A copy of the calling image's handle.
92 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
97 IN EFI_HANDLE ImageHandle
107 Status
= gBS
->OpenProtocol(ImageHandle
,
108 &gEfiShellEnvironment2Guid
,
109 (VOID
**)&mEfiShellEnvironment2
,
112 EFI_OPEN_PROTOCOL_GET_PROTOCOL
115 // look for the mEfiShellEnvironment2 protocol at a higher level
117 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
119 // figure out how big of a buffer we need.
121 Status
= gBS
->LocateHandle (ByProtocol
,
122 &gEfiShellEnvironment2Guid
,
123 NULL
, // ignored for ByProtocol
128 // maybe it's not there???
130 if (Status
== EFI_BUFFER_TOO_SMALL
) {
131 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
132 if (Buffer
== NULL
) {
133 return (EFI_OUT_OF_RESOURCES
);
135 Status
= gBS
->LocateHandle (ByProtocol
,
136 &gEfiShellEnvironment2Guid
,
137 NULL
, // ignored for ByProtocol
142 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
144 // now parse the list of returned handles
146 Status
= EFI_NOT_FOUND
;
147 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
148 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
149 &gEfiShellEnvironment2Guid
,
150 (VOID
**)&mEfiShellEnvironment2
,
153 EFI_OPEN_PROTOCOL_GET_PROTOCOL
155 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
156 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
157 Status
= EFI_SUCCESS
;
163 if (Buffer
!= NULL
) {
170 Function to do most of the work of the constructor. Allows for calling
171 multiple times without complete re-initialization.
173 @param[in] ImageHandle A copy of the ImageHandle.
174 @param[in] SystemTable A pointer to the SystemTable for the application.
176 @retval EFI_SUCCESS The operationw as successful.
180 ShellLibConstructorWorker (
181 IN EFI_HANDLE ImageHandle
,
182 IN EFI_SYSTEM_TABLE
*SystemTable
188 // UEFI 2.0 shell interfaces (used preferentially)
190 Status
= gBS
->OpenProtocol(
192 &gEfiShellProtocolGuid
,
193 (VOID
**)&gEfiShellProtocol
,
196 EFI_OPEN_PROTOCOL_GET_PROTOCOL
198 if (EFI_ERROR(Status
)) {
200 // Search for the shell protocol
202 Status
= gBS
->LocateProtocol(
203 &gEfiShellProtocolGuid
,
205 (VOID
**)&gEfiShellProtocol
207 if (EFI_ERROR(Status
)) {
208 gEfiShellProtocol
= NULL
;
211 Status
= gBS
->OpenProtocol(
213 &gEfiShellParametersProtocolGuid
,
214 (VOID
**)&gEfiShellParametersProtocol
,
217 EFI_OPEN_PROTOCOL_GET_PROTOCOL
219 if (EFI_ERROR(Status
)) {
220 gEfiShellParametersProtocol
= NULL
;
223 if (gEfiShellParametersProtocol
== NULL
|| 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 // only success getting 2 of either the old or new, but no 1/2 and 1/2
248 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
249 (gEfiShellProtocol
!= NULL
&& gEfiShellParametersProtocol
!= NULL
) ) {
250 if (gEfiShellProtocol
!= NULL
) {
251 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
252 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
253 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
254 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
255 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
256 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
257 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
258 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
259 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
260 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
262 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
263 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
264 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
265 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
266 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
267 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
268 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
269 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
270 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
271 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
273 return (EFI_SUCCESS
);
275 return (EFI_NOT_FOUND
);
278 Constructor for the Shell library.
280 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
282 @param ImageHandle the image handle of the process
283 @param SystemTable the EFI System Table pointer
285 @retval EFI_SUCCESS the initialization was complete sucessfully
286 @return others an error ocurred during initialization
290 ShellLibConstructor (
291 IN EFI_HANDLE ImageHandle
,
292 IN EFI_SYSTEM_TABLE
*SystemTable
295 mEfiShellEnvironment2
= NULL
;
296 gEfiShellProtocol
= NULL
;
297 gEfiShellParametersProtocol
= NULL
;
298 mEfiShellInterface
= NULL
;
299 mEfiShellEnvironment2Handle
= NULL
;
300 mUnicodeCollationProtocol
= NULL
;
303 // verify that auto initialize is not set false
305 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
306 return (EFI_SUCCESS
);
309 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
313 Destructor for the library. free any resources.
315 @param[in] ImageHandle A copy of the ImageHandle.
316 @param[in] SystemTable A pointer to the SystemTable for the application.
318 @retval EFI_SUCCESS The operation was successful.
319 @return An error from the CloseProtocol function.
324 IN EFI_HANDLE ImageHandle
,
325 IN EFI_SYSTEM_TABLE
*SystemTable
328 if (mEfiShellEnvironment2
!= NULL
) {
329 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
330 &gEfiShellEnvironment2Guid
,
333 mEfiShellEnvironment2
= NULL
;
335 if (mEfiShellInterface
!= NULL
) {
336 gBS
->CloseProtocol(ImageHandle
,
337 &gEfiShellInterfaceGuid
,
340 mEfiShellInterface
= NULL
;
342 if (gEfiShellProtocol
!= NULL
) {
343 gBS
->CloseProtocol(ImageHandle
,
344 &gEfiShellProtocolGuid
,
347 gEfiShellProtocol
= NULL
;
349 if (gEfiShellParametersProtocol
!= NULL
) {
350 gBS
->CloseProtocol(ImageHandle
,
351 &gEfiShellParametersProtocolGuid
,
354 gEfiShellParametersProtocol
= NULL
;
356 mEfiShellEnvironment2Handle
= NULL
;
358 return (EFI_SUCCESS
);
362 This function causes the shell library to initialize itself. If the shell library
363 is already initialized it will de-initialize all the current protocol poitners and
364 re-populate them again.
366 When the library is used with PcdShellLibAutoInitialize set to true this function
367 will return EFI_SUCCESS and perform no actions.
369 This function is intended for internal access for shell commands only.
371 @retval EFI_SUCCESS the initialization was complete sucessfully
382 // if auto initialize is not false then skip
384 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
385 return (EFI_SUCCESS
);
389 // deinit the current stuff
391 Status
= ShellLibDestructor (gImageHandle
, gST
);
392 ASSERT_EFI_ERROR (Status
);
395 // init the new stuff
397 return (ShellLibConstructorWorker(gImageHandle
, gST
));
401 This function will retrieve the information about the file for the handle
402 specified and store it in allocated pool memory.
404 This function allocates a buffer to store the file's information. It is the
405 caller's responsibility to free the buffer
407 @param FileHandle The file handle of the file for which information is
410 @retval NULL information could not be retrieved.
412 @return the information about the file
417 IN SHELL_FILE_HANDLE FileHandle
420 return (FileFunctionMap
.GetFileInfo(FileHandle
));
424 This function sets the information about the file for the opened handle
427 @param[in] FileHandle The file handle of the file for which information
430 @param[in] FileInfo The information to set.
432 @retval EFI_SUCCESS The information was set.
433 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
434 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
435 @retval EFI_NO_MEDIA The device has no medium.
436 @retval EFI_DEVICE_ERROR The device reported an error.
437 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
438 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
439 @retval EFI_ACCESS_DENIED The file was opened read only.
440 @retval EFI_VOLUME_FULL The volume is full.
445 IN SHELL_FILE_HANDLE FileHandle
,
446 IN EFI_FILE_INFO
*FileInfo
449 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
453 This function will open a file or directory referenced by DevicePath.
455 This function opens a file with the open mode according to the file path. The
456 Attributes is valid only for EFI_FILE_MODE_CREATE.
458 @param FilePath on input the device path to the file. On output
459 the remaining device path.
460 @param DeviceHandle pointer to the system device handle.
461 @param FileHandle pointer to the file handle.
462 @param OpenMode the mode to open the file with.
463 @param Attributes the file's file attributes.
465 @retval EFI_SUCCESS The information was set.
466 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
467 @retval EFI_UNSUPPORTED Could not open the file path.
468 @retval EFI_NOT_FOUND The specified file could not be found on the
469 device or the file system could not be found on
471 @retval EFI_NO_MEDIA The device has no medium.
472 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
473 medium is no longer supported.
474 @retval EFI_DEVICE_ERROR The device reported an error.
475 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
476 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
477 @retval EFI_ACCESS_DENIED The file was opened read only.
478 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
480 @retval EFI_VOLUME_FULL The volume is full.
484 ShellOpenFileByDevicePath(
485 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
486 OUT EFI_HANDLE
*DeviceHandle
,
487 OUT SHELL_FILE_HANDLE
*FileHandle
,
494 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
495 EFI_FILE_PROTOCOL
*Handle1
;
496 EFI_FILE_PROTOCOL
*Handle2
;
497 CHAR16
*FnafPathName
;
500 if (FilePath
== NULL
|| FileHandle
== NULL
|| DeviceHandle
== NULL
) {
501 return (EFI_INVALID_PARAMETER
);
505 // which shell interface should we use
507 if (gEfiShellProtocol
!= NULL
) {
509 // use UEFI Shell 2.0 method.
511 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
512 if (FileName
== NULL
) {
513 return (EFI_INVALID_PARAMETER
);
515 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
522 // use old shell method.
524 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
527 if (EFI_ERROR (Status
)) {
530 Status
= gBS
->OpenProtocol(*DeviceHandle
,
531 &gEfiSimpleFileSystemProtocolGuid
,
532 (VOID
**)&EfiSimpleFileSystemProtocol
,
535 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
536 if (EFI_ERROR (Status
)) {
539 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
540 if (EFI_ERROR (Status
)) {
546 // go down directories one node at a time.
548 while (!IsDevicePathEnd (*FilePath
)) {
550 // For file system access each node should be a file path component
552 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
553 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
556 return (EFI_INVALID_PARAMETER
);
559 // Open this file path node
565 // File Name Alignment Fix (FNAF)
566 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
567 // The structure pointed to by FilePath may be not CHAR16 aligned.
568 // This code copies the potentially unaligned PathName data from the
569 // FilePath structure to the aligned FnafPathName for use in the
570 // calls to Handl2->Open.
574 // Determine length of PathName, in bytes.
576 PathLen
= DevicePathNodeLength (*FilePath
) - SIZE_OF_FILEPATH_DEVICE_PATH
;
579 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
580 // Copy bytes from possibly unaligned location to aligned location
582 FnafPathName
= AllocateCopyPool(PathLen
, (UINT8
*)((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
);
583 if (FnafPathName
== NULL
) {
584 return EFI_OUT_OF_RESOURCES
;
588 // Try to test opening an existing file
590 Status
= Handle2
->Open (
594 OpenMode
&~EFI_FILE_MODE_CREATE
,
599 // see if the error was that it needs to be created
601 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
602 Status
= Handle2
->Open (
612 // Free the alignment buffer
614 FreePool(FnafPathName
);
617 // Close the last node
619 Handle2
->Close (Handle2
);
621 if (EFI_ERROR(Status
)) {
628 *FilePath
= NextDevicePathNode (*FilePath
);
632 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
634 *FileHandle
= (VOID
*)Handle1
;
635 return (EFI_SUCCESS
);
639 This function will open a file or directory referenced by filename.
641 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
642 otherwise, the Filehandle is NULL. The Attributes is valid only for
643 EFI_FILE_MODE_CREATE.
645 if FileName is NULL then ASSERT()
647 @param FileName pointer to file name
648 @param FileHandle pointer to the file handle.
649 @param OpenMode the mode to open the file with.
650 @param Attributes the file's file attributes.
652 @retval EFI_SUCCESS The information was set.
653 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
654 @retval EFI_UNSUPPORTED Could not open the file path.
655 @retval EFI_NOT_FOUND The specified file could not be found on the
656 device or the file system could not be found
658 @retval EFI_NO_MEDIA The device has no medium.
659 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
660 medium is no longer supported.
661 @retval EFI_DEVICE_ERROR The device reported an error.
662 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
663 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
664 @retval EFI_ACCESS_DENIED The file was opened read only.
665 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
667 @retval EFI_VOLUME_FULL The volume is full.
672 IN CONST CHAR16
*FileName
,
673 OUT SHELL_FILE_HANDLE
*FileHandle
,
678 EFI_HANDLE DeviceHandle
;
679 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
681 EFI_FILE_INFO
*FileInfo
;
682 CHAR16
*FileNameCopy
;
686 // ASSERT if FileName is NULL
688 ASSERT(FileName
!= NULL
);
690 if (FileName
== NULL
) {
691 return (EFI_INVALID_PARAMETER
);
694 if (gEfiShellProtocol
!= NULL
) {
695 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
698 // Create only a directory
700 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
701 return ShellCreateDirectory(FileName
, FileHandle
);
705 // Create the directory to create the file in
707 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
708 if (FileName
== NULL
) {
709 return (EFI_OUT_OF_RESOURCES
);
711 PathCleanUpDirectories (FileNameCopy
);
712 if (PathRemoveLastItem (FileNameCopy
)) {
713 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy
, FileHandle
))) {
714 ShellCloseFile (FileHandle
);
717 SHELL_FREE_NON_NULL (FileNameCopy
);
721 // Use UEFI Shell 2.0 method to create the file
723 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
726 if (EFI_ERROR(Status
)) {
730 if (mUnicodeCollationProtocol
== NULL
) {
731 Status
= gBS
->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&mUnicodeCollationProtocol
);
732 if (EFI_ERROR (Status
)) {
733 gEfiShellProtocol
->CloseFile (*FileHandle
);
738 if ((mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NUL") != 0) &&
739 (mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NULL") != 0) &&
740 !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
741 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
742 ASSERT(FileInfo
!= NULL
);
743 FileInfo
->Attribute
= Attributes
;
744 Status2
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
746 if (EFI_ERROR (Status2
)) {
747 gEfiShellProtocol
->CloseFile(*FileHandle
);
754 // Using EFI Shell version
755 // this means convert name to path and call that function
756 // since this will use EFI method again that will open it.
758 ASSERT(mEfiShellEnvironment2
!= NULL
);
759 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
760 if (FilePath
!= NULL
) {
761 return (ShellOpenFileByDevicePath(&FilePath
,
767 return (EFI_DEVICE_ERROR
);
770 This function create a directory
772 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
773 otherwise, the Filehandle is NULL. If the directory already existed, this
774 function opens the existing directory.
776 @param DirectoryName pointer to directory name
777 @param FileHandle pointer to the file handle.
779 @retval EFI_SUCCESS The information was set.
780 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
781 @retval EFI_UNSUPPORTED Could not open the file path.
782 @retval EFI_NOT_FOUND The specified file could not be found on the
783 device or the file system could not be found
785 @retval EFI_NO_MEDIA The device has no medium.
786 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
787 medium is no longer supported.
788 @retval EFI_DEVICE_ERROR The device reported an error.
789 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
790 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
791 @retval EFI_ACCESS_DENIED The file was opened read only.
792 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
794 @retval EFI_VOLUME_FULL The volume is full.
795 @sa ShellOpenFileByName
799 ShellCreateDirectory(
800 IN CONST CHAR16
*DirectoryName
,
801 OUT SHELL_FILE_HANDLE
*FileHandle
804 if (gEfiShellProtocol
!= NULL
) {
806 // Use UEFI Shell 2.0 method
808 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
813 return (ShellOpenFileByName(DirectoryName
,
815 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
822 This function reads information from an opened file.
824 If FileHandle is not a directory, the function reads the requested number of
825 bytes from the file at the file's current position and returns them in Buffer.
826 If the read goes beyond the end of the file, the read length is truncated to the
827 end of the file. The file's current position is increased by the number of bytes
828 returned. If FileHandle is a directory, the function reads the directory entry
829 at the file's current position and returns the entry in Buffer. If the Buffer
830 is not large enough to hold the current directory entry, then
831 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
832 BufferSize is set to be the size of the buffer needed to read the entry. On
833 success, the current position is updated to the next directory entry. If there
834 are no more directory entries, the read returns a zero-length buffer.
835 EFI_FILE_INFO is the structure returned as the directory entry.
837 @param FileHandle the opened file handle
838 @param BufferSize on input the size of buffer in bytes. on return
839 the number of bytes written.
840 @param Buffer the buffer to put read data into.
842 @retval EFI_SUCCESS Data was read.
843 @retval EFI_NO_MEDIA The device has no media.
844 @retval EFI_DEVICE_ERROR The device reported an error.
845 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
846 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
853 IN SHELL_FILE_HANDLE FileHandle
,
854 IN OUT UINTN
*BufferSize
,
858 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
863 Write data to a file.
865 This function writes the specified number of bytes to the file at the current
866 file position. The current file position is advanced the actual number of bytes
867 written, which is returned in BufferSize. Partial writes only occur when there
868 has been a data error during the write attempt (such as "volume space full").
869 The file is automatically grown to hold the data if required. Direct writes to
870 opened directories are not supported.
872 @param FileHandle The opened file for writing
873 @param BufferSize on input the number of bytes in Buffer. On output
874 the number of bytes written.
875 @param Buffer the buffer containing data to write is stored.
877 @retval EFI_SUCCESS Data was written.
878 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
879 @retval EFI_NO_MEDIA The device has no media.
880 @retval EFI_DEVICE_ERROR The device reported an error.
881 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
882 @retval EFI_WRITE_PROTECTED The device is write-protected.
883 @retval EFI_ACCESS_DENIED The file was open for read only.
884 @retval EFI_VOLUME_FULL The volume is full.
889 IN SHELL_FILE_HANDLE FileHandle
,
890 IN OUT UINTN
*BufferSize
,
894 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
898 Close an open file handle.
900 This function closes a specified file handle. All "dirty" cached file data is
901 flushed to the device, and the file is closed. In all cases the handle is
904 @param FileHandle the file handle to close.
906 @retval EFI_SUCCESS the file handle was closed sucessfully.
911 IN SHELL_FILE_HANDLE
*FileHandle
914 return (FileFunctionMap
.CloseFile(*FileHandle
));
918 Delete a file and close the handle
920 This function closes and deletes a file. In all cases the file handle is closed.
921 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
922 returned, but the handle is still closed.
924 @param FileHandle the file handle to delete
926 @retval EFI_SUCCESS the file was closed sucessfully
927 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
929 @retval INVALID_PARAMETER One of the parameters has an invalid value.
934 IN SHELL_FILE_HANDLE
*FileHandle
937 return (FileFunctionMap
.DeleteFile(*FileHandle
));
941 Set the current position in a file.
943 This function sets the current file position for the handle to the position
944 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
945 absolute positioning is supported, and seeking past the end of the file is
946 allowed (a subsequent write would grow the file). Seeking to position
947 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
948 If FileHandle is a directory, the only position that may be set is zero. This
949 has the effect of starting the read process of the directory entries over.
951 @param FileHandle The file handle on which the position is being set
952 @param Position Byte position from begining of file
954 @retval EFI_SUCCESS Operation completed sucessfully.
955 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
957 @retval INVALID_PARAMETER One of the parameters has an invalid value.
961 ShellSetFilePosition (
962 IN SHELL_FILE_HANDLE FileHandle
,
966 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
970 Gets a file's current position
972 This function retrieves the current file position for the file handle. For
973 directories, the current file position has no meaning outside of the file
974 system driver and as such the operation is not supported. An error is returned
975 if FileHandle is a directory.
977 @param FileHandle The open file handle on which to get the position.
978 @param Position Byte position from begining of file.
980 @retval EFI_SUCCESS the operation completed sucessfully.
981 @retval INVALID_PARAMETER One of the parameters has an invalid value.
982 @retval EFI_UNSUPPORTED the request is not valid on directories.
986 ShellGetFilePosition (
987 IN SHELL_FILE_HANDLE FileHandle
,
991 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
994 Flushes data on a file
996 This function flushes all modified data associated with a file to a device.
998 @param FileHandle The file handle on which to flush data
1000 @retval EFI_SUCCESS The data was flushed.
1001 @retval EFI_NO_MEDIA The device has no media.
1002 @retval EFI_DEVICE_ERROR The device reported an error.
1003 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1004 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1005 @retval EFI_ACCESS_DENIED The file was opened for read only.
1010 IN SHELL_FILE_HANDLE FileHandle
1013 return (FileFunctionMap
.FlushFile(FileHandle
));
1016 /** Retrieve first entry from a directory.
1018 This function takes an open directory handle and gets information from the
1019 first entry in the directory. A buffer is allocated to contain
1020 the information and a pointer to the buffer is returned in *Buffer. The
1021 caller can use ShellFindNextFile() to get subsequent directory entries.
1023 The buffer will be freed by ShellFindNextFile() when the last directory
1024 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1025 when finished with it.
1027 @param[in] DirHandle The file handle of the directory to search.
1028 @param[out] Buffer The pointer to the buffer for the file's information.
1030 @retval EFI_SUCCESS Found the first file.
1031 @retval EFI_NOT_FOUND Cannot find the directory.
1032 @retval EFI_NO_MEDIA The device has no media.
1033 @retval EFI_DEVICE_ERROR The device reported an error.
1034 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1035 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1040 ShellFindFirstFile (
1041 IN SHELL_FILE_HANDLE DirHandle
,
1042 OUT EFI_FILE_INFO
**Buffer
1046 // pass to file handle lib
1048 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1050 /** Retrieve next entries from a directory.
1052 To use this function, the caller must first call the ShellFindFirstFile()
1053 function to get the first directory entry. Subsequent directory entries are
1054 retrieved by using the ShellFindNextFile() function. This function can
1055 be called several times to get each entry from the directory. If the call of
1056 ShellFindNextFile() retrieved the last directory entry, the next call of
1057 this function will set *NoFile to TRUE and free the buffer.
1059 @param[in] DirHandle The file handle of the directory.
1060 @param[out] Buffer The pointer to buffer for file's information.
1061 @param[out] NoFile The pointer to boolean when last file is found.
1063 @retval EFI_SUCCESS Found the next file, or reached last file
1064 @retval EFI_NO_MEDIA The device has no media.
1065 @retval EFI_DEVICE_ERROR The device reported an error.
1066 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1071 IN SHELL_FILE_HANDLE DirHandle
,
1072 OUT EFI_FILE_INFO
*Buffer
,
1077 // pass to file handle lib
1079 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1082 Retrieve the size of a file.
1084 if FileHandle is NULL then ASSERT()
1085 if Size is NULL then ASSERT()
1087 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1090 @param FileHandle file handle from which size is retrieved
1091 @param Size pointer to size
1093 @retval EFI_SUCCESS operation was completed sucessfully
1094 @retval EFI_DEVICE_ERROR cannot access the file
1099 IN SHELL_FILE_HANDLE FileHandle
,
1103 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1106 Retrieves the status of the break execution flag
1108 this function is useful to check whether the application is being asked to halt by the shell.
1110 @retval TRUE the execution break is enabled
1111 @retval FALSE the execution break is not enabled
1115 ShellGetExecutionBreakFlag(
1120 // Check for UEFI Shell 2.0 protocols
1122 if (gEfiShellProtocol
!= NULL
) {
1125 // We are using UEFI Shell 2.0; see if the event has been triggered
1127 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1134 // using EFI Shell; call the function to check
1136 if (mEfiShellEnvironment2
!= NULL
) {
1137 return (mEfiShellEnvironment2
->GetExecutionBreak());
1143 return the value of an environment variable
1145 this function gets the value of the environment variable set by the
1146 ShellSetEnvironmentVariable function
1148 @param EnvKey The key name of the environment variable.
1150 @retval NULL the named environment variable does not exist.
1151 @return != NULL pointer to the value of the environment variable
1155 ShellGetEnvironmentVariable (
1156 IN CONST CHAR16
*EnvKey
1160 // Check for UEFI Shell 2.0 protocols
1162 if (gEfiShellProtocol
!= NULL
) {
1163 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1167 // Check for EFI shell
1169 if (mEfiShellEnvironment2
!= NULL
) {
1170 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1176 set the value of an environment variable
1178 This function changes the current value of the specified environment variable. If the
1179 environment variable exists and the Value is an empty string, then the environment
1180 variable is deleted. If the environment variable exists and the Value is not an empty
1181 string, then the value of the environment variable is changed. If the environment
1182 variable does not exist and the Value is an empty string, there is no action. If the
1183 environment variable does not exist and the Value is a non-empty string, then the
1184 environment variable is created and assigned the specified value.
1186 This is not supported pre-UEFI Shell 2.0.
1188 @param EnvKey The key name of the environment variable.
1189 @param EnvVal The Value of the environment variable
1190 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1192 @retval EFI_SUCCESS the operation was completed sucessfully
1193 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1197 ShellSetEnvironmentVariable (
1198 IN CONST CHAR16
*EnvKey
,
1199 IN CONST CHAR16
*EnvVal
,
1204 // Check for UEFI Shell 2.0 protocols
1206 if (gEfiShellProtocol
!= NULL
) {
1207 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1211 // This feature does not exist under EFI shell
1213 return (EFI_UNSUPPORTED
);
1217 Cause the shell to parse and execute a command line.
1219 This function creates a nested instance of the shell and executes the specified
1220 command (CommandLine) with the specified environment (Environment). Upon return,
1221 the status code returned by the specified command is placed in StatusCode.
1222 If Environment is NULL, then the current environment is used and all changes made
1223 by the commands executed will be reflected in the current environment. If the
1224 Environment is non-NULL, then the changes made will be discarded.
1225 The CommandLine is executed from the current working directory on the current
1228 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1229 environment. The values pointed to by the parameters will be unchanged by the
1230 ShellExecute() function. The Output parameter has no effect in a
1231 UEFI Shell 2.0 environment.
1233 @param[in] ParentHandle The parent image starting the operation.
1234 @param[in] CommandLine The pointer to a NULL terminated command line.
1235 @param[in] Output True to display debug output. False to hide it.
1236 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1237 in the form "x=y". If NULL, the current set is used.
1238 @param[out] Status The status of the run command line.
1240 @retval EFI_SUCCESS The operation completed sucessfully. Status
1241 contains the status code returned.
1242 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1243 @retval EFI_OUT_OF_RESOURCES Out of resources.
1244 @retval EFI_UNSUPPORTED The operation is not allowed.
1249 IN EFI_HANDLE
*ParentHandle
,
1250 IN CHAR16
*CommandLine OPTIONAL
,
1251 IN BOOLEAN Output OPTIONAL
,
1252 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1253 OUT EFI_STATUS
*Status OPTIONAL
1256 EFI_STATUS CmdStatus
;
1258 // Check for UEFI Shell 2.0 protocols
1260 if (gEfiShellProtocol
!= NULL
) {
1262 // Call UEFI Shell 2.0 version (not using Output parameter)
1264 return (gEfiShellProtocol
->Execute(ParentHandle
,
1266 EnvironmentVariables
,
1271 // Check for EFI shell
1273 if (mEfiShellEnvironment2
!= NULL
) {
1275 // Call EFI Shell version.
1276 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1278 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1282 // No Status output parameter so just use the returned status
1284 if (Status
!= NULL
) {
1285 *Status
= CmdStatus
;
1288 // If there was an error, we can't tell if it was from the command or from
1289 // the Execute() function, so we'll just assume the shell ran successfully
1290 // and the error came from the command.
1295 return (EFI_UNSUPPORTED
);
1299 Retreives the current directory path
1301 If the DeviceName is NULL, it returns the current device's current directory
1302 name. If the DeviceName is not NULL, it returns the current directory name
1305 Note that the current directory string should exclude the tailing backslash character.
1307 @param DeviceName the name of the drive to get directory on
1309 @retval NULL the directory does not exist
1310 @return != NULL the directory
1314 ShellGetCurrentDir (
1315 IN CHAR16
* CONST DeviceName OPTIONAL
1319 // Check for UEFI Shell 2.0 protocols
1321 if (gEfiShellProtocol
!= NULL
) {
1322 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1326 // Check for EFI shell
1328 if (mEfiShellEnvironment2
!= NULL
) {
1329 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1335 sets (enabled or disabled) the page break mode
1337 when page break mode is enabled the screen will stop scrolling
1338 and wait for operator input before scrolling a subsequent screen.
1340 @param CurrentState TRUE to enable and FALSE to disable
1344 ShellSetPageBreakMode (
1345 IN BOOLEAN CurrentState
1349 // check for enabling
1351 if (CurrentState
!= 0x00) {
1353 // check for UEFI Shell 2.0
1355 if (gEfiShellProtocol
!= NULL
) {
1357 // Enable with UEFI 2.0 Shell
1359 gEfiShellProtocol
->EnablePageBreak();
1363 // Check for EFI shell
1365 if (mEfiShellEnvironment2
!= NULL
) {
1367 // Enable with EFI Shell
1369 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1375 // check for UEFI Shell 2.0
1377 if (gEfiShellProtocol
!= NULL
) {
1379 // Disable with UEFI 2.0 Shell
1381 gEfiShellProtocol
->DisablePageBreak();
1385 // Check for EFI shell
1387 if (mEfiShellEnvironment2
!= NULL
) {
1389 // Disable with EFI Shell
1391 mEfiShellEnvironment2
->DisablePageBreak ();
1399 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1400 /// This allows for the struct to be populated.
1407 SHELL_FILE_HANDLE Handle
;
1408 EFI_FILE_INFO
*Info
;
1409 } EFI_SHELL_FILE_INFO_NO_CONST
;
1412 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1414 if OldStyleFileList is NULL then ASSERT()
1416 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1417 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1418 the ShellCloseFileMetaArg function.
1420 @param[in] FileList the EFI shell list type
1421 @param[in, out] ListHead the list to add to
1423 @retval the resultant head of the double linked new format list;
1427 InternalShellConvertFileListType (
1428 IN LIST_ENTRY
*FileList
,
1429 IN OUT LIST_ENTRY
*ListHead
1432 SHELL_FILE_ARG
*OldInfo
;
1434 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1439 ASSERT(FileList
!= NULL
);
1440 ASSERT(ListHead
!= NULL
);
1443 // enumerate through each member of the old list and copy
1445 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1446 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1447 ASSERT(OldInfo
!= NULL
);
1450 // Skip ones that failed to open...
1452 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1457 // make sure the old list was valid
1459 ASSERT(OldInfo
->Info
!= NULL
);
1460 ASSERT(OldInfo
->FullName
!= NULL
);
1461 ASSERT(OldInfo
->FileName
!= NULL
);
1464 // allocate a new EFI_SHELL_FILE_INFO object
1466 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1467 if (NewInfo
== NULL
) {
1468 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1474 // copy the simple items
1476 NewInfo
->Handle
= OldInfo
->Handle
;
1477 NewInfo
->Status
= OldInfo
->Status
;
1479 // old shell checks for 0 not NULL
1480 OldInfo
->Handle
= 0;
1483 // allocate new space to copy strings and structure
1485 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1486 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1487 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1490 // make sure all the memory allocations were sucessful
1492 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1494 // Free the partially allocated new node
1496 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1497 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1498 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1499 SHELL_FREE_NON_NULL(NewInfo
);
1502 // Free the previously converted stuff
1504 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1510 // add that to the list
1512 InsertTailList(ListHead
, &NewInfo
->Link
);
1517 Opens a group of files based on a path.
1519 This function uses the Arg to open all the matching files. Each matched
1520 file has a SHELL_FILE_INFO structure to record the file information. These
1521 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1522 structures from ListHead to access each file. This function supports wildcards
1523 and will process '?' and '*' as such. the list must be freed with a call to
1524 ShellCloseFileMetaArg().
1526 If you are NOT appending to an existing list *ListHead must be NULL. If
1527 *ListHead is NULL then it must be callee freed.
1529 @param Arg pointer to path string
1530 @param OpenMode mode to open files with
1531 @param ListHead head of linked list of results
1533 @retval EFI_SUCCESS the operation was sucessful and the list head
1534 contains the list of opened files
1535 @return != EFI_SUCCESS the operation failed
1537 @sa InternalShellConvertFileListType
1541 ShellOpenFileMetaArg (
1544 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1548 LIST_ENTRY mOldStyleFileList
;
1549 CHAR16
*CleanFilePathStr
;
1552 // ASSERT that Arg and ListHead are not NULL
1554 ASSERT(Arg
!= NULL
);
1555 ASSERT(ListHead
!= NULL
);
1557 CleanFilePathStr
= NULL
;
1559 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1560 if (EFI_ERROR (Status
)) {
1565 // Check for UEFI Shell 2.0 protocols
1567 if (gEfiShellProtocol
!= NULL
) {
1568 if (*ListHead
== NULL
) {
1569 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1570 if (*ListHead
== NULL
) {
1571 FreePool(CleanFilePathStr
);
1572 return (EFI_OUT_OF_RESOURCES
);
1574 InitializeListHead(&((*ListHead
)->Link
));
1576 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1579 if (EFI_ERROR(Status
)) {
1580 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1582 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1584 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1585 FreePool(*ListHead
);
1586 FreePool(CleanFilePathStr
);
1588 return (EFI_NOT_FOUND
);
1590 FreePool(CleanFilePathStr
);
1595 // Check for EFI shell
1597 if (mEfiShellEnvironment2
!= NULL
) {
1599 // make sure the list head is initialized
1601 InitializeListHead(&mOldStyleFileList
);
1604 // Get the EFI Shell list of files
1606 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1607 if (EFI_ERROR(Status
)) {
1609 FreePool(CleanFilePathStr
);
1613 if (*ListHead
== NULL
) {
1614 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1615 if (*ListHead
== NULL
) {
1616 FreePool(CleanFilePathStr
);
1617 return (EFI_OUT_OF_RESOURCES
);
1619 InitializeListHead(&((*ListHead
)->Link
));
1623 // Convert that to equivalent of UEFI Shell 2.0 structure
1625 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1628 // Free the EFI Shell version that was converted.
1630 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1632 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1633 FreePool(*ListHead
);
1635 Status
= EFI_NOT_FOUND
;
1637 FreePool(CleanFilePathStr
);
1641 FreePool(CleanFilePathStr
);
1642 return (EFI_UNSUPPORTED
);
1645 Free the linked list returned from ShellOpenFileMetaArg.
1647 if ListHead is NULL then ASSERT().
1649 @param ListHead the pointer to free.
1651 @retval EFI_SUCCESS the operation was sucessful.
1655 ShellCloseFileMetaArg (
1656 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1662 // ASSERT that ListHead is not NULL
1664 ASSERT(ListHead
!= NULL
);
1667 // Check for UEFI Shell 2.0 protocols
1669 if (gEfiShellProtocol
!= NULL
) {
1670 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1671 } else if (mEfiShellEnvironment2
!= NULL
) {
1673 // Since this is EFI Shell version we need to free our internally made copy
1676 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1677 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1678 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1679 RemoveEntryList(Node
);
1680 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1681 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1682 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1683 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1684 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1686 SHELL_FREE_NON_NULL(*ListHead
);
1690 return (EFI_UNSUPPORTED
);
1694 Find a file by searching the CWD and then the path.
1696 If FileName is NULL then ASSERT.
1698 If the return value is not NULL then the memory must be caller freed.
1700 @param FileName Filename string.
1702 @retval NULL the file was not found
1703 @return !NULL the full path to the file.
1708 IN CONST CHAR16
*FileName
1712 SHELL_FILE_HANDLE Handle
;
1716 CONST CHAR16
*Walker
;
1723 // First make sure its not an absolute path.
1725 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1726 if (!EFI_ERROR(Status
)){
1727 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1728 ASSERT(RetVal
== NULL
);
1729 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1730 ShellCloseFile(&Handle
);
1733 ShellCloseFile(&Handle
);
1737 Path
= ShellGetEnvironmentVariable(L
"cwd");
1739 Size
= StrSize(Path
) + sizeof(CHAR16
);
1740 Size
+= StrSize(FileName
);
1741 TestPath
= AllocateZeroPool(Size
);
1742 if (TestPath
== NULL
) {
1745 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), Path
);
1746 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1747 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1748 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1749 if (!EFI_ERROR(Status
)){
1750 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1751 ASSERT(RetVal
== NULL
);
1752 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1753 ShellCloseFile(&Handle
);
1757 ShellCloseFile(&Handle
);
1762 Path
= ShellGetEnvironmentVariable(L
"path");
1764 Size
= StrSize(Path
)+sizeof(CHAR16
);
1765 Size
+= StrSize(FileName
);
1766 TestPath
= AllocateZeroPool(Size
);
1767 if (TestPath
== NULL
) {
1770 Walker
= (CHAR16
*)Path
;
1772 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1773 if (TestPath
!= NULL
) {
1774 TempChar
= StrStr(TestPath
, L
";");
1775 if (TempChar
!= NULL
) {
1776 *TempChar
= CHAR_NULL
;
1778 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1779 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1781 if (FileName
[0] == L
'\\') {
1784 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1785 if (StrStr(Walker
, L
";") != NULL
) {
1786 Walker
= StrStr(Walker
, L
";") + 1;
1790 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1791 if (!EFI_ERROR(Status
)){
1792 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1793 ASSERT(RetVal
== NULL
);
1794 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1795 ShellCloseFile(&Handle
);
1798 ShellCloseFile(&Handle
);
1802 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1809 Find a file by searching the CWD and then the path with a variable set of file
1810 extensions. If the file is not found it will append each extension in the list
1811 in the order provided and return the first one that is successful.
1813 If FileName is NULL, then ASSERT.
1814 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1816 If the return value is not NULL then the memory must be caller freed.
1818 @param[in] FileName Filename string.
1819 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1821 @retval NULL The file was not found.
1822 @retval !NULL The path to the file.
1826 ShellFindFilePathEx (
1827 IN CONST CHAR16
*FileName
,
1828 IN CONST CHAR16
*FileExtension
1833 CONST CHAR16
*ExtensionWalker
;
1838 ASSERT(FileName
!= NULL
);
1839 if (FileExtension
== NULL
) {
1840 return (ShellFindFilePath(FileName
));
1842 RetVal
= ShellFindFilePath(FileName
);
1843 if (RetVal
!= NULL
) {
1846 Size
= StrSize(FileName
);
1847 Size
+= StrSize(FileExtension
);
1848 TestPath
= AllocateZeroPool(Size
);
1849 if (TestPath
== NULL
) {
1852 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1853 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1854 if (ExtensionWalker
!= NULL
) {
1855 StrCatS(TestPath
, Size
/sizeof(CHAR16
), ExtensionWalker
);
1857 TempChar
= StrStr(TestPath
, L
";");
1858 if (TempChar
!= NULL
) {
1859 *TempChar
= CHAR_NULL
;
1861 RetVal
= ShellFindFilePath(TestPath
);
1862 if (RetVal
!= NULL
) {
1865 ASSERT(ExtensionWalker
!= NULL
);
1866 TempChar2
= StrStr(ExtensionWalker
, L
";");
1875 SHELL_PARAM_TYPE Type
;
1877 UINTN OriginalPosition
;
1878 } SHELL_PARAM_PACKAGE
;
1881 Checks the list of valid arguments and returns TRUE if the item was found. If the
1882 return value is TRUE then the type parameter is set also.
1884 if CheckList is NULL then ASSERT();
1885 if Name is NULL then ASSERT();
1886 if Type is NULL then ASSERT();
1888 @param Name pointer to Name of parameter found
1889 @param CheckList List to check against
1890 @param Type pointer to type of parameter if it was found
1892 @retval TRUE the Parameter was found. Type is valid.
1893 @retval FALSE the Parameter was not found. Type is not valid.
1897 InternalIsOnCheckList (
1898 IN CONST CHAR16
*Name
,
1899 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1900 OUT SHELL_PARAM_TYPE
*Type
1903 SHELL_PARAM_ITEM
*TempListItem
;
1907 // ASSERT that all 3 pointer parameters aren't NULL
1909 ASSERT(CheckList
!= NULL
);
1910 ASSERT(Type
!= NULL
);
1911 ASSERT(Name
!= NULL
);
1914 // question mark and page break mode are always supported
1916 if ((StrCmp(Name
, L
"-?") == 0) ||
1917 (StrCmp(Name
, L
"-b") == 0)
1924 // Enumerate through the list
1926 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1928 // If the Type is TypeStart only check the first characters of the passed in param
1929 // If it matches set the type and return TRUE
1931 if (TempListItem
->Type
== TypeStart
) {
1932 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1933 *Type
= TempListItem
->Type
;
1937 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1938 if (TempString
!= NULL
) {
1939 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1940 *Type
= TempListItem
->Type
;
1941 FreePool(TempString
);
1944 FreePool(TempString
);
1946 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1947 *Type
= TempListItem
->Type
;
1955 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1957 @param[in] Name pointer to Name of parameter found
1958 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1959 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1961 @retval TRUE the Parameter is a flag.
1962 @retval FALSE the Parameter not a flag.
1967 IN CONST CHAR16
*Name
,
1968 IN CONST BOOLEAN AlwaysAllowNumbers
,
1969 IN CONST BOOLEAN TimeNumbers
1973 // ASSERT that Name isn't NULL
1975 ASSERT(Name
!= NULL
);
1978 // If we accept numbers then dont return TRUE. (they will be values)
1980 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
1985 // If the Name has a /, +, or - as the first character return TRUE
1987 if ((Name
[0] == L
'/') ||
1988 (Name
[0] == L
'-') ||
1997 Checks the command line arguments passed against the list of valid ones.
1999 If no initialization is required, then return RETURN_SUCCESS.
2001 @param[in] CheckList pointer to list of parameters to check
2002 @param[out] CheckPackage pointer to pointer to list checked values
2003 @param[out] ProblemParam optional pointer to pointer to unicode string for
2004 the paramater that caused failure. If used then the
2005 caller is responsible for freeing the memory.
2006 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2007 @param[in] Argv pointer to array of parameters
2008 @param[in] Argc Count of parameters in Argv
2009 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
2011 @retval EFI_SUCCESS The operation completed sucessfully.
2012 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2013 @retval EFI_INVALID_PARAMETER A parameter was invalid
2014 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2015 duplicated. the duplicated command line argument
2016 was returned in ProblemParam if provided.
2017 @retval EFI_NOT_FOUND a argument required a value that was missing.
2018 the invalid command line argument was returned in
2019 ProblemParam if provided.
2023 InternalCommandLineParse (
2024 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2025 OUT LIST_ENTRY
**CheckPackage
,
2026 OUT CHAR16
**ProblemParam OPTIONAL
,
2027 IN BOOLEAN AutoPageBreak
,
2028 IN CONST CHAR16
**Argv
,
2030 IN BOOLEAN AlwaysAllowNumbers
2034 SHELL_PARAM_TYPE CurrentItemType
;
2035 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2039 CONST CHAR16
*TempPointer
;
2040 UINTN CurrentValueSize
;
2043 CurrentItemPackage
= NULL
;
2049 // If there is only 1 item we dont need to do anything
2052 *CheckPackage
= NULL
;
2053 return (EFI_SUCCESS
);
2059 ASSERT(CheckList
!= NULL
);
2060 ASSERT(Argv
!= NULL
);
2063 // initialize the linked list
2065 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2066 if (*CheckPackage
== NULL
) {
2067 return (EFI_OUT_OF_RESOURCES
);
2070 InitializeListHead(*CheckPackage
);
2073 // loop through each of the arguments
2075 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2076 if (Argv
[LoopCounter
] == NULL
) {
2078 // do nothing for NULL argv
2080 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2082 // We might have leftover if last parameter didnt have optional value
2084 if (GetItemValue
!= 0) {
2086 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2091 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2092 if (CurrentItemPackage
== NULL
) {
2093 ShellCommandLineFreeVarList(*CheckPackage
);
2094 *CheckPackage
= NULL
;
2095 return (EFI_OUT_OF_RESOURCES
);
2097 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2098 if (CurrentItemPackage
->Name
== NULL
) {
2099 ShellCommandLineFreeVarList(*CheckPackage
);
2100 *CheckPackage
= NULL
;
2101 return (EFI_OUT_OF_RESOURCES
);
2103 CurrentItemPackage
->Type
= CurrentItemType
;
2104 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2105 CurrentItemPackage
->Value
= NULL
;
2108 // Does this flag require a value
2110 switch (CurrentItemPackage
->Type
) {
2112 // possibly trigger the next loop(s) to populate the value of this item
2119 case TypeDoubleValue
:
2124 GetItemValue
= (UINTN
)(-1);
2129 // this item has no value expected; we are done
2131 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2132 ASSERT(GetItemValue
== 0);
2135 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, (BOOLEAN
)(CurrentItemPackage
->Type
== TypeTimeValue
))) {
2137 // get the item VALUE for a previous flag
2139 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2140 NewValue
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2141 if (NewValue
== NULL
) {
2142 SHELL_FREE_NON_NULL (CurrentItemPackage
->Value
);
2143 SHELL_FREE_NON_NULL (CurrentItemPackage
);
2144 ShellCommandLineFreeVarList (*CheckPackage
);
2145 *CheckPackage
= NULL
;
2146 return EFI_OUT_OF_RESOURCES
;
2148 CurrentItemPackage
->Value
= NewValue
;
2149 if (ValueSize
== 0) {
2150 StrCpyS( CurrentItemPackage
->Value
,
2151 CurrentValueSize
/sizeof(CHAR16
),
2155 StrCatS( CurrentItemPackage
->Value
,
2156 CurrentValueSize
/sizeof(CHAR16
),
2159 StrCatS( CurrentItemPackage
->Value
,
2160 CurrentValueSize
/sizeof(CHAR16
),
2164 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2167 if (GetItemValue
== 0) {
2168 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2170 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2172 // add this one as a non-flag
2175 TempPointer
= Argv
[LoopCounter
];
2176 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2177 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2178 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2182 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2183 if (CurrentItemPackage
== NULL
) {
2184 ShellCommandLineFreeVarList(*CheckPackage
);
2185 *CheckPackage
= NULL
;
2186 return (EFI_OUT_OF_RESOURCES
);
2188 CurrentItemPackage
->Name
= NULL
;
2189 CurrentItemPackage
->Type
= TypePosition
;
2190 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2191 if (CurrentItemPackage
->Value
== NULL
) {
2192 ShellCommandLineFreeVarList(*CheckPackage
);
2193 *CheckPackage
= NULL
;
2194 return (EFI_OUT_OF_RESOURCES
);
2196 CurrentItemPackage
->OriginalPosition
= Count
++;
2197 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2200 // this was a non-recognised flag... error!
2202 if (ProblemParam
!= NULL
) {
2203 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2205 ShellCommandLineFreeVarList(*CheckPackage
);
2206 *CheckPackage
= NULL
;
2207 return (EFI_VOLUME_CORRUPTED
);
2210 if (GetItemValue
!= 0) {
2212 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2215 // support for AutoPageBreak
2217 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2218 ShellSetPageBreakMode(TRUE
);
2220 return (EFI_SUCCESS
);
2224 Checks the command line arguments passed against the list of valid ones.
2225 Optionally removes NULL values first.
2227 If no initialization is required, then return RETURN_SUCCESS.
2229 @param[in] CheckList The pointer to list of parameters to check.
2230 @param[out] CheckPackage The package of checked values.
2231 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2232 the paramater that caused failure.
2233 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2234 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2236 @retval EFI_SUCCESS The operation completed sucessfully.
2237 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2238 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2239 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2240 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2241 of the command line arguments was returned in
2242 ProblemParam if provided.
2243 @retval EFI_NOT_FOUND A argument required a value that was missing.
2244 The invalid command line argument was returned in
2245 ProblemParam if provided.
2249 ShellCommandLineParseEx (
2250 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2251 OUT LIST_ENTRY
**CheckPackage
,
2252 OUT CHAR16
**ProblemParam OPTIONAL
,
2253 IN BOOLEAN AutoPageBreak
,
2254 IN BOOLEAN AlwaysAllowNumbers
2258 // ASSERT that CheckList and CheckPackage aren't NULL
2260 ASSERT(CheckList
!= NULL
);
2261 ASSERT(CheckPackage
!= NULL
);
2264 // Check for UEFI Shell 2.0 protocols
2266 if (gEfiShellParametersProtocol
!= NULL
) {
2267 return (InternalCommandLineParse(CheckList
,
2271 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2272 gEfiShellParametersProtocol
->Argc
,
2273 AlwaysAllowNumbers
));
2277 // ASSERT That EFI Shell is not required
2279 ASSERT (mEfiShellInterface
!= NULL
);
2280 return (InternalCommandLineParse(CheckList
,
2284 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2285 mEfiShellInterface
->Argc
,
2286 AlwaysAllowNumbers
));
2290 Frees shell variable list that was returned from ShellCommandLineParse.
2292 This function will free all the memory that was used for the CheckPackage
2293 list of postprocessed shell arguments.
2295 this function has no return value.
2297 if CheckPackage is NULL, then return
2299 @param CheckPackage the list to de-allocate
2303 ShellCommandLineFreeVarList (
2304 IN LIST_ENTRY
*CheckPackage
2310 // check for CheckPackage == NULL
2312 if (CheckPackage
== NULL
) {
2317 // for each node in the list
2319 for ( Node
= GetFirstNode(CheckPackage
)
2320 ; !IsListEmpty(CheckPackage
)
2321 ; Node
= GetFirstNode(CheckPackage
)
2324 // Remove it from the list
2326 RemoveEntryList(Node
);
2329 // if it has a name free the name
2331 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2332 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2336 // if it has a value free the value
2338 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2339 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2343 // free the node structure
2345 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2348 // free the list head node
2350 FreePool(CheckPackage
);
2353 Checks for presence of a flag parameter
2355 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2357 if CheckPackage is NULL then return FALSE.
2358 if KeyString is NULL then ASSERT()
2360 @param CheckPackage The package of parsed command line arguments
2361 @param KeyString the Key of the command line argument to check for
2363 @retval TRUE the flag is on the command line
2364 @retval FALSE the flag is not on the command line
2368 ShellCommandLineGetFlag (
2369 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2370 IN CONST CHAR16
* CONST KeyString
2377 // return FALSE for no package or KeyString is NULL
2379 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2384 // enumerate through the list of parametrs
2386 for ( Node
= GetFirstNode(CheckPackage
)
2387 ; !IsNull (CheckPackage
, Node
)
2388 ; Node
= GetNextNode(CheckPackage
, Node
)
2391 // If the Name matches, return TRUE (and there may be NULL name)
2393 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2395 // If Type is TypeStart then only compare the begining of the strings
2397 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2398 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2402 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2403 if (TempString
!= NULL
) {
2404 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2405 FreePool(TempString
);
2408 FreePool(TempString
);
2410 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2418 Returns value from command line argument.
2420 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2422 If CheckPackage is NULL, then return NULL.
2424 @param[in] CheckPackage The package of parsed command line arguments.
2425 @param[in] KeyString The Key of the command line argument to check for.
2427 @retval NULL The flag is not on the command line.
2428 @retval !=NULL The pointer to unicode string of the value.
2432 ShellCommandLineGetValue (
2433 IN CONST LIST_ENTRY
*CheckPackage
,
2434 IN CHAR16
*KeyString
2441 // return NULL for no package or KeyString is NULL
2443 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2448 // enumerate through the list of parametrs
2450 for ( Node
= GetFirstNode(CheckPackage
)
2451 ; !IsNull (CheckPackage
, Node
)
2452 ; Node
= GetNextNode(CheckPackage
, Node
)
2455 // If the Name matches, return TRUE (and there may be NULL name)
2457 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2459 // If Type is TypeStart then only compare the begining of the strings
2461 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2462 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2463 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2466 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2467 if (TempString
!= NULL
) {
2468 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2469 FreePool(TempString
);
2470 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2472 FreePool(TempString
);
2474 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2475 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2483 Returns raw value from command line argument.
2485 Raw value parameters are in the form of "value" in a specific position in the list.
2487 If CheckPackage is NULL, then return NULL.
2489 @param[in] CheckPackage The package of parsed command line arguments.
2490 @param[in] Position The position of the value.
2492 @retval NULL The flag is not on the command line.
2493 @retval !=NULL The pointer to unicode string of the value.
2497 ShellCommandLineGetRawValue (
2498 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2505 // check for CheckPackage == NULL
2507 if (CheckPackage
== NULL
) {
2512 // enumerate through the list of parametrs
2514 for ( Node
= GetFirstNode(CheckPackage
)
2515 ; !IsNull (CheckPackage
, Node
)
2516 ; Node
= GetNextNode(CheckPackage
, Node
)
2519 // If the position matches, return the value
2521 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2522 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2529 returns the number of command line value parameters that were parsed.
2531 this will not include flags.
2533 @param[in] CheckPackage The package of parsed command line arguments.
2535 @retval (UINTN)-1 No parsing has ocurred
2536 @return other The number of value parameters found
2540 ShellCommandLineGetCount(
2541 IN CONST LIST_ENTRY
*CheckPackage
2547 if (CheckPackage
== NULL
) {
2550 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2551 ; !IsNull (CheckPackage
, Node1
)
2552 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2554 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2562 Determines if a parameter is duplicated.
2564 If Param is not NULL then it will point to a callee allocated string buffer
2565 with the parameter value if a duplicate is found.
2567 If CheckPackage is NULL, then ASSERT.
2569 @param[in] CheckPackage The package of parsed command line arguments.
2570 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2572 @retval EFI_SUCCESS No parameters were duplicated.
2573 @retval EFI_DEVICE_ERROR A duplicate was found.
2577 ShellCommandLineCheckDuplicate (
2578 IN CONST LIST_ENTRY
*CheckPackage
,
2585 ASSERT(CheckPackage
!= NULL
);
2587 for ( Node1
= GetFirstNode(CheckPackage
)
2588 ; !IsNull (CheckPackage
, Node1
)
2589 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2591 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2592 ; !IsNull (CheckPackage
, Node2
)
2593 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2595 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) {
2596 if (Param
!= NULL
) {
2598 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2600 return (EFI_DEVICE_ERROR
);
2604 return (EFI_SUCCESS
);
2608 This is a find and replace function. Upon successful return the NewString is a copy of
2609 SourceString with each instance of FindTarget replaced with ReplaceWith.
2611 If SourceString and NewString overlap the behavior is undefined.
2613 If the string would grow bigger than NewSize it will halt and return error.
2615 @param[in] SourceString The string with source buffer.
2616 @param[in, out] NewString The string with resultant buffer.
2617 @param[in] NewSize The size in bytes of NewString.
2618 @param[in] FindTarget The string to look for.
2619 @param[in] ReplaceWith The string to replace FindTarget with.
2620 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2621 immediately before it.
2622 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2624 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2625 @retval EFI_INVALID_PARAMETER NewString was NULL.
2626 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2627 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2628 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2629 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2630 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2631 the new string (truncation occurred).
2632 @retval EFI_SUCCESS The string was successfully copied with replacement.
2636 ShellCopySearchAndReplace(
2637 IN CHAR16 CONST
*SourceString
,
2638 IN OUT CHAR16
*NewString
,
2640 IN CONST CHAR16
*FindTarget
,
2641 IN CONST CHAR16
*ReplaceWith
,
2642 IN CONST BOOLEAN SkipPreCarrot
,
2643 IN CONST BOOLEAN ParameterReplacing
2649 if ( (SourceString
== NULL
)
2650 || (NewString
== NULL
)
2651 || (FindTarget
== NULL
)
2652 || (ReplaceWith
== NULL
)
2653 || (StrLen(FindTarget
) < 1)
2654 || (StrLen(SourceString
) < 1)
2656 return (EFI_INVALID_PARAMETER
);
2659 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2660 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2662 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2663 if (Replace
!= NULL
) {
2664 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2667 if (Replace
== NULL
) {
2668 return (EFI_OUT_OF_RESOURCES
);
2670 NewString
= ZeroMem(NewString
, NewSize
);
2671 while (*SourceString
!= CHAR_NULL
) {
2673 // if we find the FindTarget and either Skip == FALSE or Skip and we
2674 // dont have a carrot do a replace...
2676 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2677 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2679 SourceString
+= StrLen(FindTarget
);
2680 Size
= StrSize(NewString
);
2681 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2683 return (EFI_BUFFER_TOO_SMALL
);
2685 StrCatS(NewString
, NewSize
/sizeof(CHAR16
), Replace
);
2687 Size
= StrSize(NewString
);
2688 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2690 return (EFI_BUFFER_TOO_SMALL
);
2692 StrnCatS(NewString
, NewSize
/sizeof(CHAR16
), SourceString
, 1);
2697 return (EFI_SUCCESS
);
2701 Internal worker function to output a string.
2703 This function will output a string to the correct StdOut.
2705 @param[in] String The string to print out.
2707 @retval EFI_SUCCESS The operation was sucessful.
2708 @retval !EFI_SUCCESS The operation failed.
2713 IN CONST CHAR16
*String
2717 Size
= StrSize(String
) - sizeof(CHAR16
);
2719 return (EFI_SUCCESS
);
2721 if (gEfiShellParametersProtocol
!= NULL
) {
2722 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2724 if (mEfiShellInterface
!= NULL
) {
2725 if (mEfiShellInterface
->RedirArgc
== 0) {
2727 // Divide in half for old shell. Must be string length not size.
2729 Size
/=2; // Divide in half only when no redirection.
2731 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2734 return (EFI_UNSUPPORTED
);
2738 Print at a specific location on the screen.
2740 This function will move the cursor to a given screen location and print the specified string
2742 If -1 is specified for either the Row or Col the current screen location for BOTH
2745 if either Row or Col is out of range for the current console, then ASSERT
2746 if Format is NULL, then ASSERT
2748 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2749 the following additional flags:
2750 %N - Set output attribute to normal
2751 %H - Set output attribute to highlight
2752 %E - Set output attribute to error
2753 %B - Set output attribute to blue color
2754 %V - Set output attribute to green color
2756 Note: The background color is controlled by the shell command cls.
2758 @param[in] Col the column to print at
2759 @param[in] Row the row to print at
2760 @param[in] Format the format string
2761 @param[in] Marker the marker for the variable argument list
2763 @return EFI_SUCCESS The operation was successful.
2764 @return EFI_DEVICE_ERROR The console device reported an error.
2768 InternalShellPrintWorker(
2769 IN INT32 Col OPTIONAL
,
2770 IN INT32 Row OPTIONAL
,
2771 IN CONST CHAR16
*Format
,
2776 CHAR16
*ResumeLocation
;
2777 CHAR16
*FormatWalker
;
2778 UINTN OriginalAttribute
;
2779 CHAR16
*mPostReplaceFormat
;
2780 CHAR16
*mPostReplaceFormat2
;
2782 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2783 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2785 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2786 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2787 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2788 return (EFI_OUT_OF_RESOURCES
);
2791 Status
= EFI_SUCCESS
;
2792 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2795 // Back and forth each time fixing up 1 of our flags...
2797 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2798 ASSERT_EFI_ERROR(Status
);
2799 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2800 ASSERT_EFI_ERROR(Status
);
2801 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2802 ASSERT_EFI_ERROR(Status
);
2803 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2804 ASSERT_EFI_ERROR(Status
);
2805 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2806 ASSERT_EFI_ERROR(Status
);
2809 // Use the last buffer from replacing to print from...
2811 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2813 if (Col
!= -1 && Row
!= -1) {
2814 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2817 FormatWalker
= mPostReplaceFormat2
;
2818 while (*FormatWalker
!= CHAR_NULL
) {
2820 // Find the next attribute change request
2822 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2823 if (ResumeLocation
!= NULL
) {
2824 *ResumeLocation
= CHAR_NULL
;
2827 // print the current FormatWalker string
2829 if (StrLen(FormatWalker
)>0) {
2830 Status
= InternalPrintTo(FormatWalker
);
2831 if (EFI_ERROR(Status
)) {
2837 // update the attribute
2839 if (ResumeLocation
!= NULL
) {
2840 if (*(ResumeLocation
-1) == L
'^') {
2842 // Move cursor back 1 position to overwrite the ^
2844 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2847 // Print a simple '%' symbol
2849 Status
= InternalPrintTo(L
"%");
2850 ResumeLocation
= ResumeLocation
- 1;
2852 switch (*(ResumeLocation
+1)) {
2854 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2857 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2860 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2863 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2866 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2870 // Print a simple '%' symbol
2872 Status
= InternalPrintTo(L
"%");
2873 if (EFI_ERROR(Status
)) {
2876 ResumeLocation
= ResumeLocation
- 1;
2882 // reset to normal now...
2888 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2890 FormatWalker
= ResumeLocation
+ 2;
2893 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2895 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2896 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2901 Print at a specific location on the screen.
2903 This function will move the cursor to a given screen location and print the specified string.
2905 If -1 is specified for either the Row or Col the current screen location for BOTH
2908 If either Row or Col is out of range for the current console, then ASSERT.
2909 If Format is NULL, then ASSERT.
2911 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2912 the following additional flags:
2913 %N - Set output attribute to normal
2914 %H - Set output attribute to highlight
2915 %E - Set output attribute to error
2916 %B - Set output attribute to blue color
2917 %V - Set output attribute to green color
2919 Note: The background color is controlled by the shell command cls.
2921 @param[in] Col the column to print at
2922 @param[in] Row the row to print at
2923 @param[in] Format the format string
2924 @param[in] ... The variable argument list.
2926 @return EFI_SUCCESS The printing was successful.
2927 @return EFI_DEVICE_ERROR The console device reported an error.
2932 IN INT32 Col OPTIONAL
,
2933 IN INT32 Row OPTIONAL
,
2934 IN CONST CHAR16
*Format
,
2940 if (Format
== NULL
) {
2941 return (EFI_INVALID_PARAMETER
);
2943 VA_START (Marker
, Format
);
2944 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2950 Print at a specific location on the screen.
2952 This function will move the cursor to a given screen location and print the specified string.
2954 If -1 is specified for either the Row or Col the current screen location for BOTH
2957 If either Row or Col is out of range for the current console, then ASSERT.
2958 If Format is NULL, then ASSERT.
2960 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2961 the following additional flags:
2962 %N - Set output attribute to normal.
2963 %H - Set output attribute to highlight.
2964 %E - Set output attribute to error.
2965 %B - Set output attribute to blue color.
2966 %V - Set output attribute to green color.
2968 Note: The background color is controlled by the shell command cls.
2970 @param[in] Col The column to print at.
2971 @param[in] Row The row to print at.
2972 @param[in] Language The language of the string to retrieve. If this parameter
2973 is NULL, then the current platform language is used.
2974 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2975 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2976 @param[in] ... The variable argument list.
2978 @return EFI_SUCCESS The printing was successful.
2979 @return EFI_DEVICE_ERROR The console device reported an error.
2984 IN INT32 Col OPTIONAL
,
2985 IN INT32 Row OPTIONAL
,
2986 IN CONST CHAR8
*Language OPTIONAL
,
2987 IN CONST EFI_STRING_ID HiiFormatStringId
,
2988 IN CONST EFI_HANDLE HiiFormatHandle
,
2993 CHAR16
*HiiFormatString
;
2996 RetVal
= EFI_DEVICE_ERROR
;
2998 VA_START (Marker
, HiiFormatHandle
);
2999 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
3000 if (HiiFormatString
!= NULL
) {
3001 RetVal
= InternalShellPrintWorker (Col
, Row
, HiiFormatString
, Marker
);
3002 SHELL_FREE_NON_NULL (HiiFormatString
);
3010 Function to determine if a given filename represents a file or a directory.
3012 @param[in] DirName Path to directory to test.
3014 @retval EFI_SUCCESS The Path represents a directory
3015 @retval EFI_NOT_FOUND The Path does not represent a directory
3016 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3017 @return The path failed to open
3022 IN CONST CHAR16
*DirName
3026 SHELL_FILE_HANDLE Handle
;
3027 CHAR16
*TempLocation
;
3028 CHAR16
*TempLocation2
;
3030 ASSERT(DirName
!= NULL
);
3033 TempLocation
= NULL
;
3035 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3036 if (EFI_ERROR(Status
)) {
3038 // try good logic first.
3040 if (gEfiShellProtocol
!= NULL
) {
3041 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3042 if (TempLocation
== NULL
) {
3043 ShellCloseFile(&Handle
);
3044 return (EFI_OUT_OF_RESOURCES
);
3046 TempLocation2
= StrStr(TempLocation
, L
":");
3047 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3048 *(TempLocation2
+1) = CHAR_NULL
;
3050 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3051 FreePool(TempLocation
);
3052 return (EFI_SUCCESS
);
3054 FreePool(TempLocation
);
3057 // probably a map name?!?!!?
3059 TempLocation
= StrStr(DirName
, L
"\\");
3060 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3061 return (EFI_SUCCESS
);
3067 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3068 ShellCloseFile(&Handle
);
3069 return (EFI_SUCCESS
);
3071 ShellCloseFile(&Handle
);
3072 return (EFI_NOT_FOUND
);
3076 Function to determine if a given filename represents a file.
3078 @param[in] Name Path to file to test.
3080 @retval EFI_SUCCESS The Path represents a file.
3081 @retval EFI_NOT_FOUND The Path does not represent a file.
3082 @retval other The path failed to open.
3087 IN CONST CHAR16
*Name
3091 SHELL_FILE_HANDLE Handle
;
3093 ASSERT(Name
!= NULL
);
3097 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3098 if (EFI_ERROR(Status
)) {
3102 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3103 ShellCloseFile(&Handle
);
3104 return (EFI_SUCCESS
);
3106 ShellCloseFile(&Handle
);
3107 return (EFI_NOT_FOUND
);
3111 Function to determine if a given filename represents a file.
3113 This will search the CWD and then the Path.
3115 If Name is NULL, then ASSERT.
3117 @param[in] Name Path to file to test.
3119 @retval EFI_SUCCESS The Path represents a file.
3120 @retval EFI_NOT_FOUND The Path does not represent a file.
3121 @retval other The path failed to open.
3126 IN CONST CHAR16
*Name
3132 if (!EFI_ERROR(ShellIsFile(Name
))) {
3133 return (EFI_SUCCESS
);
3136 NewName
= ShellFindFilePath(Name
);
3137 if (NewName
== NULL
) {
3138 return (EFI_NOT_FOUND
);
3140 Status
= ShellIsFile(NewName
);
3146 Function return the number converted from a hex representation of a number.
3148 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3149 result. Use ShellConvertStringToUint64 instead.
3151 @param[in] String String representation of a number.
3153 @return The unsigned integer result of the conversion.
3154 @retval (UINTN)(-1) An error occured.
3159 IN CONST CHAR16
*String
3164 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3165 return ((UINTN
)RetVal
);
3168 return ((UINTN
)(-1));
3172 Function to determine whether a string is decimal or hex representation of a number
3173 and return the number converted from the string. Spaces are always skipped.
3175 @param[in] String String representation of a number
3178 @retval (UINTN)(-1) An error ocurred.
3183 IN CONST CHAR16
*String
3191 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3195 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3196 return ((UINTN
)RetVal
);
3198 return ((UINTN
)(-1));
3202 Safely append with automatic string resizing given length of Destination and
3203 desired length of copy from Source.
3205 append the first D characters of Source to the end of Destination, where D is
3206 the lesser of Count and the StrLen() of Source. If appending those D characters
3207 will fit within Destination (whose Size is given as CurrentSize) and
3208 still leave room for a NULL terminator, then those characters are appended,
3209 starting at the original terminating NULL of Destination, and a new terminating
3212 If appending D characters onto Destination will result in a overflow of the size
3213 given in CurrentSize the string will be grown such that the copy can be performed
3214 and CurrentSize will be updated to the new size.
3216 If Source is NULL, there is nothing to append, just return the current buffer in
3219 if Destination is NULL, then ASSERT()
3220 if Destination's current length (including NULL terminator) is already more then
3221 CurrentSize, then ASSERT()
3223 @param[in, out] Destination The String to append onto
3224 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3225 return possibly the new size (still in bytes). if NULL
3226 then allocate whatever is needed.
3227 @param[in] Source The String to append from
3228 @param[in] Count Maximum number of characters to append. if 0 then
3231 @return Destination return the resultant string.
3236 IN OUT CHAR16
**Destination
,
3237 IN OUT UINTN
*CurrentSize
,
3238 IN CONST CHAR16
*Source
,
3242 UINTN DestinationStartSize
;
3248 ASSERT(Destination
!= NULL
);
3251 // If there's nothing to do then just return Destination
3253 if (Source
== NULL
) {
3254 return (*Destination
);
3258 // allow for un-initialized pointers, based on size being 0
3260 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3261 *Destination
= NULL
;
3265 // allow for NULL pointers address as Destination
3267 if (*Destination
!= NULL
) {
3268 ASSERT(CurrentSize
!= 0);
3269 DestinationStartSize
= StrSize(*Destination
);
3270 ASSERT(DestinationStartSize
<= *CurrentSize
);
3272 DestinationStartSize
= 0;
3273 // ASSERT(*CurrentSize == 0);
3277 // Append all of Source?
3280 Count
= StrLen(Source
);
3284 // Test and grow if required
3286 if (CurrentSize
!= NULL
) {
3287 NewSize
= *CurrentSize
;
3288 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3289 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3290 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3292 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3293 *CurrentSize
= NewSize
;
3296 NewSize
= (Count
+1)*sizeof(CHAR16
);
3297 *Destination
= AllocateZeroPool(NewSize
);
3301 // Now use standard StrnCat on a big enough buffer
3303 if (*Destination
== NULL
) {
3307 StrnCatS(*Destination
, NewSize
/sizeof(CHAR16
), Source
, Count
);
3308 return *Destination
;
3312 Prompt the user and return the resultant answer to the requestor.
3314 This function will display the requested question on the shell prompt and then
3315 wait for an appropriate answer to be input from the console.
3317 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3318 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3320 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3323 In either case *Response must be callee freed if Response was not NULL;
3325 @param Type What type of question is asked. This is used to filter the input
3326 to prevent invalid answers to question.
3327 @param Prompt Pointer to string prompt to use to request input.
3328 @param Response Pointer to Response which will be populated upon return.
3330 @retval EFI_SUCCESS The operation was sucessful.
3331 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3332 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3333 @return other The operation failed.
3337 ShellPromptForResponse (
3338 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3339 IN CHAR16
*Prompt OPTIONAL
,
3340 IN OUT VOID
**Response OPTIONAL
3346 SHELL_PROMPT_RESPONSE
*Resp
;
3350 Status
= EFI_UNSUPPORTED
;
3354 if (Type
!= ShellPromptResponseTypeFreeform
) {
3355 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3357 return (EFI_OUT_OF_RESOURCES
);
3362 case ShellPromptResponseTypeQuitContinue
:
3363 if (Prompt
!= NULL
) {
3364 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3367 // wait for valid response
3369 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3370 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3371 if (EFI_ERROR(Status
)) {
3374 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3375 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3376 *Resp
= ShellPromptResponseQuit
;
3378 *Resp
= ShellPromptResponseContinue
;
3381 case ShellPromptResponseTypeYesNoCancel
:
3382 if (Prompt
!= NULL
) {
3383 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3386 // wait for valid response
3388 *Resp
= ShellPromptResponseMax
;
3389 while (*Resp
== ShellPromptResponseMax
) {
3390 if (ShellGetExecutionBreakFlag()) {
3391 Status
= EFI_ABORTED
;
3394 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3395 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3396 if (EFI_ERROR(Status
)) {
3399 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3400 switch (Key
.UnicodeChar
) {
3403 *Resp
= ShellPromptResponseYes
;
3407 *Resp
= ShellPromptResponseNo
;
3411 *Resp
= ShellPromptResponseCancel
;
3415 break; case ShellPromptResponseTypeYesNoAllCancel
:
3416 if (Prompt
!= NULL
) {
3417 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3420 // wait for valid response
3422 *Resp
= ShellPromptResponseMax
;
3423 while (*Resp
== ShellPromptResponseMax
) {
3424 if (ShellGetExecutionBreakFlag()) {
3425 Status
= EFI_ABORTED
;
3428 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3429 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3430 if (EFI_ERROR(Status
)) {
3433 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3434 switch (Key
.UnicodeChar
) {
3437 *Resp
= ShellPromptResponseYes
;
3441 *Resp
= ShellPromptResponseNo
;
3445 *Resp
= ShellPromptResponseAll
;
3449 *Resp
= ShellPromptResponseCancel
;
3454 case ShellPromptResponseTypeEnterContinue
:
3455 case ShellPromptResponseTypeAnyKeyContinue
:
3456 if (Prompt
!= NULL
) {
3457 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3460 // wait for valid response
3462 *Resp
= ShellPromptResponseMax
;
3463 while (*Resp
== ShellPromptResponseMax
) {
3464 if (ShellGetExecutionBreakFlag()) {
3465 Status
= EFI_ABORTED
;
3468 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3469 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3470 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3471 if (EFI_ERROR(Status
)) {
3474 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3475 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3476 *Resp
= ShellPromptResponseContinue
;
3480 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3481 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3482 ASSERT_EFI_ERROR(Status
);
3483 *Resp
= ShellPromptResponseContinue
;
3488 case ShellPromptResponseTypeYesNo
:
3489 if (Prompt
!= NULL
) {
3490 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3493 // wait for valid response
3495 *Resp
= ShellPromptResponseMax
;
3496 while (*Resp
== ShellPromptResponseMax
) {
3497 if (ShellGetExecutionBreakFlag()) {
3498 Status
= EFI_ABORTED
;
3501 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3502 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3503 if (EFI_ERROR(Status
)) {
3506 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3507 switch (Key
.UnicodeChar
) {
3510 *Resp
= ShellPromptResponseYes
;
3514 *Resp
= ShellPromptResponseNo
;
3519 case ShellPromptResponseTypeFreeform
:
3520 if (Prompt
!= NULL
) {
3521 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3524 if (ShellGetExecutionBreakFlag()) {
3525 Status
= EFI_ABORTED
;
3528 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3529 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3530 if (EFI_ERROR(Status
)) {
3533 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3534 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3537 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3538 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3542 // This is the location to add new prompt types.
3543 // If your new type loops remember to add ExecutionBreak support.
3549 if (Response
!= NULL
) {
3552 } else if (Buffer
!= NULL
) {
3559 if (Buffer
!= NULL
) {
3564 ShellPrintEx(-1, -1, L
"\r\n");
3569 Prompt the user and return the resultant answer to the requestor.
3571 This function is the same as ShellPromptForResponse, except that the prompt is
3572 automatically pulled from HII.
3574 @param Type What type of question is asked. This is used to filter the input
3575 to prevent invalid answers to question.
3576 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3577 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3578 @param Response Pointer to Response which will be populated upon return.
3580 @retval EFI_SUCCESS the operation was sucessful.
3581 @return other the operation failed.
3583 @sa ShellPromptForResponse
3587 ShellPromptForResponseHii (
3588 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3589 IN CONST EFI_STRING_ID HiiFormatStringId
,
3590 IN CONST EFI_HANDLE HiiFormatHandle
,
3591 IN OUT VOID
**Response
3597 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3598 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3604 Function to determin if an entire string is a valid number.
3606 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3608 @param[in] String The string to evaluate.
3609 @param[in] ForceHex TRUE - always assume hex.
3610 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3611 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3613 @retval TRUE It is all numeric (dec/hex) characters.
3614 @retval FALSE There is a non-numeric character.
3618 InternalShellIsHexOrDecimalNumber (
3619 IN CONST CHAR16
*String
,
3620 IN CONST BOOLEAN ForceHex
,
3621 IN CONST BOOLEAN StopAtSpace
,
3622 IN CONST BOOLEAN TimeNumbers
3628 // chop off a single negative sign
3630 if (String
!= NULL
&& *String
== L
'-') {
3634 if (String
== NULL
) {
3639 // chop leading zeroes
3641 while(String
!= NULL
&& *String
== L
'0'){
3645 // allow '0x' or '0X', but not 'x' or 'X'
3647 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3648 if (*(String
-1) != L
'0') {
3650 // we got an x without a preceeding 0
3656 } else if (ForceHex
) {
3663 // loop through the remaining characters and use the lib function
3665 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3666 if (TimeNumbers
&& (String
[0] == L
':')) {
3670 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3674 if (!ShellIsDecimalDigitCharacter(*String
)) {
3684 Function to determine if a given filename exists.
3686 @param[in] Name Path to test.
3688 @retval EFI_SUCCESS The Path represents a file.
3689 @retval EFI_NOT_FOUND The Path does not represent a file.
3690 @retval other The path failed to open.
3695 IN CONST CHAR16
*Name
3699 EFI_SHELL_FILE_INFO
*List
;
3701 ASSERT(Name
!= NULL
);
3704 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3705 if (EFI_ERROR(Status
)) {
3709 ShellCloseFileMetaArg(&List
);
3711 return (EFI_SUCCESS
);
3715 Convert a Unicode character to upper case only if
3716 it maps to a valid small-case ASCII character.
3718 This internal function only deal with Unicode character
3719 which maps to a valid small-case ASCII character, i.e.
3720 L'a' to L'z'. For other Unicode character, the input character
3721 is returned directly.
3723 @param Char The character to convert.
3725 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3726 @retval Unchanged Otherwise.
3731 InternalShellCharToUpper (
3735 if (Char
>= L
'a' && Char
<= L
'z') {
3736 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3743 Convert a Unicode character to numerical value.
3745 This internal function only deal with Unicode character
3746 which maps to a valid hexadecimal ASII character, i.e.
3747 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3748 Unicode character, the value returned does not make sense.
3750 @param Char The character to convert.
3752 @return The numerical value converted.
3757 InternalShellHexCharToUintn (
3761 if (ShellIsDecimalDigitCharacter (Char
)) {
3765 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3769 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3771 This function returns a value of type UINT64 by interpreting the contents
3772 of the Unicode string specified by String as a hexadecimal number.
3773 The format of the input Unicode string String is:
3775 [spaces][zeros][x][hexadecimal digits].
3777 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3778 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3779 If "x" appears in the input string, it must be prefixed with at least one 0.
3780 The function will ignore the pad space, which includes spaces or tab characters,
3781 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3782 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3783 first valid hexadecimal digit. Then, the function stops at the first character that is
3784 a not a valid hexadecimal character or NULL, whichever one comes first.
3786 If String has only pad spaces, then zero is returned.
3787 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3788 then zero is returned.
3790 @param[in] String A pointer to a Null-terminated Unicode string.
3791 @param[out] Value Upon a successful return the value of the conversion.
3792 @param[in] StopAtSpace FALSE to skip spaces.
3794 @retval EFI_SUCCESS The conversion was successful.
3795 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3796 @retval EFI_DEVICE_ERROR An overflow occured.
3800 InternalShellStrHexToUint64 (
3801 IN CONST CHAR16
*String
,
3803 IN CONST BOOLEAN StopAtSpace
3808 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3809 return (EFI_INVALID_PARAMETER
);
3813 // Ignore the pad spaces (space or tab)
3815 while ((*String
== L
' ') || (*String
== L
'\t')) {
3820 // Ignore leading Zeros after the spaces
3822 while (*String
== L
'0') {
3826 if (InternalShellCharToUpper (*String
) == L
'X') {
3827 if (*(String
- 1) != L
'0') {
3839 // there is a space where there should't be
3841 if (*String
== L
' ') {
3842 return (EFI_INVALID_PARAMETER
);
3845 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3847 // If the Hex Number represented by String overflows according
3848 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3850 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3851 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3852 return (EFI_DEVICE_ERROR
);
3855 Result
= (LShiftU64(Result
, 4));
3856 Result
+= InternalShellHexCharToUintn (*String
);
3860 // stop at spaces if requested
3862 if (StopAtSpace
&& *String
== L
' ') {
3868 return (EFI_SUCCESS
);
3872 Convert a Null-terminated Unicode decimal string to a value of
3875 This function returns a value of type UINT64 by interpreting the contents
3876 of the Unicode string specified by String as a decimal number. The format
3877 of the input Unicode string String is:
3879 [spaces] [decimal digits].
3881 The valid decimal digit character is in the range [0-9]. The
3882 function will ignore the pad space, which includes spaces or
3883 tab characters, before [decimal digits]. The running zero in the
3884 beginning of [decimal digits] will be ignored. Then, the function
3885 stops at the first character that is a not a valid decimal character
3886 or a Null-terminator, whichever one comes first.
3888 If String has only pad spaces, then 0 is returned.
3889 If String has no pad spaces or valid decimal digits,
3892 @param[in] String A pointer to a Null-terminated Unicode string.
3893 @param[out] Value Upon a successful return the value of the conversion.
3894 @param[in] StopAtSpace FALSE to skip spaces.
3896 @retval EFI_SUCCESS The conversion was successful.
3897 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3898 @retval EFI_DEVICE_ERROR An overflow occured.
3902 InternalShellStrDecimalToUint64 (
3903 IN CONST CHAR16
*String
,
3905 IN CONST BOOLEAN StopAtSpace
3910 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3911 return (EFI_INVALID_PARAMETER
);
3915 // Ignore the pad spaces (space or tab)
3917 while ((*String
== L
' ') || (*String
== L
'\t')) {
3922 // Ignore leading Zeros after the spaces
3924 while (*String
== L
'0') {
3931 // Stop upon space if requested
3932 // (if the whole value was 0)
3934 if (StopAtSpace
&& *String
== L
' ') {
3936 return (EFI_SUCCESS
);
3939 while (ShellIsDecimalDigitCharacter (*String
)) {
3941 // If the number represented by String overflows according
3942 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3945 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3946 return (EFI_DEVICE_ERROR
);
3949 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3953 // Stop at spaces if requested
3955 if (StopAtSpace
&& *String
== L
' ') {
3962 return (EFI_SUCCESS
);
3966 Function to verify and convert a string to its numerical value.
3968 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3970 @param[in] String The string to evaluate.
3971 @param[out] Value Upon a successful return the value of the conversion.
3972 @param[in] ForceHex TRUE - always assume hex.
3973 @param[in] StopAtSpace FALSE to skip spaces.
3975 @retval EFI_SUCCESS The conversion was successful.
3976 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3977 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3981 ShellConvertStringToUint64(
3982 IN CONST CHAR16
*String
,
3984 IN CONST BOOLEAN ForceHex
,
3985 IN CONST BOOLEAN StopAtSpace
3989 CONST CHAR16
*Walker
;
3995 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3998 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3999 return (EFI_INVALID_PARAMETER
);
4002 return (EFI_INVALID_PARAMETER
);
4007 // Chop off leading spaces
4009 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
4012 // make sure we have something left that is numeric.
4014 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
4015 return (EFI_INVALID_PARAMETER
);
4019 // do the conversion.
4021 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
4022 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
4024 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
4027 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
4028 return (EFI_NOT_FOUND
);
4031 if (Value
!= NULL
) {
4039 Function to determin if an entire string is a valid number.
4041 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4043 @param[in] String The string to evaluate.
4044 @param[in] ForceHex TRUE - always assume hex.
4045 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4047 @retval TRUE It is all numeric (dec/hex) characters.
4048 @retval FALSE There is a non-numeric character.
4052 ShellIsHexOrDecimalNumber (
4053 IN CONST CHAR16
*String
,
4054 IN CONST BOOLEAN ForceHex
,
4055 IN CONST BOOLEAN StopAtSpace
4058 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4065 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4066 buffer. The returned buffer must be callee freed.
4068 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4069 maintained and not changed for all operations with the same file.
4071 @param[in] Handle SHELL_FILE_HANDLE to read from.
4072 @param[in, out] Ascii Boolean value for indicating whether the file is
4073 Ascii (TRUE) or UCS2 (FALSE).
4075 @return The line of text from the file.
4076 @retval NULL There was not enough memory available.
4078 @sa ShellFileHandleReadLine
4082 ShellFileHandleReturnLine(
4083 IN SHELL_FILE_HANDLE Handle
,
4084 IN OUT BOOLEAN
*Ascii
4094 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4095 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4096 RetVal
= AllocateZeroPool(Size
);
4097 if (RetVal
== NULL
) {
4100 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4103 if (Status
== EFI_END_OF_FILE
&& RetVal
!= NULL
&& *RetVal
!= CHAR_NULL
) {
4104 Status
= EFI_SUCCESS
;
4106 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4114 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4116 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4117 maintained and not changed for all operations with the same file.
4119 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4122 @param[in] Handle SHELL_FILE_HANDLE to read from.
4123 @param[in, out] Buffer The pointer to buffer to read into. If this function
4124 returns EFI_SUCCESS, then on output Buffer will
4125 contain a UCS2 string, even if the file being
4127 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4128 On output, unchanged unless Buffer is too small
4129 to contain the next line of the file. In that
4130 case Size is set to the number of bytes needed
4131 to hold the next line of the file (as a UCS2
4132 string, even if it is an ASCII file).
4133 @param[in] Truncate If the buffer is large enough, this has no effect.
4134 If the buffer is is too small and Truncate is TRUE,
4135 the line will be truncated.
4136 If the buffer is is too small and Truncate is FALSE,
4137 then no read will occur.
4139 @param[in, out] Ascii Boolean value for indicating whether the file is
4140 Ascii (TRUE) or UCS2 (FALSE).
4142 @retval EFI_SUCCESS The operation was successful. The line is stored in
4144 @retval EFI_END_OF_FILE There are no more lines in the file.
4145 @retval EFI_INVALID_PARAMETER Handle was NULL.
4146 @retval EFI_INVALID_PARAMETER Size was NULL.
4147 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4148 Size was updated to the minimum space required.
4152 ShellFileHandleReadLine(
4153 IN SHELL_FILE_HANDLE Handle
,
4154 IN OUT CHAR16
*Buffer
,
4156 IN BOOLEAN Truncate
,
4157 IN OUT BOOLEAN
*Ascii
4164 UINT64 OriginalFilePosition
;
4170 return (EFI_INVALID_PARAMETER
);
4172 if (Buffer
== NULL
) {
4175 *Buffer
= CHAR_NULL
;
4177 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4178 if (OriginalFilePosition
== 0) {
4179 CharSize
= sizeof(CHAR16
);
4180 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4181 ASSERT_EFI_ERROR(Status
);
4182 if (CharBuffer
== gUnicodeFileTag
) {
4186 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4191 CharSize
= sizeof(CHAR8
);
4193 CharSize
= sizeof(CHAR16
);
4195 for (CountSoFar
= 0;;CountSoFar
++){
4197 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4198 if ( EFI_ERROR(Status
)
4200 || (CharBuffer
== L
'\n' && !(*Ascii
))
4201 || (CharBuffer
== '\n' && *Ascii
)
4203 if (CharSize
== 0) {
4204 Status
= EFI_END_OF_FILE
;
4209 // if we have space save it...
4211 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4212 ASSERT(Buffer
!= NULL
);
4213 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4214 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4219 // if we ran out of space tell when...
4221 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4222 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4224 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4226 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4228 return (EFI_BUFFER_TOO_SMALL
);
4230 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4231 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4238 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4240 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4241 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4242 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4243 the help content only.
4244 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4245 @retval EFI_NOT_FOUND The help data could not be found.
4246 @retval EFI_SUCCESS The operation was successful.
4251 IN CONST CHAR16
*CommandToGetHelpOn
,
4252 IN CONST CHAR16
*SectionToGetHelpOn
,
4253 IN BOOLEAN PrintCommandText
4262 // Get the string to print based
4264 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4267 // make sure we got a valid string
4269 if (EFI_ERROR(Status
)){
4272 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4273 return EFI_NOT_FOUND
;
4277 // Chop off trailing stuff we dont need
4279 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4280 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4284 // Print this out to the console
4286 if (PrintCommandText
) {
4287 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4289 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4292 SHELL_FREE_NON_NULL(OutText
);
4298 Function to delete a file by name
4300 @param[in] FileName Pointer to file name to delete.
4302 @retval EFI_SUCCESS the file was deleted sucessfully
4303 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4305 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4306 @retval EFI_NOT_FOUND The specified file could not be found on the
4307 device or the file system could not be found
4309 @retval EFI_NO_MEDIA The device has no medium.
4310 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4311 medium is no longer supported.
4312 @retval EFI_DEVICE_ERROR The device reported an error.
4313 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4314 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4315 @retval EFI_ACCESS_DENIED The file was opened read only.
4316 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4318 @retval other The file failed to open
4322 ShellDeleteFileByName(
4323 IN CONST CHAR16
*FileName
4327 SHELL_FILE_HANDLE FileHandle
;
4329 Status
= ShellFileExists(FileName
);
4331 if (Status
== EFI_SUCCESS
){
4332 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4333 if (Status
== EFI_SUCCESS
){
4334 Status
= ShellDeleteFile(&FileHandle
);
4343 Cleans off all the quotes in the string.
4345 @param[in] OriginalString pointer to the string to be cleaned.
4346 @param[out] CleanString The new string with all quotes removed.
4347 Memory allocated in the function and free
4350 @retval EFI_SUCCESS The operation was successful.
4354 InternalShellStripQuotes (
4355 IN CONST CHAR16
*OriginalString
,
4356 OUT CHAR16
**CleanString
4361 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4362 return EFI_INVALID_PARAMETER
;
4365 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4366 if (*CleanString
== NULL
) {
4367 return EFI_OUT_OF_RESOURCES
;
4370 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4371 if (*Walker
== L
'\"') {
4372 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));