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 - 2017, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "UefiShellLib.h"
18 #include <Library/SortLib.h>
19 #include <Library/BaseLib.h>
24 SHELL_PARAM_ITEM EmptyParamList
[] = {
27 SHELL_PARAM_ITEM SfoParamList
[] = {
31 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
32 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
33 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
34 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
35 EFI_HANDLE mEfiShellEnvironment2Handle
;
36 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
37 EFI_UNICODE_COLLATION_PROTOCOL
*mUnicodeCollationProtocol
;
40 Check if a Unicode character is a hexadecimal character.
42 This internal function checks if a Unicode character is a
43 numeric character. The valid hexadecimal characters are
44 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
46 @param Char The character to check against.
48 @retval TRUE If the Char is a hexadecmial character.
49 @retval FALSE If the Char is not a hexadecmial character.
54 ShellIsHexaDecimalDigitCharacter (
58 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
62 Check if a Unicode character is a decimal character.
64 This internal function checks if a Unicode character is a
65 decimal character. The valid characters are
69 @param Char The character to check against.
71 @retval TRUE If the Char is a hexadecmial character.
72 @retval FALSE If the Char is not a hexadecmial character.
77 ShellIsDecimalDigitCharacter (
81 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
85 Helper function to find ShellEnvironment2 for constructor.
87 @param[in] ImageHandle A copy of the calling image's handle.
89 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
93 IN EFI_HANDLE ImageHandle
103 Status
= gBS
->OpenProtocol(ImageHandle
,
104 &gEfiShellEnvironment2Guid
,
105 (VOID
**)&mEfiShellEnvironment2
,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
111 // look for the mEfiShellEnvironment2 protocol at a higher level
113 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
115 // figure out how big of a buffer we need.
117 Status
= gBS
->LocateHandle (ByProtocol
,
118 &gEfiShellEnvironment2Guid
,
119 NULL
, // ignored for ByProtocol
124 // maybe it's not there???
126 if (Status
== EFI_BUFFER_TOO_SMALL
) {
127 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
128 if (Buffer
== NULL
) {
129 return (EFI_OUT_OF_RESOURCES
);
131 Status
= gBS
->LocateHandle (ByProtocol
,
132 &gEfiShellEnvironment2Guid
,
133 NULL
, // ignored for ByProtocol
138 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
140 // now parse the list of returned handles
142 Status
= EFI_NOT_FOUND
;
143 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
144 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
145 &gEfiShellEnvironment2Guid
,
146 (VOID
**)&mEfiShellEnvironment2
,
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL
151 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
152 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
153 Status
= EFI_SUCCESS
;
159 if (Buffer
!= NULL
) {
166 Function to do most of the work of the constructor. Allows for calling
167 multiple times without complete re-initialization.
169 @param[in] ImageHandle A copy of the ImageHandle.
170 @param[in] SystemTable A pointer to the SystemTable for the application.
172 @retval EFI_SUCCESS The operationw as successful.
175 ShellLibConstructorWorker (
176 IN EFI_HANDLE ImageHandle
,
177 IN EFI_SYSTEM_TABLE
*SystemTable
183 // UEFI 2.0 shell interfaces (used preferentially)
185 Status
= gBS
->OpenProtocol(
187 &gEfiShellProtocolGuid
,
188 (VOID
**)&gEfiShellProtocol
,
191 EFI_OPEN_PROTOCOL_GET_PROTOCOL
193 if (EFI_ERROR(Status
)) {
195 // Search for the shell protocol
197 Status
= gBS
->LocateProtocol(
198 &gEfiShellProtocolGuid
,
200 (VOID
**)&gEfiShellProtocol
202 if (EFI_ERROR(Status
)) {
203 gEfiShellProtocol
= NULL
;
206 Status
= gBS
->OpenProtocol(
208 &gEfiShellParametersProtocolGuid
,
209 (VOID
**)&gEfiShellParametersProtocol
,
212 EFI_OPEN_PROTOCOL_GET_PROTOCOL
214 if (EFI_ERROR(Status
)) {
215 gEfiShellParametersProtocol
= NULL
;
218 if (gEfiShellParametersProtocol
== NULL
|| gEfiShellProtocol
== NULL
) {
220 // Moved to seperate function due to complexity
222 Status
= ShellFindSE2(ImageHandle
);
224 if (EFI_ERROR(Status
)) {
225 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
226 mEfiShellEnvironment2
= NULL
;
228 Status
= gBS
->OpenProtocol(ImageHandle
,
229 &gEfiShellInterfaceGuid
,
230 (VOID
**)&mEfiShellInterface
,
233 EFI_OPEN_PROTOCOL_GET_PROTOCOL
235 if (EFI_ERROR(Status
)) {
236 mEfiShellInterface
= NULL
;
241 // only success getting 2 of either the old or new, but no 1/2 and 1/2
243 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
244 (gEfiShellProtocol
!= NULL
&& gEfiShellParametersProtocol
!= NULL
) ) {
245 if (gEfiShellProtocol
!= NULL
) {
246 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
247 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
248 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
249 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
250 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
251 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
252 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
253 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
254 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
255 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
257 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
258 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
259 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
260 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
261 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
262 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
263 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
264 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
265 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
266 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
268 return (EFI_SUCCESS
);
270 return (EFI_NOT_FOUND
);
273 Constructor for the Shell library.
275 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
277 @param ImageHandle the image handle of the process
278 @param SystemTable the EFI System Table pointer
280 @retval EFI_SUCCESS the initialization was complete sucessfully
281 @return others an error ocurred during initialization
285 ShellLibConstructor (
286 IN EFI_HANDLE ImageHandle
,
287 IN EFI_SYSTEM_TABLE
*SystemTable
290 mEfiShellEnvironment2
= NULL
;
291 gEfiShellProtocol
= NULL
;
292 gEfiShellParametersProtocol
= NULL
;
293 mEfiShellInterface
= NULL
;
294 mEfiShellEnvironment2Handle
= NULL
;
295 mUnicodeCollationProtocol
= NULL
;
298 // verify that auto initialize is not set false
300 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
301 return (EFI_SUCCESS
);
304 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
308 Destructor for the library. free any resources.
310 @param[in] ImageHandle A copy of the ImageHandle.
311 @param[in] SystemTable A pointer to the SystemTable for the application.
313 @retval EFI_SUCCESS The operation was successful.
314 @return An error from the CloseProtocol function.
319 IN EFI_HANDLE ImageHandle
,
320 IN EFI_SYSTEM_TABLE
*SystemTable
323 if (mEfiShellEnvironment2
!= NULL
) {
324 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
325 &gEfiShellEnvironment2Guid
,
328 mEfiShellEnvironment2
= NULL
;
330 if (mEfiShellInterface
!= NULL
) {
331 gBS
->CloseProtocol(ImageHandle
,
332 &gEfiShellInterfaceGuid
,
335 mEfiShellInterface
= NULL
;
337 if (gEfiShellProtocol
!= NULL
) {
338 gBS
->CloseProtocol(ImageHandle
,
339 &gEfiShellProtocolGuid
,
342 gEfiShellProtocol
= NULL
;
344 if (gEfiShellParametersProtocol
!= NULL
) {
345 gBS
->CloseProtocol(ImageHandle
,
346 &gEfiShellParametersProtocolGuid
,
349 gEfiShellParametersProtocol
= NULL
;
351 mEfiShellEnvironment2Handle
= NULL
;
353 return (EFI_SUCCESS
);
357 This function causes the shell library to initialize itself. If the shell library
358 is already initialized it will de-initialize all the current protocol poitners and
359 re-populate them again.
361 When the library is used with PcdShellLibAutoInitialize set to true this function
362 will return EFI_SUCCESS and perform no actions.
364 This function is intended for internal access for shell commands only.
366 @retval EFI_SUCCESS the initialization was complete sucessfully
377 // if auto initialize is not false then skip
379 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
380 return (EFI_SUCCESS
);
384 // deinit the current stuff
386 Status
= ShellLibDestructor (gImageHandle
, gST
);
387 ASSERT_EFI_ERROR (Status
);
390 // init the new stuff
392 return (ShellLibConstructorWorker(gImageHandle
, gST
));
396 This function will retrieve the information about the file for the handle
397 specified and store it in allocated pool memory.
399 This function allocates a buffer to store the file's information. It is the
400 caller's responsibility to free the buffer
402 @param FileHandle The file handle of the file for which information is
405 @retval NULL information could not be retrieved.
407 @return the information about the file
412 IN SHELL_FILE_HANDLE FileHandle
415 return (FileFunctionMap
.GetFileInfo(FileHandle
));
419 This function sets the information about the file for the opened handle
422 @param[in] FileHandle The file handle of the file for which information
425 @param[in] FileInfo The information to set.
427 @retval EFI_SUCCESS The information was set.
428 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
429 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
430 @retval EFI_NO_MEDIA The device has no medium.
431 @retval EFI_DEVICE_ERROR The device reported an error.
432 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
433 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
434 @retval EFI_ACCESS_DENIED The file was opened read only.
435 @retval EFI_VOLUME_FULL The volume is full.
440 IN SHELL_FILE_HANDLE FileHandle
,
441 IN EFI_FILE_INFO
*FileInfo
444 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
448 This function will open a file or directory referenced by DevicePath.
450 This function opens a file with the open mode according to the file path. The
451 Attributes is valid only for EFI_FILE_MODE_CREATE.
453 @param FilePath on input the device path to the file. On output
454 the remaining device path.
455 @param DeviceHandle pointer to the system device handle.
456 @param FileHandle pointer to the file handle.
457 @param OpenMode the mode to open the file with.
458 @param Attributes the file's file attributes.
460 @retval EFI_SUCCESS The information was set.
461 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
462 @retval EFI_UNSUPPORTED Could not open the file path.
463 @retval EFI_NOT_FOUND The specified file could not be found on the
464 device or the file system could not be found on
466 @retval EFI_NO_MEDIA The device has no medium.
467 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
468 medium is no longer supported.
469 @retval EFI_DEVICE_ERROR The device reported an error.
470 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
471 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
472 @retval EFI_ACCESS_DENIED The file was opened read only.
473 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
475 @retval EFI_VOLUME_FULL The volume is full.
479 ShellOpenFileByDevicePath(
480 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
481 OUT EFI_HANDLE
*DeviceHandle
,
482 OUT SHELL_FILE_HANDLE
*FileHandle
,
489 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
490 EFI_FILE_PROTOCOL
*Handle1
;
491 EFI_FILE_PROTOCOL
*Handle2
;
492 CHAR16
*FnafPathName
;
495 if (FilePath
== NULL
|| FileHandle
== NULL
|| DeviceHandle
== NULL
) {
496 return (EFI_INVALID_PARAMETER
);
500 // which shell interface should we use
502 if (gEfiShellProtocol
!= NULL
) {
504 // use UEFI Shell 2.0 method.
506 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
507 if (FileName
== NULL
) {
508 return (EFI_INVALID_PARAMETER
);
510 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
517 // use old shell method.
519 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
522 if (EFI_ERROR (Status
)) {
525 Status
= gBS
->OpenProtocol(*DeviceHandle
,
526 &gEfiSimpleFileSystemProtocolGuid
,
527 (VOID
**)&EfiSimpleFileSystemProtocol
,
530 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
531 if (EFI_ERROR (Status
)) {
534 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
535 if (EFI_ERROR (Status
)) {
541 // go down directories one node at a time.
543 while (!IsDevicePathEnd (*FilePath
)) {
545 // For file system access each node should be a file path component
547 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
548 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
551 return (EFI_INVALID_PARAMETER
);
554 // Open this file path node
560 // File Name Alignment Fix (FNAF)
561 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
562 // The structure pointed to by FilePath may be not CHAR16 aligned.
563 // This code copies the potentially unaligned PathName data from the
564 // FilePath structure to the aligned FnafPathName for use in the
565 // calls to Handl2->Open.
569 // Determine length of PathName, in bytes.
571 PathLen
= DevicePathNodeLength (*FilePath
) - SIZE_OF_FILEPATH_DEVICE_PATH
;
574 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
575 // Copy bytes from possibly unaligned location to aligned location
577 FnafPathName
= AllocateCopyPool(PathLen
, (UINT8
*)((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
);
578 if (FnafPathName
== NULL
) {
579 return EFI_OUT_OF_RESOURCES
;
583 // Try to test opening an existing file
585 Status
= Handle2
->Open (
589 OpenMode
&~EFI_FILE_MODE_CREATE
,
594 // see if the error was that it needs to be created
596 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
597 Status
= Handle2
->Open (
607 // Free the alignment buffer
609 FreePool(FnafPathName
);
612 // Close the last node
614 Handle2
->Close (Handle2
);
616 if (EFI_ERROR(Status
)) {
623 *FilePath
= NextDevicePathNode (*FilePath
);
627 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
629 *FileHandle
= (VOID
*)Handle1
;
630 return (EFI_SUCCESS
);
634 This function will open a file or directory referenced by filename.
636 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
637 otherwise, the Filehandle is NULL. The Attributes is valid only for
638 EFI_FILE_MODE_CREATE.
640 if FileName is NULL then ASSERT()
642 @param FileName pointer to file name
643 @param FileHandle pointer to the file handle.
644 @param OpenMode the mode to open the file with.
645 @param Attributes the file's file attributes.
647 @retval EFI_SUCCESS The information was set.
648 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
649 @retval EFI_UNSUPPORTED Could not open the file path.
650 @retval EFI_NOT_FOUND The specified file could not be found on the
651 device or the file system could not be found
653 @retval EFI_NO_MEDIA The device has no medium.
654 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
655 medium is no longer supported.
656 @retval EFI_DEVICE_ERROR The device reported an error.
657 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
658 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
659 @retval EFI_ACCESS_DENIED The file was opened read only.
660 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
662 @retval EFI_VOLUME_FULL The volume is full.
667 IN CONST CHAR16
*FileName
,
668 OUT SHELL_FILE_HANDLE
*FileHandle
,
673 EFI_HANDLE DeviceHandle
;
674 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
676 EFI_FILE_INFO
*FileInfo
;
677 CHAR16
*FileNameCopy
;
681 // ASSERT if FileName is NULL
683 ASSERT(FileName
!= NULL
);
685 if (FileName
== NULL
) {
686 return (EFI_INVALID_PARAMETER
);
689 if (gEfiShellProtocol
!= NULL
) {
690 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
693 // Create only a directory
695 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
696 return ShellCreateDirectory(FileName
, FileHandle
);
700 // Create the directory to create the file in
702 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
703 if (FileNameCopy
== NULL
) {
704 return (EFI_OUT_OF_RESOURCES
);
706 PathCleanUpDirectories (FileNameCopy
);
707 if (PathRemoveLastItem (FileNameCopy
)) {
708 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy
, FileHandle
))) {
709 ShellCloseFile (FileHandle
);
712 SHELL_FREE_NON_NULL (FileNameCopy
);
716 // Use UEFI Shell 2.0 method to create the file
718 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
721 if (EFI_ERROR(Status
)) {
725 if (mUnicodeCollationProtocol
== NULL
) {
726 Status
= gBS
->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&mUnicodeCollationProtocol
);
727 if (EFI_ERROR (Status
)) {
728 gEfiShellProtocol
->CloseFile (*FileHandle
);
733 if ((mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NUL") != 0) &&
734 (mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NULL") != 0) &&
735 !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
736 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
737 ASSERT(FileInfo
!= NULL
);
738 FileInfo
->Attribute
= Attributes
;
739 Status2
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
741 if (EFI_ERROR (Status2
)) {
742 gEfiShellProtocol
->CloseFile(*FileHandle
);
749 // Using EFI Shell version
750 // this means convert name to path and call that function
751 // since this will use EFI method again that will open it.
753 ASSERT(mEfiShellEnvironment2
!= NULL
);
754 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
755 if (FilePath
!= NULL
) {
756 return (ShellOpenFileByDevicePath(&FilePath
,
762 return (EFI_DEVICE_ERROR
);
765 This function create a directory
767 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
768 otherwise, the Filehandle is NULL. If the directory already existed, this
769 function opens the existing directory.
771 @param DirectoryName pointer to directory name
772 @param FileHandle pointer to the file handle.
774 @retval EFI_SUCCESS The information was set.
775 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
776 @retval EFI_UNSUPPORTED Could not open the file path.
777 @retval EFI_NOT_FOUND The specified file could not be found on the
778 device or the file system could not be found
780 @retval EFI_NO_MEDIA The device has no medium.
781 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
782 medium is no longer supported.
783 @retval EFI_DEVICE_ERROR The device reported an error.
784 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
785 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
786 @retval EFI_ACCESS_DENIED The file was opened read only.
787 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
789 @retval EFI_VOLUME_FULL The volume is full.
790 @sa ShellOpenFileByName
794 ShellCreateDirectory(
795 IN CONST CHAR16
*DirectoryName
,
796 OUT SHELL_FILE_HANDLE
*FileHandle
799 if (gEfiShellProtocol
!= NULL
) {
801 // Use UEFI Shell 2.0 method
803 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
808 return (ShellOpenFileByName(DirectoryName
,
810 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
817 This function reads information from an opened file.
819 If FileHandle is not a directory, the function reads the requested number of
820 bytes from the file at the file's current position and returns them in Buffer.
821 If the read goes beyond the end of the file, the read length is truncated to the
822 end of the file. The file's current position is increased by the number of bytes
823 returned. If FileHandle is a directory, the function reads the directory entry
824 at the file's current position and returns the entry in Buffer. If the Buffer
825 is not large enough to hold the current directory entry, then
826 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
827 BufferSize is set to be the size of the buffer needed to read the entry. On
828 success, the current position is updated to the next directory entry. If there
829 are no more directory entries, the read returns a zero-length buffer.
830 EFI_FILE_INFO is the structure returned as the directory entry.
832 @param FileHandle the opened file handle
833 @param BufferSize on input the size of buffer in bytes. on return
834 the number of bytes written.
835 @param Buffer the buffer to put read data into.
837 @retval EFI_SUCCESS Data was read.
838 @retval EFI_NO_MEDIA The device has no media.
839 @retval EFI_DEVICE_ERROR The device reported an error.
840 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
841 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
848 IN SHELL_FILE_HANDLE FileHandle
,
849 IN OUT UINTN
*BufferSize
,
853 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
858 Write data to a file.
860 This function writes the specified number of bytes to the file at the current
861 file position. The current file position is advanced the actual number of bytes
862 written, which is returned in BufferSize. Partial writes only occur when there
863 has been a data error during the write attempt (such as "volume space full").
864 The file is automatically grown to hold the data if required. Direct writes to
865 opened directories are not supported.
867 @param FileHandle The opened file for writing
868 @param BufferSize on input the number of bytes in Buffer. On output
869 the number of bytes written.
870 @param Buffer the buffer containing data to write is stored.
872 @retval EFI_SUCCESS Data was written.
873 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
874 @retval EFI_NO_MEDIA The device has no media.
875 @retval EFI_DEVICE_ERROR The device reported an error.
876 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
877 @retval EFI_WRITE_PROTECTED The device is write-protected.
878 @retval EFI_ACCESS_DENIED The file was open for read only.
879 @retval EFI_VOLUME_FULL The volume is full.
884 IN SHELL_FILE_HANDLE FileHandle
,
885 IN OUT UINTN
*BufferSize
,
889 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
893 Close an open file handle.
895 This function closes a specified file handle. All "dirty" cached file data is
896 flushed to the device, and the file is closed. In all cases the handle is
899 @param FileHandle the file handle to close.
901 @retval EFI_SUCCESS the file handle was closed sucessfully.
906 IN SHELL_FILE_HANDLE
*FileHandle
909 return (FileFunctionMap
.CloseFile(*FileHandle
));
913 Delete a file and close the handle
915 This function closes and deletes a file. In all cases the file handle is closed.
916 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
917 returned, but the handle is still closed.
919 @param FileHandle the file handle to delete
921 @retval EFI_SUCCESS the file was closed sucessfully
922 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
924 @retval INVALID_PARAMETER One of the parameters has an invalid value.
929 IN SHELL_FILE_HANDLE
*FileHandle
932 return (FileFunctionMap
.DeleteFile(*FileHandle
));
936 Set the current position in a file.
938 This function sets the current file position for the handle to the position
939 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
940 absolute positioning is supported, and seeking past the end of the file is
941 allowed (a subsequent write would grow the file). Seeking to position
942 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
943 If FileHandle is a directory, the only position that may be set is zero. This
944 has the effect of starting the read process of the directory entries over.
946 @param FileHandle The file handle on which the position is being set
947 @param Position Byte position from begining of file
949 @retval EFI_SUCCESS Operation completed sucessfully.
950 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
952 @retval INVALID_PARAMETER One of the parameters has an invalid value.
956 ShellSetFilePosition (
957 IN SHELL_FILE_HANDLE FileHandle
,
961 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
965 Gets a file's current position
967 This function retrieves the current file position for the file handle. For
968 directories, the current file position has no meaning outside of the file
969 system driver and as such the operation is not supported. An error is returned
970 if FileHandle is a directory.
972 @param FileHandle The open file handle on which to get the position.
973 @param Position Byte position from begining of file.
975 @retval EFI_SUCCESS the operation completed sucessfully.
976 @retval INVALID_PARAMETER One of the parameters has an invalid value.
977 @retval EFI_UNSUPPORTED the request is not valid on directories.
981 ShellGetFilePosition (
982 IN SHELL_FILE_HANDLE FileHandle
,
986 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
989 Flushes data on a file
991 This function flushes all modified data associated with a file to a device.
993 @param FileHandle The file handle on which to flush data
995 @retval EFI_SUCCESS The data was flushed.
996 @retval EFI_NO_MEDIA The device has no media.
997 @retval EFI_DEVICE_ERROR The device reported an error.
998 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
999 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1000 @retval EFI_ACCESS_DENIED The file was opened for read only.
1005 IN SHELL_FILE_HANDLE FileHandle
1008 return (FileFunctionMap
.FlushFile(FileHandle
));
1011 /** Retrieve first entry from a directory.
1013 This function takes an open directory handle and gets information from the
1014 first entry in the directory. A buffer is allocated to contain
1015 the information and a pointer to the buffer is returned in *Buffer. The
1016 caller can use ShellFindNextFile() to get subsequent directory entries.
1018 The buffer will be freed by ShellFindNextFile() when the last directory
1019 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1020 when finished with it.
1022 @param[in] DirHandle The file handle of the directory to search.
1023 @param[out] Buffer The pointer to the buffer for the file's information.
1025 @retval EFI_SUCCESS Found the first file.
1026 @retval EFI_NOT_FOUND Cannot find the directory.
1027 @retval EFI_NO_MEDIA The device has no media.
1028 @retval EFI_DEVICE_ERROR The device reported an error.
1029 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1030 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1035 ShellFindFirstFile (
1036 IN SHELL_FILE_HANDLE DirHandle
,
1037 OUT EFI_FILE_INFO
**Buffer
1041 // pass to file handle lib
1043 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1045 /** Retrieve next entries from a directory.
1047 To use this function, the caller must first call the ShellFindFirstFile()
1048 function to get the first directory entry. Subsequent directory entries are
1049 retrieved by using the ShellFindNextFile() function. This function can
1050 be called several times to get each entry from the directory. If the call of
1051 ShellFindNextFile() retrieved the last directory entry, the next call of
1052 this function will set *NoFile to TRUE and free the buffer.
1054 @param[in] DirHandle The file handle of the directory.
1055 @param[out] Buffer The pointer to buffer for file's information.
1056 @param[out] NoFile The pointer to boolean when last file is found.
1058 @retval EFI_SUCCESS Found the next file, or reached last file
1059 @retval EFI_NO_MEDIA The device has no media.
1060 @retval EFI_DEVICE_ERROR The device reported an error.
1061 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1066 IN SHELL_FILE_HANDLE DirHandle
,
1067 OUT EFI_FILE_INFO
*Buffer
,
1072 // pass to file handle lib
1074 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1077 Retrieve the size of a file.
1079 if FileHandle is NULL then ASSERT()
1080 if Size is NULL then ASSERT()
1082 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1085 @param FileHandle file handle from which size is retrieved
1086 @param Size pointer to size
1088 @retval EFI_SUCCESS operation was completed sucessfully
1089 @retval EFI_DEVICE_ERROR cannot access the file
1094 IN SHELL_FILE_HANDLE FileHandle
,
1098 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1101 Retrieves the status of the break execution flag
1103 this function is useful to check whether the application is being asked to halt by the shell.
1105 @retval TRUE the execution break is enabled
1106 @retval FALSE the execution break is not enabled
1110 ShellGetExecutionBreakFlag(
1115 // Check for UEFI Shell 2.0 protocols
1117 if (gEfiShellProtocol
!= NULL
) {
1120 // We are using UEFI Shell 2.0; see if the event has been triggered
1122 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1129 // using EFI Shell; call the function to check
1131 if (mEfiShellEnvironment2
!= NULL
) {
1132 return (mEfiShellEnvironment2
->GetExecutionBreak());
1138 return the value of an environment variable
1140 this function gets the value of the environment variable set by the
1141 ShellSetEnvironmentVariable function
1143 @param EnvKey The key name of the environment variable.
1145 @retval NULL the named environment variable does not exist.
1146 @return != NULL pointer to the value of the environment variable
1150 ShellGetEnvironmentVariable (
1151 IN CONST CHAR16
*EnvKey
1155 // Check for UEFI Shell 2.0 protocols
1157 if (gEfiShellProtocol
!= NULL
) {
1158 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1162 // Check for EFI shell
1164 if (mEfiShellEnvironment2
!= NULL
) {
1165 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1171 set the value of an environment variable
1173 This function changes the current value of the specified environment variable. If the
1174 environment variable exists and the Value is an empty string, then the environment
1175 variable is deleted. If the environment variable exists and the Value is not an empty
1176 string, then the value of the environment variable is changed. If the environment
1177 variable does not exist and the Value is an empty string, there is no action. If the
1178 environment variable does not exist and the Value is a non-empty string, then the
1179 environment variable is created and assigned the specified value.
1181 This is not supported pre-UEFI Shell 2.0.
1183 @param EnvKey The key name of the environment variable.
1184 @param EnvVal The Value of the environment variable
1185 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1187 @retval EFI_SUCCESS the operation was completed sucessfully
1188 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1192 ShellSetEnvironmentVariable (
1193 IN CONST CHAR16
*EnvKey
,
1194 IN CONST CHAR16
*EnvVal
,
1199 // Check for UEFI Shell 2.0 protocols
1201 if (gEfiShellProtocol
!= NULL
) {
1202 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1206 // This feature does not exist under EFI shell
1208 return (EFI_UNSUPPORTED
);
1212 Cause the shell to parse and execute a command line.
1214 This function creates a nested instance of the shell and executes the specified
1215 command (CommandLine) with the specified environment (Environment). Upon return,
1216 the status code returned by the specified command is placed in StatusCode.
1217 If Environment is NULL, then the current environment is used and all changes made
1218 by the commands executed will be reflected in the current environment. If the
1219 Environment is non-NULL, then the changes made will be discarded.
1220 The CommandLine is executed from the current working directory on the current
1223 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1224 environment. The values pointed to by the parameters will be unchanged by the
1225 ShellExecute() function. The Output parameter has no effect in a
1226 UEFI Shell 2.0 environment.
1228 @param[in] ParentHandle The parent image starting the operation.
1229 @param[in] CommandLine The pointer to a NULL terminated command line.
1230 @param[in] Output True to display debug output. False to hide it.
1231 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1232 in the form "x=y". If NULL, the current set is used.
1233 @param[out] Status The status of the run command line.
1235 @retval EFI_SUCCESS The operation completed sucessfully. Status
1236 contains the status code returned.
1237 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1238 @retval EFI_OUT_OF_RESOURCES Out of resources.
1239 @retval EFI_UNSUPPORTED The operation is not allowed.
1244 IN EFI_HANDLE
*ParentHandle
,
1245 IN CHAR16
*CommandLine OPTIONAL
,
1246 IN BOOLEAN Output OPTIONAL
,
1247 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1248 OUT EFI_STATUS
*Status OPTIONAL
1251 EFI_STATUS CmdStatus
;
1253 // Check for UEFI Shell 2.0 protocols
1255 if (gEfiShellProtocol
!= NULL
) {
1257 // Call UEFI Shell 2.0 version (not using Output parameter)
1259 return (gEfiShellProtocol
->Execute(ParentHandle
,
1261 EnvironmentVariables
,
1266 // Check for EFI shell
1268 if (mEfiShellEnvironment2
!= NULL
) {
1270 // Call EFI Shell version.
1271 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1273 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1277 // No Status output parameter so just use the returned status
1279 if (Status
!= NULL
) {
1280 *Status
= CmdStatus
;
1283 // If there was an error, we can't tell if it was from the command or from
1284 // the Execute() function, so we'll just assume the shell ran successfully
1285 // and the error came from the command.
1290 return (EFI_UNSUPPORTED
);
1294 Retreives the current directory path
1296 If the DeviceName is NULL, it returns the current device's current directory
1297 name. If the DeviceName is not NULL, it returns the current directory name
1300 Note that the current directory string should exclude the tailing backslash character.
1302 @param DeviceName the name of the drive to get directory on
1304 @retval NULL the directory does not exist
1305 @return != NULL the directory
1309 ShellGetCurrentDir (
1310 IN CHAR16
* CONST DeviceName OPTIONAL
1314 // Check for UEFI Shell 2.0 protocols
1316 if (gEfiShellProtocol
!= NULL
) {
1317 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1321 // Check for EFI shell
1323 if (mEfiShellEnvironment2
!= NULL
) {
1324 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1330 sets (enabled or disabled) the page break mode
1332 when page break mode is enabled the screen will stop scrolling
1333 and wait for operator input before scrolling a subsequent screen.
1335 @param CurrentState TRUE to enable and FALSE to disable
1339 ShellSetPageBreakMode (
1340 IN BOOLEAN CurrentState
1344 // check for enabling
1346 if (CurrentState
!= 0x00) {
1348 // check for UEFI Shell 2.0
1350 if (gEfiShellProtocol
!= NULL
) {
1352 // Enable with UEFI 2.0 Shell
1354 gEfiShellProtocol
->EnablePageBreak();
1358 // Check for EFI shell
1360 if (mEfiShellEnvironment2
!= NULL
) {
1362 // Enable with EFI Shell
1364 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1370 // check for UEFI Shell 2.0
1372 if (gEfiShellProtocol
!= NULL
) {
1374 // Disable with UEFI 2.0 Shell
1376 gEfiShellProtocol
->DisablePageBreak();
1380 // Check for EFI shell
1382 if (mEfiShellEnvironment2
!= NULL
) {
1384 // Disable with EFI Shell
1386 mEfiShellEnvironment2
->DisablePageBreak ();
1394 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1395 /// This allows for the struct to be populated.
1402 SHELL_FILE_HANDLE Handle
;
1403 EFI_FILE_INFO
*Info
;
1404 } EFI_SHELL_FILE_INFO_NO_CONST
;
1407 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1409 if OldStyleFileList is NULL then ASSERT()
1411 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1412 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1413 the ShellCloseFileMetaArg function.
1415 @param[in] FileList the EFI shell list type
1416 @param[in, out] ListHead the list to add to
1418 @retval the resultant head of the double linked new format list;
1421 InternalShellConvertFileListType (
1422 IN LIST_ENTRY
*FileList
,
1423 IN OUT LIST_ENTRY
*ListHead
1426 SHELL_FILE_ARG
*OldInfo
;
1428 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1433 ASSERT(FileList
!= NULL
);
1434 ASSERT(ListHead
!= NULL
);
1437 // enumerate through each member of the old list and copy
1439 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1440 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1441 ASSERT(OldInfo
!= NULL
);
1444 // Skip ones that failed to open...
1446 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1451 // make sure the old list was valid
1453 ASSERT(OldInfo
->Info
!= NULL
);
1454 ASSERT(OldInfo
->FullName
!= NULL
);
1455 ASSERT(OldInfo
->FileName
!= NULL
);
1458 // allocate a new EFI_SHELL_FILE_INFO object
1460 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1461 if (NewInfo
== NULL
) {
1462 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1468 // copy the simple items
1470 NewInfo
->Handle
= OldInfo
->Handle
;
1471 NewInfo
->Status
= OldInfo
->Status
;
1473 // old shell checks for 0 not NULL
1474 OldInfo
->Handle
= 0;
1477 // allocate new space to copy strings and structure
1479 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1480 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1481 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1484 // make sure all the memory allocations were sucessful
1486 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1488 // Free the partially allocated new node
1490 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1491 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1492 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1493 SHELL_FREE_NON_NULL(NewInfo
);
1496 // Free the previously converted stuff
1498 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1504 // add that to the list
1506 InsertTailList(ListHead
, &NewInfo
->Link
);
1511 Opens a group of files based on a path.
1513 This function uses the Arg to open all the matching files. Each matched
1514 file has a SHELL_FILE_INFO structure to record the file information. These
1515 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1516 structures from ListHead to access each file. This function supports wildcards
1517 and will process '?' and '*' as such. the list must be freed with a call to
1518 ShellCloseFileMetaArg().
1520 If you are NOT appending to an existing list *ListHead must be NULL. If
1521 *ListHead is NULL then it must be callee freed.
1523 @param Arg pointer to path string
1524 @param OpenMode mode to open files with
1525 @param ListHead head of linked list of results
1527 @retval EFI_SUCCESS the operation was sucessful and the list head
1528 contains the list of opened files
1529 @return != EFI_SUCCESS the operation failed
1531 @sa InternalShellConvertFileListType
1535 ShellOpenFileMetaArg (
1538 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1542 LIST_ENTRY mOldStyleFileList
;
1543 CHAR16
*CleanFilePathStr
;
1546 // ASSERT that Arg and ListHead are not NULL
1548 ASSERT(Arg
!= NULL
);
1549 ASSERT(ListHead
!= NULL
);
1551 CleanFilePathStr
= NULL
;
1553 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1554 if (EFI_ERROR (Status
)) {
1559 // Check for UEFI Shell 2.0 protocols
1561 if (gEfiShellProtocol
!= NULL
) {
1562 if (*ListHead
== NULL
) {
1563 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1564 if (*ListHead
== NULL
) {
1565 FreePool(CleanFilePathStr
);
1566 return (EFI_OUT_OF_RESOURCES
);
1568 InitializeListHead(&((*ListHead
)->Link
));
1570 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1573 if (EFI_ERROR(Status
)) {
1574 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1576 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1578 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1579 FreePool(*ListHead
);
1580 FreePool(CleanFilePathStr
);
1582 return (EFI_NOT_FOUND
);
1584 FreePool(CleanFilePathStr
);
1589 // Check for EFI shell
1591 if (mEfiShellEnvironment2
!= NULL
) {
1593 // make sure the list head is initialized
1595 InitializeListHead(&mOldStyleFileList
);
1598 // Get the EFI Shell list of files
1600 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1601 if (EFI_ERROR(Status
)) {
1603 FreePool(CleanFilePathStr
);
1607 if (*ListHead
== NULL
) {
1608 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1609 if (*ListHead
== NULL
) {
1610 FreePool(CleanFilePathStr
);
1611 return (EFI_OUT_OF_RESOURCES
);
1613 InitializeListHead(&((*ListHead
)->Link
));
1617 // Convert that to equivalent of UEFI Shell 2.0 structure
1619 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1622 // Free the EFI Shell version that was converted.
1624 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1626 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1627 FreePool(*ListHead
);
1629 Status
= EFI_NOT_FOUND
;
1631 FreePool(CleanFilePathStr
);
1635 FreePool(CleanFilePathStr
);
1636 return (EFI_UNSUPPORTED
);
1639 Free the linked list returned from ShellOpenFileMetaArg.
1641 if ListHead is NULL then ASSERT().
1643 @param ListHead the pointer to free.
1645 @retval EFI_SUCCESS the operation was sucessful.
1649 ShellCloseFileMetaArg (
1650 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1656 // ASSERT that ListHead is not NULL
1658 ASSERT(ListHead
!= NULL
);
1661 // Check for UEFI Shell 2.0 protocols
1663 if (gEfiShellProtocol
!= NULL
) {
1664 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1665 } else if (mEfiShellEnvironment2
!= NULL
) {
1667 // Since this is EFI Shell version we need to free our internally made copy
1670 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1671 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1672 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1673 RemoveEntryList(Node
);
1674 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1675 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1676 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1677 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1678 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1680 SHELL_FREE_NON_NULL(*ListHead
);
1684 return (EFI_UNSUPPORTED
);
1688 Find a file by searching the CWD and then the path.
1690 If FileName is NULL then ASSERT.
1692 If the return value is not NULL then the memory must be caller freed.
1694 @param FileName Filename string.
1696 @retval NULL the file was not found
1697 @return !NULL the full path to the file.
1702 IN CONST CHAR16
*FileName
1706 SHELL_FILE_HANDLE Handle
;
1710 CONST CHAR16
*Walker
;
1717 // First make sure its not an absolute path.
1719 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1720 if (!EFI_ERROR(Status
)){
1721 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1722 ASSERT(RetVal
== NULL
);
1723 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1724 ShellCloseFile(&Handle
);
1727 ShellCloseFile(&Handle
);
1731 Path
= ShellGetEnvironmentVariable(L
"cwd");
1733 Size
= StrSize(Path
) + sizeof(CHAR16
);
1734 Size
+= StrSize(FileName
);
1735 TestPath
= AllocateZeroPool(Size
);
1736 if (TestPath
== NULL
) {
1739 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), Path
);
1740 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1741 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1742 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1743 if (!EFI_ERROR(Status
)){
1744 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1745 ASSERT(RetVal
== NULL
);
1746 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1747 ShellCloseFile(&Handle
);
1751 ShellCloseFile(&Handle
);
1756 Path
= ShellGetEnvironmentVariable(L
"path");
1758 Size
= StrSize(Path
)+sizeof(CHAR16
);
1759 Size
+= StrSize(FileName
);
1760 TestPath
= AllocateZeroPool(Size
);
1761 if (TestPath
== NULL
) {
1764 Walker
= (CHAR16
*)Path
;
1766 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1767 if (TestPath
!= NULL
) {
1768 TempChar
= StrStr(TestPath
, L
";");
1769 if (TempChar
!= NULL
) {
1770 *TempChar
= CHAR_NULL
;
1772 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1773 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1775 if (FileName
[0] == L
'\\') {
1778 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1779 if (StrStr(Walker
, L
";") != NULL
) {
1780 Walker
= StrStr(Walker
, L
";") + 1;
1784 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1785 if (!EFI_ERROR(Status
)){
1786 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1787 ASSERT(RetVal
== NULL
);
1788 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1789 ShellCloseFile(&Handle
);
1792 ShellCloseFile(&Handle
);
1796 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1803 Find a file by searching the CWD and then the path with a variable set of file
1804 extensions. If the file is not found it will append each extension in the list
1805 in the order provided and return the first one that is successful.
1807 If FileName is NULL, then ASSERT.
1808 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1810 If the return value is not NULL then the memory must be caller freed.
1812 @param[in] FileName Filename string.
1813 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1815 @retval NULL The file was not found.
1816 @retval !NULL The path to the file.
1820 ShellFindFilePathEx (
1821 IN CONST CHAR16
*FileName
,
1822 IN CONST CHAR16
*FileExtension
1827 CONST CHAR16
*ExtensionWalker
;
1832 ASSERT(FileName
!= NULL
);
1833 if (FileExtension
== NULL
) {
1834 return (ShellFindFilePath(FileName
));
1836 RetVal
= ShellFindFilePath(FileName
);
1837 if (RetVal
!= NULL
) {
1840 Size
= StrSize(FileName
);
1841 Size
+= StrSize(FileExtension
);
1842 TestPath
= AllocateZeroPool(Size
);
1843 if (TestPath
== NULL
) {
1846 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1847 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1848 if (ExtensionWalker
!= NULL
) {
1849 StrCatS(TestPath
, Size
/sizeof(CHAR16
), ExtensionWalker
);
1851 TempChar
= StrStr(TestPath
, L
";");
1852 if (TempChar
!= NULL
) {
1853 *TempChar
= CHAR_NULL
;
1855 RetVal
= ShellFindFilePath(TestPath
);
1856 if (RetVal
!= NULL
) {
1859 ASSERT(ExtensionWalker
!= NULL
);
1860 TempChar2
= StrStr(ExtensionWalker
, L
";");
1869 SHELL_PARAM_TYPE Type
;
1871 UINTN OriginalPosition
;
1872 } SHELL_PARAM_PACKAGE
;
1875 Checks the list of valid arguments and returns TRUE if the item was found. If the
1876 return value is TRUE then the type parameter is set also.
1878 if CheckList is NULL then ASSERT();
1879 if Name is NULL then ASSERT();
1880 if Type is NULL then ASSERT();
1882 @param Name pointer to Name of parameter found
1883 @param CheckList List to check against
1884 @param Type pointer to type of parameter if it was found
1886 @retval TRUE the Parameter was found. Type is valid.
1887 @retval FALSE the Parameter was not found. Type is not valid.
1890 InternalIsOnCheckList (
1891 IN CONST CHAR16
*Name
,
1892 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1893 OUT SHELL_PARAM_TYPE
*Type
1896 SHELL_PARAM_ITEM
*TempListItem
;
1900 // ASSERT that all 3 pointer parameters aren't NULL
1902 ASSERT(CheckList
!= NULL
);
1903 ASSERT(Type
!= NULL
);
1904 ASSERT(Name
!= NULL
);
1907 // question mark and page break mode are always supported
1909 if ((StrCmp(Name
, L
"-?") == 0) ||
1910 (StrCmp(Name
, L
"-b") == 0)
1917 // Enumerate through the list
1919 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1921 // If the Type is TypeStart only check the first characters of the passed in param
1922 // If it matches set the type and return TRUE
1924 if (TempListItem
->Type
== TypeStart
) {
1925 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1926 *Type
= TempListItem
->Type
;
1930 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1931 if (TempString
!= NULL
) {
1932 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1933 *Type
= TempListItem
->Type
;
1934 FreePool(TempString
);
1937 FreePool(TempString
);
1939 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1940 *Type
= TempListItem
->Type
;
1948 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1950 @param[in] Name pointer to Name of parameter found
1951 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1952 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1954 @retval TRUE the Parameter is a flag.
1955 @retval FALSE the Parameter not a flag.
1959 IN CONST CHAR16
*Name
,
1960 IN CONST BOOLEAN AlwaysAllowNumbers
,
1961 IN CONST BOOLEAN TimeNumbers
1965 // ASSERT that Name isn't NULL
1967 ASSERT(Name
!= NULL
);
1970 // If we accept numbers then dont return TRUE. (they will be values)
1972 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
1977 // If the Name has a /, +, or - as the first character return TRUE
1979 if ((Name
[0] == L
'/') ||
1980 (Name
[0] == L
'-') ||
1989 Checks the command line arguments passed against the list of valid ones.
1991 If no initialization is required, then return RETURN_SUCCESS.
1993 @param[in] CheckList pointer to list of parameters to check
1994 @param[out] CheckPackage pointer to pointer to list checked values
1995 @param[out] ProblemParam optional pointer to pointer to unicode string for
1996 the paramater that caused failure. If used then the
1997 caller is responsible for freeing the memory.
1998 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1999 @param[in] Argv pointer to array of parameters
2000 @param[in] Argc Count of parameters in Argv
2001 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
2003 @retval EFI_SUCCESS The operation completed sucessfully.
2004 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2005 @retval EFI_INVALID_PARAMETER A parameter was invalid
2006 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2007 duplicated. the duplicated command line argument
2008 was returned in ProblemParam if provided.
2009 @retval EFI_NOT_FOUND a argument required a value that was missing.
2010 the invalid command line argument was returned in
2011 ProblemParam if provided.
2014 InternalCommandLineParse (
2015 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2016 OUT LIST_ENTRY
**CheckPackage
,
2017 OUT CHAR16
**ProblemParam OPTIONAL
,
2018 IN BOOLEAN AutoPageBreak
,
2019 IN CONST CHAR16
**Argv
,
2021 IN BOOLEAN AlwaysAllowNumbers
2025 SHELL_PARAM_TYPE CurrentItemType
;
2026 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2030 CONST CHAR16
*TempPointer
;
2031 UINTN CurrentValueSize
;
2034 CurrentItemPackage
= NULL
;
2040 // If there is only 1 item we dont need to do anything
2043 *CheckPackage
= NULL
;
2044 return (EFI_SUCCESS
);
2050 ASSERT(CheckList
!= NULL
);
2051 ASSERT(Argv
!= NULL
);
2054 // initialize the linked list
2056 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2057 if (*CheckPackage
== NULL
) {
2058 return (EFI_OUT_OF_RESOURCES
);
2061 InitializeListHead(*CheckPackage
);
2064 // loop through each of the arguments
2066 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2067 if (Argv
[LoopCounter
] == NULL
) {
2069 // do nothing for NULL argv
2071 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2073 // We might have leftover if last parameter didnt have optional value
2075 if (GetItemValue
!= 0) {
2077 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2082 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2083 if (CurrentItemPackage
== NULL
) {
2084 ShellCommandLineFreeVarList(*CheckPackage
);
2085 *CheckPackage
= NULL
;
2086 return (EFI_OUT_OF_RESOURCES
);
2088 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2089 if (CurrentItemPackage
->Name
== NULL
) {
2090 ShellCommandLineFreeVarList(*CheckPackage
);
2091 *CheckPackage
= NULL
;
2092 return (EFI_OUT_OF_RESOURCES
);
2094 CurrentItemPackage
->Type
= CurrentItemType
;
2095 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2096 CurrentItemPackage
->Value
= NULL
;
2099 // Does this flag require a value
2101 switch (CurrentItemPackage
->Type
) {
2103 // possibly trigger the next loop(s) to populate the value of this item
2110 case TypeDoubleValue
:
2115 GetItemValue
= (UINTN
)(-1);
2120 // this item has no value expected; we are done
2122 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2123 ASSERT(GetItemValue
== 0);
2126 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, (BOOLEAN
)(CurrentItemPackage
->Type
== TypeTimeValue
))) {
2128 // get the item VALUE for a previous flag
2130 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2131 NewValue
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2132 if (NewValue
== NULL
) {
2133 SHELL_FREE_NON_NULL (CurrentItemPackage
->Value
);
2134 SHELL_FREE_NON_NULL (CurrentItemPackage
);
2135 ShellCommandLineFreeVarList (*CheckPackage
);
2136 *CheckPackage
= NULL
;
2137 return EFI_OUT_OF_RESOURCES
;
2139 CurrentItemPackage
->Value
= NewValue
;
2140 if (ValueSize
== 0) {
2141 StrCpyS( CurrentItemPackage
->Value
,
2142 CurrentValueSize
/sizeof(CHAR16
),
2146 StrCatS( CurrentItemPackage
->Value
,
2147 CurrentValueSize
/sizeof(CHAR16
),
2150 StrCatS( CurrentItemPackage
->Value
,
2151 CurrentValueSize
/sizeof(CHAR16
),
2155 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2158 if (GetItemValue
== 0) {
2159 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2161 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2163 // add this one as a non-flag
2166 TempPointer
= Argv
[LoopCounter
];
2167 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2168 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2169 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2173 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2174 if (CurrentItemPackage
== NULL
) {
2175 ShellCommandLineFreeVarList(*CheckPackage
);
2176 *CheckPackage
= NULL
;
2177 return (EFI_OUT_OF_RESOURCES
);
2179 CurrentItemPackage
->Name
= NULL
;
2180 CurrentItemPackage
->Type
= TypePosition
;
2181 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2182 if (CurrentItemPackage
->Value
== NULL
) {
2183 ShellCommandLineFreeVarList(*CheckPackage
);
2184 *CheckPackage
= NULL
;
2185 return (EFI_OUT_OF_RESOURCES
);
2187 CurrentItemPackage
->OriginalPosition
= Count
++;
2188 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2191 // this was a non-recognised flag... error!
2193 if (ProblemParam
!= NULL
) {
2194 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2196 ShellCommandLineFreeVarList(*CheckPackage
);
2197 *CheckPackage
= NULL
;
2198 return (EFI_VOLUME_CORRUPTED
);
2201 if (GetItemValue
!= 0) {
2203 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2206 // support for AutoPageBreak
2208 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2209 ShellSetPageBreakMode(TRUE
);
2211 return (EFI_SUCCESS
);
2215 Checks the command line arguments passed against the list of valid ones.
2216 Optionally removes NULL values first.
2218 If no initialization is required, then return RETURN_SUCCESS.
2220 @param[in] CheckList The pointer to list of parameters to check.
2221 @param[out] CheckPackage The package of checked values.
2222 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2223 the paramater that caused failure.
2224 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2225 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2227 @retval EFI_SUCCESS The operation completed sucessfully.
2228 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2229 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2230 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2231 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2232 of the command line arguments was returned in
2233 ProblemParam if provided.
2234 @retval EFI_NOT_FOUND A argument required a value that was missing.
2235 The invalid command line argument was returned in
2236 ProblemParam if provided.
2240 ShellCommandLineParseEx (
2241 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2242 OUT LIST_ENTRY
**CheckPackage
,
2243 OUT CHAR16
**ProblemParam OPTIONAL
,
2244 IN BOOLEAN AutoPageBreak
,
2245 IN BOOLEAN AlwaysAllowNumbers
2249 // ASSERT that CheckList and CheckPackage aren't NULL
2251 ASSERT(CheckList
!= NULL
);
2252 ASSERT(CheckPackage
!= NULL
);
2255 // Check for UEFI Shell 2.0 protocols
2257 if (gEfiShellParametersProtocol
!= NULL
) {
2258 return (InternalCommandLineParse(CheckList
,
2262 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2263 gEfiShellParametersProtocol
->Argc
,
2264 AlwaysAllowNumbers
));
2268 // ASSERT That EFI Shell is not required
2270 ASSERT (mEfiShellInterface
!= NULL
);
2271 return (InternalCommandLineParse(CheckList
,
2275 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2276 mEfiShellInterface
->Argc
,
2277 AlwaysAllowNumbers
));
2281 Frees shell variable list that was returned from ShellCommandLineParse.
2283 This function will free all the memory that was used for the CheckPackage
2284 list of postprocessed shell arguments.
2286 this function has no return value.
2288 if CheckPackage is NULL, then return
2290 @param CheckPackage the list to de-allocate
2294 ShellCommandLineFreeVarList (
2295 IN LIST_ENTRY
*CheckPackage
2301 // check for CheckPackage == NULL
2303 if (CheckPackage
== NULL
) {
2308 // for each node in the list
2310 for ( Node
= GetFirstNode(CheckPackage
)
2311 ; !IsListEmpty(CheckPackage
)
2312 ; Node
= GetFirstNode(CheckPackage
)
2315 // Remove it from the list
2317 RemoveEntryList(Node
);
2320 // if it has a name free the name
2322 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2323 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2327 // if it has a value free the value
2329 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2330 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2334 // free the node structure
2336 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2339 // free the list head node
2341 FreePool(CheckPackage
);
2344 Checks for presence of a flag parameter
2346 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2348 if CheckPackage is NULL then return FALSE.
2349 if KeyString is NULL then ASSERT()
2351 @param CheckPackage The package of parsed command line arguments
2352 @param KeyString the Key of the command line argument to check for
2354 @retval TRUE the flag is on the command line
2355 @retval FALSE the flag is not on the command line
2359 ShellCommandLineGetFlag (
2360 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2361 IN CONST CHAR16
* CONST KeyString
2368 // return FALSE for no package or KeyString is NULL
2370 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2375 // enumerate through the list of parametrs
2377 for ( Node
= GetFirstNode(CheckPackage
)
2378 ; !IsNull (CheckPackage
, Node
)
2379 ; Node
= GetNextNode(CheckPackage
, Node
)
2382 // If the Name matches, return TRUE (and there may be NULL name)
2384 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2386 // If Type is TypeStart then only compare the begining of the strings
2388 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2389 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2393 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2394 if (TempString
!= NULL
) {
2395 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2396 FreePool(TempString
);
2399 FreePool(TempString
);
2401 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2409 Returns value from command line argument.
2411 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2413 If CheckPackage is NULL, then return NULL.
2415 @param[in] CheckPackage The package of parsed command line arguments.
2416 @param[in] KeyString The Key of the command line argument to check for.
2418 @retval NULL The flag is not on the command line.
2419 @retval !=NULL The pointer to unicode string of the value.
2423 ShellCommandLineGetValue (
2424 IN CONST LIST_ENTRY
*CheckPackage
,
2425 IN CHAR16
*KeyString
2432 // return NULL for no package or KeyString is NULL
2434 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2439 // enumerate through the list of parametrs
2441 for ( Node
= GetFirstNode(CheckPackage
)
2442 ; !IsNull (CheckPackage
, Node
)
2443 ; Node
= GetNextNode(CheckPackage
, Node
)
2446 // If the Name matches, return TRUE (and there may be NULL name)
2448 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2450 // If Type is TypeStart then only compare the begining of the strings
2452 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2453 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2454 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2457 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2458 if (TempString
!= NULL
) {
2459 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2460 FreePool(TempString
);
2461 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2463 FreePool(TempString
);
2465 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2466 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2474 Returns raw value from command line argument.
2476 Raw value parameters are in the form of "value" in a specific position in the list.
2478 If CheckPackage is NULL, then return NULL.
2480 @param[in] CheckPackage The package of parsed command line arguments.
2481 @param[in] Position The position of the value.
2483 @retval NULL The flag is not on the command line.
2484 @retval !=NULL The pointer to unicode string of the value.
2488 ShellCommandLineGetRawValue (
2489 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2496 // check for CheckPackage == NULL
2498 if (CheckPackage
== NULL
) {
2503 // enumerate through the list of parametrs
2505 for ( Node
= GetFirstNode(CheckPackage
)
2506 ; !IsNull (CheckPackage
, Node
)
2507 ; Node
= GetNextNode(CheckPackage
, Node
)
2510 // If the position matches, return the value
2512 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2513 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2520 returns the number of command line value parameters that were parsed.
2522 this will not include flags.
2524 @param[in] CheckPackage The package of parsed command line arguments.
2526 @retval (UINTN)-1 No parsing has ocurred
2527 @return other The number of value parameters found
2531 ShellCommandLineGetCount(
2532 IN CONST LIST_ENTRY
*CheckPackage
2538 if (CheckPackage
== NULL
) {
2541 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2542 ; !IsNull (CheckPackage
, Node1
)
2543 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2545 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2553 Determines if a parameter is duplicated.
2555 If Param is not NULL then it will point to a callee allocated string buffer
2556 with the parameter value if a duplicate is found.
2558 If CheckPackage is NULL, then ASSERT.
2560 @param[in] CheckPackage The package of parsed command line arguments.
2561 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2563 @retval EFI_SUCCESS No parameters were duplicated.
2564 @retval EFI_DEVICE_ERROR A duplicate was found.
2568 ShellCommandLineCheckDuplicate (
2569 IN CONST LIST_ENTRY
*CheckPackage
,
2576 ASSERT(CheckPackage
!= NULL
);
2578 for ( Node1
= GetFirstNode(CheckPackage
)
2579 ; !IsNull (CheckPackage
, Node1
)
2580 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2582 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2583 ; !IsNull (CheckPackage
, Node2
)
2584 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2586 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) {
2587 if (Param
!= NULL
) {
2589 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2591 return (EFI_DEVICE_ERROR
);
2595 return (EFI_SUCCESS
);
2599 This is a find and replace function. Upon successful return the NewString is a copy of
2600 SourceString with each instance of FindTarget replaced with ReplaceWith.
2602 If SourceString and NewString overlap the behavior is undefined.
2604 If the string would grow bigger than NewSize it will halt and return error.
2606 @param[in] SourceString The string with source buffer.
2607 @param[in, out] NewString The string with resultant buffer.
2608 @param[in] NewSize The size in bytes of NewString.
2609 @param[in] FindTarget The string to look for.
2610 @param[in] ReplaceWith The string to replace FindTarget with.
2611 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2612 immediately before it.
2613 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2615 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2616 @retval EFI_INVALID_PARAMETER NewString was NULL.
2617 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2618 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2619 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2620 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2621 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2622 the new string (truncation occurred).
2623 @retval EFI_SUCCESS The string was successfully copied with replacement.
2627 ShellCopySearchAndReplace(
2628 IN CHAR16 CONST
*SourceString
,
2629 IN OUT CHAR16
*NewString
,
2631 IN CONST CHAR16
*FindTarget
,
2632 IN CONST CHAR16
*ReplaceWith
,
2633 IN CONST BOOLEAN SkipPreCarrot
,
2634 IN CONST BOOLEAN ParameterReplacing
2640 if ( (SourceString
== NULL
)
2641 || (NewString
== NULL
)
2642 || (FindTarget
== NULL
)
2643 || (ReplaceWith
== NULL
)
2644 || (StrLen(FindTarget
) < 1)
2645 || (StrLen(SourceString
) < 1)
2647 return (EFI_INVALID_PARAMETER
);
2650 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2651 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2653 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2654 if (Replace
!= NULL
) {
2655 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2658 if (Replace
== NULL
) {
2659 return (EFI_OUT_OF_RESOURCES
);
2661 NewString
= ZeroMem(NewString
, NewSize
);
2662 while (*SourceString
!= CHAR_NULL
) {
2664 // if we find the FindTarget and either Skip == FALSE or Skip and we
2665 // dont have a carrot do a replace...
2667 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2668 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2670 SourceString
+= StrLen(FindTarget
);
2671 Size
= StrSize(NewString
);
2672 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2674 return (EFI_BUFFER_TOO_SMALL
);
2676 StrCatS(NewString
, NewSize
/sizeof(CHAR16
), Replace
);
2678 Size
= StrSize(NewString
);
2679 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2681 return (EFI_BUFFER_TOO_SMALL
);
2683 StrnCatS(NewString
, NewSize
/sizeof(CHAR16
), SourceString
, 1);
2688 return (EFI_SUCCESS
);
2692 Internal worker function to output a string.
2694 This function will output a string to the correct StdOut.
2696 @param[in] String The string to print out.
2698 @retval EFI_SUCCESS The operation was sucessful.
2699 @retval !EFI_SUCCESS The operation failed.
2703 IN CONST CHAR16
*String
2707 Size
= StrSize(String
) - sizeof(CHAR16
);
2709 return (EFI_SUCCESS
);
2711 if (gEfiShellParametersProtocol
!= NULL
) {
2712 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2714 if (mEfiShellInterface
!= NULL
) {
2715 if (mEfiShellInterface
->RedirArgc
== 0) {
2717 // Divide in half for old shell. Must be string length not size.
2719 Size
/=2; // Divide in half only when no redirection.
2721 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2724 return (EFI_UNSUPPORTED
);
2728 Print at a specific location on the screen.
2730 This function will move the cursor to a given screen location and print the specified string
2732 If -1 is specified for either the Row or Col the current screen location for BOTH
2735 if either Row or Col is out of range for the current console, then ASSERT
2736 if Format is NULL, then ASSERT
2738 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2739 the following additional flags:
2740 %N - Set output attribute to normal
2741 %H - Set output attribute to highlight
2742 %E - Set output attribute to error
2743 %B - Set output attribute to blue color
2744 %V - Set output attribute to green color
2746 Note: The background color is controlled by the shell command cls.
2748 @param[in] Col the column to print at
2749 @param[in] Row the row to print at
2750 @param[in] Format the format string
2751 @param[in] Marker the marker for the variable argument list
2753 @return EFI_SUCCESS The operation was successful.
2754 @return EFI_DEVICE_ERROR The console device reported an error.
2757 InternalShellPrintWorker(
2758 IN INT32 Col OPTIONAL
,
2759 IN INT32 Row OPTIONAL
,
2760 IN CONST CHAR16
*Format
,
2765 CHAR16
*ResumeLocation
;
2766 CHAR16
*FormatWalker
;
2767 UINTN OriginalAttribute
;
2768 CHAR16
*mPostReplaceFormat
;
2769 CHAR16
*mPostReplaceFormat2
;
2771 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2772 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2774 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2775 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2776 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2777 return (EFI_OUT_OF_RESOURCES
);
2780 Status
= EFI_SUCCESS
;
2781 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2784 // Back and forth each time fixing up 1 of our flags...
2786 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2787 ASSERT_EFI_ERROR(Status
);
2788 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2789 ASSERT_EFI_ERROR(Status
);
2790 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2791 ASSERT_EFI_ERROR(Status
);
2792 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2793 ASSERT_EFI_ERROR(Status
);
2794 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2795 ASSERT_EFI_ERROR(Status
);
2798 // Use the last buffer from replacing to print from...
2800 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2802 if (Col
!= -1 && Row
!= -1) {
2803 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2806 FormatWalker
= mPostReplaceFormat2
;
2807 while (*FormatWalker
!= CHAR_NULL
) {
2809 // Find the next attribute change request
2811 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2812 if (ResumeLocation
!= NULL
) {
2813 *ResumeLocation
= CHAR_NULL
;
2816 // print the current FormatWalker string
2818 if (StrLen(FormatWalker
)>0) {
2819 Status
= InternalPrintTo(FormatWalker
);
2820 if (EFI_ERROR(Status
)) {
2826 // update the attribute
2828 if (ResumeLocation
!= NULL
) {
2829 if ((ResumeLocation
!= mPostReplaceFormat2
) && (*(ResumeLocation
-1) == L
'^')) {
2831 // Move cursor back 1 position to overwrite the ^
2833 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2836 // Print a simple '%' symbol
2838 Status
= InternalPrintTo(L
"%");
2839 ResumeLocation
= ResumeLocation
- 1;
2841 switch (*(ResumeLocation
+1)) {
2843 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2846 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2849 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2852 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2855 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2859 // Print a simple '%' symbol
2861 Status
= InternalPrintTo(L
"%");
2862 if (EFI_ERROR(Status
)) {
2865 ResumeLocation
= ResumeLocation
- 1;
2871 // reset to normal now...
2877 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2879 FormatWalker
= ResumeLocation
+ 2;
2882 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2884 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2885 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2890 Print at a specific location on the screen.
2892 This function will move the cursor to a given screen location and print the specified string.
2894 If -1 is specified for either the Row or Col the current screen location for BOTH
2897 If either Row or Col is out of range for the current console, then ASSERT.
2898 If Format is NULL, then ASSERT.
2900 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2901 the following additional flags:
2902 %N - Set output attribute to normal
2903 %H - Set output attribute to highlight
2904 %E - Set output attribute to error
2905 %B - Set output attribute to blue color
2906 %V - Set output attribute to green color
2908 Note: The background color is controlled by the shell command cls.
2910 @param[in] Col the column to print at
2911 @param[in] Row the row to print at
2912 @param[in] Format the format string
2913 @param[in] ... The variable argument list.
2915 @return EFI_SUCCESS The printing was successful.
2916 @return EFI_DEVICE_ERROR The console device reported an error.
2921 IN INT32 Col OPTIONAL
,
2922 IN INT32 Row OPTIONAL
,
2923 IN CONST CHAR16
*Format
,
2929 if (Format
== NULL
) {
2930 return (EFI_INVALID_PARAMETER
);
2932 VA_START (Marker
, Format
);
2933 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2939 Print at a specific location on the screen.
2941 This function will move the cursor to a given screen location and print the specified string.
2943 If -1 is specified for either the Row or Col the current screen location for BOTH
2946 If either Row or Col is out of range for the current console, then ASSERT.
2947 If Format is NULL, then ASSERT.
2949 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2950 the following additional flags:
2951 %N - Set output attribute to normal.
2952 %H - Set output attribute to highlight.
2953 %E - Set output attribute to error.
2954 %B - Set output attribute to blue color.
2955 %V - Set output attribute to green color.
2957 Note: The background color is controlled by the shell command cls.
2959 @param[in] Col The column to print at.
2960 @param[in] Row The row to print at.
2961 @param[in] Language The language of the string to retrieve. If this parameter
2962 is NULL, then the current platform language is used.
2963 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2964 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2965 @param[in] ... The variable argument list.
2967 @return EFI_SUCCESS The printing was successful.
2968 @return EFI_DEVICE_ERROR The console device reported an error.
2973 IN INT32 Col OPTIONAL
,
2974 IN INT32 Row OPTIONAL
,
2975 IN CONST CHAR8
*Language OPTIONAL
,
2976 IN CONST EFI_STRING_ID HiiFormatStringId
,
2977 IN CONST EFI_HANDLE HiiFormatHandle
,
2982 CHAR16
*HiiFormatString
;
2985 RetVal
= EFI_DEVICE_ERROR
;
2987 VA_START (Marker
, HiiFormatHandle
);
2988 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2989 if (HiiFormatString
!= NULL
) {
2990 RetVal
= InternalShellPrintWorker (Col
, Row
, HiiFormatString
, Marker
);
2991 SHELL_FREE_NON_NULL (HiiFormatString
);
2999 Function to determine if a given filename represents a file or a directory.
3001 @param[in] DirName Path to directory to test.
3003 @retval EFI_SUCCESS The Path represents a directory
3004 @retval EFI_NOT_FOUND The Path does not represent a directory
3005 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3006 @return The path failed to open
3011 IN CONST CHAR16
*DirName
3015 SHELL_FILE_HANDLE Handle
;
3016 CHAR16
*TempLocation
;
3017 CHAR16
*TempLocation2
;
3019 ASSERT(DirName
!= NULL
);
3022 TempLocation
= NULL
;
3024 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3025 if (EFI_ERROR(Status
)) {
3027 // try good logic first.
3029 if (gEfiShellProtocol
!= NULL
) {
3030 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3031 if (TempLocation
== NULL
) {
3032 ShellCloseFile(&Handle
);
3033 return (EFI_OUT_OF_RESOURCES
);
3035 TempLocation2
= StrStr(TempLocation
, L
":");
3036 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3037 *(TempLocation2
+1) = CHAR_NULL
;
3039 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3040 FreePool(TempLocation
);
3041 return (EFI_SUCCESS
);
3043 FreePool(TempLocation
);
3046 // probably a map name?!?!!?
3048 TempLocation
= StrStr(DirName
, L
"\\");
3049 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3050 return (EFI_SUCCESS
);
3056 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3057 ShellCloseFile(&Handle
);
3058 return (EFI_SUCCESS
);
3060 ShellCloseFile(&Handle
);
3061 return (EFI_NOT_FOUND
);
3065 Function to determine if a given filename represents a file.
3067 @param[in] Name Path to file to test.
3069 @retval EFI_SUCCESS The Path represents a file.
3070 @retval EFI_NOT_FOUND The Path does not represent a file.
3071 @retval other The path failed to open.
3076 IN CONST CHAR16
*Name
3080 SHELL_FILE_HANDLE Handle
;
3082 ASSERT(Name
!= NULL
);
3086 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3087 if (EFI_ERROR(Status
)) {
3091 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3092 ShellCloseFile(&Handle
);
3093 return (EFI_SUCCESS
);
3095 ShellCloseFile(&Handle
);
3096 return (EFI_NOT_FOUND
);
3100 Function to determine if a given filename represents a file.
3102 This will search the CWD and then the Path.
3104 If Name is NULL, then ASSERT.
3106 @param[in] Name Path to file to test.
3108 @retval EFI_SUCCESS The Path represents a file.
3109 @retval EFI_NOT_FOUND The Path does not represent a file.
3110 @retval other The path failed to open.
3115 IN CONST CHAR16
*Name
3121 if (!EFI_ERROR(ShellIsFile(Name
))) {
3122 return (EFI_SUCCESS
);
3125 NewName
= ShellFindFilePath(Name
);
3126 if (NewName
== NULL
) {
3127 return (EFI_NOT_FOUND
);
3129 Status
= ShellIsFile(NewName
);
3135 Function return the number converted from a hex representation of a number.
3137 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3138 result. Use ShellConvertStringToUint64 instead.
3140 @param[in] String String representation of a number.
3142 @return The unsigned integer result of the conversion.
3143 @retval (UINTN)(-1) An error occured.
3148 IN CONST CHAR16
*String
3153 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3154 return ((UINTN
)RetVal
);
3157 return ((UINTN
)(-1));
3161 Function to determine whether a string is decimal or hex representation of a number
3162 and return the number converted from the string. Spaces are always skipped.
3164 @param[in] String String representation of a number
3167 @retval (UINTN)(-1) An error ocurred.
3172 IN CONST CHAR16
*String
3180 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3184 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3185 return ((UINTN
)RetVal
);
3187 return ((UINTN
)(-1));
3191 Safely append with automatic string resizing given length of Destination and
3192 desired length of copy from Source.
3194 append the first D characters of Source to the end of Destination, where D is
3195 the lesser of Count and the StrLen() of Source. If appending those D characters
3196 will fit within Destination (whose Size is given as CurrentSize) and
3197 still leave room for a NULL terminator, then those characters are appended,
3198 starting at the original terminating NULL of Destination, and a new terminating
3201 If appending D characters onto Destination will result in a overflow of the size
3202 given in CurrentSize the string will be grown such that the copy can be performed
3203 and CurrentSize will be updated to the new size.
3205 If Source is NULL, there is nothing to append, just return the current buffer in
3208 if Destination is NULL, then ASSERT()
3209 if Destination's current length (including NULL terminator) is already more then
3210 CurrentSize, then ASSERT()
3212 @param[in, out] Destination The String to append onto
3213 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3214 return possibly the new size (still in bytes). if NULL
3215 then allocate whatever is needed.
3216 @param[in] Source The String to append from
3217 @param[in] Count Maximum number of characters to append. if 0 then
3220 @return Destination return the resultant string.
3225 IN OUT CHAR16
**Destination
,
3226 IN OUT UINTN
*CurrentSize
,
3227 IN CONST CHAR16
*Source
,
3231 UINTN DestinationStartSize
;
3237 ASSERT(Destination
!= NULL
);
3240 // If there's nothing to do then just return Destination
3242 if (Source
== NULL
) {
3243 return (*Destination
);
3247 // allow for un-initialized pointers, based on size being 0
3249 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3250 *Destination
= NULL
;
3254 // allow for NULL pointers address as Destination
3256 if (*Destination
!= NULL
) {
3257 ASSERT(CurrentSize
!= 0);
3258 DestinationStartSize
= StrSize(*Destination
);
3259 ASSERT(DestinationStartSize
<= *CurrentSize
);
3261 DestinationStartSize
= 0;
3262 // ASSERT(*CurrentSize == 0);
3266 // Append all of Source?
3269 Count
= StrLen(Source
);
3273 // Test and grow if required
3275 if (CurrentSize
!= NULL
) {
3276 NewSize
= *CurrentSize
;
3277 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3278 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3279 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3281 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3282 *CurrentSize
= NewSize
;
3285 NewSize
= (Count
+1)*sizeof(CHAR16
);
3286 *Destination
= AllocateZeroPool(NewSize
);
3290 // Now use standard StrnCat on a big enough buffer
3292 if (*Destination
== NULL
) {
3296 StrnCatS(*Destination
, NewSize
/sizeof(CHAR16
), Source
, Count
);
3297 return *Destination
;
3301 Prompt the user and return the resultant answer to the requestor.
3303 This function will display the requested question on the shell prompt and then
3304 wait for an appropriate answer to be input from the console.
3306 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3307 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3309 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3312 In either case *Response must be callee freed if Response was not NULL;
3314 @param Type What type of question is asked. This is used to filter the input
3315 to prevent invalid answers to question.
3316 @param Prompt Pointer to string prompt to use to request input.
3317 @param Response Pointer to Response which will be populated upon return.
3319 @retval EFI_SUCCESS The operation was sucessful.
3320 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3321 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3322 @return other The operation failed.
3326 ShellPromptForResponse (
3327 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3328 IN CHAR16
*Prompt OPTIONAL
,
3329 IN OUT VOID
**Response OPTIONAL
3335 SHELL_PROMPT_RESPONSE
*Resp
;
3339 Status
= EFI_UNSUPPORTED
;
3343 if (Type
!= ShellPromptResponseTypeFreeform
) {
3344 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3346 return (EFI_OUT_OF_RESOURCES
);
3351 case ShellPromptResponseTypeQuitContinue
:
3352 if (Prompt
!= NULL
) {
3353 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3356 // wait for valid response
3358 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3359 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3360 if (EFI_ERROR(Status
)) {
3363 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3364 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3365 *Resp
= ShellPromptResponseQuit
;
3367 *Resp
= ShellPromptResponseContinue
;
3370 case ShellPromptResponseTypeYesNoCancel
:
3371 if (Prompt
!= NULL
) {
3372 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3375 // wait for valid response
3377 *Resp
= ShellPromptResponseMax
;
3378 while (*Resp
== ShellPromptResponseMax
) {
3379 if (ShellGetExecutionBreakFlag()) {
3380 Status
= EFI_ABORTED
;
3383 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3384 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3385 if (EFI_ERROR(Status
)) {
3388 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3389 switch (Key
.UnicodeChar
) {
3392 *Resp
= ShellPromptResponseYes
;
3396 *Resp
= ShellPromptResponseNo
;
3400 *Resp
= ShellPromptResponseCancel
;
3405 case ShellPromptResponseTypeYesNoAllCancel
:
3406 if (Prompt
!= NULL
) {
3407 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3410 // wait for valid response
3412 *Resp
= ShellPromptResponseMax
;
3413 while (*Resp
== ShellPromptResponseMax
) {
3414 if (ShellGetExecutionBreakFlag()) {
3415 Status
= EFI_ABORTED
;
3418 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3419 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3420 if (EFI_ERROR(Status
)) {
3424 if (Key
.UnicodeChar
<= 127 && Key
.UnicodeChar
>= 32) {
3425 ShellPrintEx (-1, -1, L
"%c", Key
.UnicodeChar
);
3428 switch (Key
.UnicodeChar
) {
3431 *Resp
= ShellPromptResponseYes
;
3435 *Resp
= ShellPromptResponseNo
;
3439 *Resp
= ShellPromptResponseAll
;
3443 *Resp
= ShellPromptResponseCancel
;
3448 case ShellPromptResponseTypeEnterContinue
:
3449 case ShellPromptResponseTypeAnyKeyContinue
:
3450 if (Prompt
!= NULL
) {
3451 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3454 // wait for valid response
3456 *Resp
= ShellPromptResponseMax
;
3457 while (*Resp
== ShellPromptResponseMax
) {
3458 if (ShellGetExecutionBreakFlag()) {
3459 Status
= EFI_ABORTED
;
3462 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3463 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3464 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3465 if (EFI_ERROR(Status
)) {
3468 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3469 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3470 *Resp
= ShellPromptResponseContinue
;
3474 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3475 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3476 ASSERT_EFI_ERROR(Status
);
3477 *Resp
= ShellPromptResponseContinue
;
3482 case ShellPromptResponseTypeYesNo
:
3483 if (Prompt
!= NULL
) {
3484 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3487 // wait for valid response
3489 *Resp
= ShellPromptResponseMax
;
3490 while (*Resp
== ShellPromptResponseMax
) {
3491 if (ShellGetExecutionBreakFlag()) {
3492 Status
= EFI_ABORTED
;
3495 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3496 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3497 if (EFI_ERROR(Status
)) {
3500 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3501 switch (Key
.UnicodeChar
) {
3504 *Resp
= ShellPromptResponseYes
;
3508 *Resp
= ShellPromptResponseNo
;
3513 case ShellPromptResponseTypeFreeform
:
3514 if (Prompt
!= NULL
) {
3515 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3518 if (ShellGetExecutionBreakFlag()) {
3519 Status
= EFI_ABORTED
;
3522 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3523 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3524 if (EFI_ERROR(Status
)) {
3527 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3528 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3531 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3532 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3536 // This is the location to add new prompt types.
3537 // If your new type loops remember to add ExecutionBreak support.
3543 if (Response
!= NULL
) {
3546 } else if (Buffer
!= NULL
) {
3553 if (Buffer
!= NULL
) {
3558 ShellPrintEx(-1, -1, L
"\r\n");
3563 Prompt the user and return the resultant answer to the requestor.
3565 This function is the same as ShellPromptForResponse, except that the prompt is
3566 automatically pulled from HII.
3568 @param Type What type of question is asked. This is used to filter the input
3569 to prevent invalid answers to question.
3570 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3571 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3572 @param Response Pointer to Response which will be populated upon return.
3574 @retval EFI_SUCCESS the operation was sucessful.
3575 @return other the operation failed.
3577 @sa ShellPromptForResponse
3581 ShellPromptForResponseHii (
3582 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3583 IN CONST EFI_STRING_ID HiiFormatStringId
,
3584 IN CONST EFI_HANDLE HiiFormatHandle
,
3585 IN OUT VOID
**Response
3591 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3592 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3598 Function to determin if an entire string is a valid number.
3600 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3602 @param[in] String The string to evaluate.
3603 @param[in] ForceHex TRUE - always assume hex.
3604 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3605 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3607 @retval TRUE It is all numeric (dec/hex) characters.
3608 @retval FALSE There is a non-numeric character.
3611 InternalShellIsHexOrDecimalNumber (
3612 IN CONST CHAR16
*String
,
3613 IN CONST BOOLEAN ForceHex
,
3614 IN CONST BOOLEAN StopAtSpace
,
3615 IN CONST BOOLEAN TimeNumbers
3621 // chop off a single negative sign
3623 if (String
!= NULL
&& *String
== L
'-') {
3627 if (String
== NULL
) {
3632 // chop leading zeroes
3634 while(String
!= NULL
&& *String
== L
'0'){
3638 // allow '0x' or '0X', but not 'x' or 'X'
3640 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3641 if (*(String
-1) != L
'0') {
3643 // we got an x without a preceeding 0
3649 } else if (ForceHex
) {
3656 // loop through the remaining characters and use the lib function
3658 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3659 if (TimeNumbers
&& (String
[0] == L
':')) {
3663 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3667 if (!ShellIsDecimalDigitCharacter(*String
)) {
3677 Function to determine if a given filename exists.
3679 @param[in] Name Path to test.
3681 @retval EFI_SUCCESS The Path represents a file.
3682 @retval EFI_NOT_FOUND The Path does not represent a file.
3683 @retval other The path failed to open.
3688 IN CONST CHAR16
*Name
3692 EFI_SHELL_FILE_INFO
*List
;
3694 ASSERT(Name
!= NULL
);
3697 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3698 if (EFI_ERROR(Status
)) {
3702 ShellCloseFileMetaArg(&List
);
3704 return (EFI_SUCCESS
);
3708 Convert a Unicode character to upper case only if
3709 it maps to a valid small-case ASCII character.
3711 This internal function only deal with Unicode character
3712 which maps to a valid small-case ASCII character, i.e.
3713 L'a' to L'z'. For other Unicode character, the input character
3714 is returned directly.
3716 @param Char The character to convert.
3718 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3719 @retval Unchanged Otherwise.
3723 InternalShellCharToUpper (
3727 if (Char
>= L
'a' && Char
<= L
'z') {
3728 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3735 Convert a Unicode character to numerical value.
3737 This internal function only deal with Unicode character
3738 which maps to a valid hexadecimal ASII character, i.e.
3739 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3740 Unicode character, the value returned does not make sense.
3742 @param Char The character to convert.
3744 @return The numerical value converted.
3748 InternalShellHexCharToUintn (
3752 if (ShellIsDecimalDigitCharacter (Char
)) {
3756 return (10 + InternalShellCharToUpper (Char
) - L
'A');
3760 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3762 This function returns a value of type UINT64 by interpreting the contents
3763 of the Unicode string specified by String as a hexadecimal number.
3764 The format of the input Unicode string String is:
3766 [spaces][zeros][x][hexadecimal digits].
3768 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3769 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3770 If "x" appears in the input string, it must be prefixed with at least one 0.
3771 The function will ignore the pad space, which includes spaces or tab characters,
3772 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3773 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3774 first valid hexadecimal digit. Then, the function stops at the first character that is
3775 a not a valid hexadecimal character or NULL, whichever one comes first.
3777 If String has only pad spaces, then zero is returned.
3778 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3779 then zero is returned.
3781 @param[in] String A pointer to a Null-terminated Unicode string.
3782 @param[out] Value Upon a successful return the value of the conversion.
3783 @param[in] StopAtSpace FALSE to skip spaces.
3785 @retval EFI_SUCCESS The conversion was successful.
3786 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3787 @retval EFI_DEVICE_ERROR An overflow occured.
3790 InternalShellStrHexToUint64 (
3791 IN CONST CHAR16
*String
,
3793 IN CONST BOOLEAN StopAtSpace
3798 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3799 return (EFI_INVALID_PARAMETER
);
3803 // Ignore the pad spaces (space or tab)
3805 while ((*String
== L
' ') || (*String
== L
'\t')) {
3810 // Ignore leading Zeros after the spaces
3812 while (*String
== L
'0') {
3816 if (InternalShellCharToUpper (*String
) == L
'X') {
3817 if (*(String
- 1) != L
'0') {
3829 // there is a space where there should't be
3831 if (*String
== L
' ') {
3832 return (EFI_INVALID_PARAMETER
);
3835 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3837 // If the Hex Number represented by String overflows according
3838 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3840 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3841 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3842 return (EFI_DEVICE_ERROR
);
3845 Result
= (LShiftU64(Result
, 4));
3846 Result
+= InternalShellHexCharToUintn (*String
);
3850 // stop at spaces if requested
3852 if (StopAtSpace
&& *String
== L
' ') {
3858 return (EFI_SUCCESS
);
3862 Convert a Null-terminated Unicode decimal string to a value of
3865 This function returns a value of type UINT64 by interpreting the contents
3866 of the Unicode string specified by String as a decimal number. The format
3867 of the input Unicode string String is:
3869 [spaces] [decimal digits].
3871 The valid decimal digit character is in the range [0-9]. The
3872 function will ignore the pad space, which includes spaces or
3873 tab characters, before [decimal digits]. The running zero in the
3874 beginning of [decimal digits] will be ignored. Then, the function
3875 stops at the first character that is a not a valid decimal character
3876 or a Null-terminator, whichever one comes first.
3878 If String has only pad spaces, then 0 is returned.
3879 If String has no pad spaces or valid decimal digits,
3882 @param[in] String A pointer to a Null-terminated Unicode string.
3883 @param[out] Value Upon a successful return the value of the conversion.
3884 @param[in] StopAtSpace FALSE to skip spaces.
3886 @retval EFI_SUCCESS The conversion was successful.
3887 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3888 @retval EFI_DEVICE_ERROR An overflow occured.
3891 InternalShellStrDecimalToUint64 (
3892 IN CONST CHAR16
*String
,
3894 IN CONST BOOLEAN StopAtSpace
3899 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3900 return (EFI_INVALID_PARAMETER
);
3904 // Ignore the pad spaces (space or tab)
3906 while ((*String
== L
' ') || (*String
== L
'\t')) {
3911 // Ignore leading Zeros after the spaces
3913 while (*String
== L
'0') {
3920 // Stop upon space if requested
3921 // (if the whole value was 0)
3923 if (StopAtSpace
&& *String
== L
' ') {
3925 return (EFI_SUCCESS
);
3928 while (ShellIsDecimalDigitCharacter (*String
)) {
3930 // If the number represented by String overflows according
3931 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3934 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3935 return (EFI_DEVICE_ERROR
);
3938 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3942 // Stop at spaces if requested
3944 if (StopAtSpace
&& *String
== L
' ') {
3951 return (EFI_SUCCESS
);
3955 Function to verify and convert a string to its numerical value.
3957 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3959 @param[in] String The string to evaluate.
3960 @param[out] Value Upon a successful return the value of the conversion.
3961 @param[in] ForceHex TRUE - always assume hex.
3962 @param[in] StopAtSpace FALSE to skip spaces.
3964 @retval EFI_SUCCESS The conversion was successful.
3965 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3966 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3970 ShellConvertStringToUint64(
3971 IN CONST CHAR16
*String
,
3973 IN CONST BOOLEAN ForceHex
,
3974 IN CONST BOOLEAN StopAtSpace
3978 CONST CHAR16
*Walker
;
3984 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3987 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3988 return (EFI_INVALID_PARAMETER
);
3991 return (EFI_INVALID_PARAMETER
);
3996 // Chop off leading spaces
3998 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
4001 // make sure we have something left that is numeric.
4003 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
4004 return (EFI_INVALID_PARAMETER
);
4008 // do the conversion.
4010 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
4011 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
4013 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
4016 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
4017 return (EFI_NOT_FOUND
);
4020 if (Value
!= NULL
) {
4028 Function to determin if an entire string is a valid number.
4030 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4032 @param[in] String The string to evaluate.
4033 @param[in] ForceHex TRUE - always assume hex.
4034 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4036 @retval TRUE It is all numeric (dec/hex) characters.
4037 @retval FALSE There is a non-numeric character.
4041 ShellIsHexOrDecimalNumber (
4042 IN CONST CHAR16
*String
,
4043 IN CONST BOOLEAN ForceHex
,
4044 IN CONST BOOLEAN StopAtSpace
4047 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4054 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4055 buffer. The returned buffer must be callee freed.
4057 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4058 maintained and not changed for all operations with the same file.
4060 @param[in] Handle SHELL_FILE_HANDLE to read from.
4061 @param[in, out] Ascii Boolean value for indicating whether the file is
4062 Ascii (TRUE) or UCS2 (FALSE).
4064 @return The line of text from the file.
4065 @retval NULL There was not enough memory available.
4067 @sa ShellFileHandleReadLine
4071 ShellFileHandleReturnLine(
4072 IN SHELL_FILE_HANDLE Handle
,
4073 IN OUT BOOLEAN
*Ascii
4083 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4084 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4085 RetVal
= AllocateZeroPool(Size
);
4086 if (RetVal
== NULL
) {
4089 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4092 if (Status
== EFI_END_OF_FILE
&& RetVal
!= NULL
&& *RetVal
!= CHAR_NULL
) {
4093 Status
= EFI_SUCCESS
;
4095 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4103 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4105 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4106 maintained and not changed for all operations with the same file.
4108 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4111 @param[in] Handle SHELL_FILE_HANDLE to read from.
4112 @param[in, out] Buffer The pointer to buffer to read into. If this function
4113 returns EFI_SUCCESS, then on output Buffer will
4114 contain a UCS2 string, even if the file being
4116 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4117 On output, unchanged unless Buffer is too small
4118 to contain the next line of the file. In that
4119 case Size is set to the number of bytes needed
4120 to hold the next line of the file (as a UCS2
4121 string, even if it is an ASCII file).
4122 @param[in] Truncate If the buffer is large enough, this has no effect.
4123 If the buffer is is too small and Truncate is TRUE,
4124 the line will be truncated.
4125 If the buffer is is too small and Truncate is FALSE,
4126 then no read will occur.
4128 @param[in, out] Ascii Boolean value for indicating whether the file is
4129 Ascii (TRUE) or UCS2 (FALSE).
4131 @retval EFI_SUCCESS The operation was successful. The line is stored in
4133 @retval EFI_END_OF_FILE There are no more lines in the file.
4134 @retval EFI_INVALID_PARAMETER Handle was NULL.
4135 @retval EFI_INVALID_PARAMETER Size was NULL.
4136 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4137 Size was updated to the minimum space required.
4141 ShellFileHandleReadLine(
4142 IN SHELL_FILE_HANDLE Handle
,
4143 IN OUT CHAR16
*Buffer
,
4145 IN BOOLEAN Truncate
,
4146 IN OUT BOOLEAN
*Ascii
4153 UINT64 OriginalFilePosition
;
4159 return (EFI_INVALID_PARAMETER
);
4161 if (Buffer
== NULL
) {
4164 *Buffer
= CHAR_NULL
;
4166 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4167 if (OriginalFilePosition
== 0) {
4168 CharSize
= sizeof(CHAR16
);
4169 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4170 ASSERT_EFI_ERROR(Status
);
4171 if (CharBuffer
== gUnicodeFileTag
) {
4175 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4180 CharSize
= sizeof(CHAR8
);
4182 CharSize
= sizeof(CHAR16
);
4184 for (CountSoFar
= 0;;CountSoFar
++){
4186 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4187 if ( EFI_ERROR(Status
)
4189 || (CharBuffer
== L
'\n' && !(*Ascii
))
4190 || (CharBuffer
== '\n' && *Ascii
)
4192 if (CharSize
== 0) {
4193 Status
= EFI_END_OF_FILE
;
4198 // if we have space save it...
4200 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4201 ASSERT(Buffer
!= NULL
);
4202 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4203 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4208 // if we ran out of space tell when...
4210 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4211 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4213 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4215 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4217 return (EFI_BUFFER_TOO_SMALL
);
4219 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4220 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4227 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4229 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4230 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4231 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4232 the help content only.
4233 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4234 @retval EFI_NOT_FOUND The help data could not be found.
4235 @retval EFI_SUCCESS The operation was successful.
4240 IN CONST CHAR16
*CommandToGetHelpOn
,
4241 IN CONST CHAR16
*SectionToGetHelpOn
,
4242 IN BOOLEAN PrintCommandText
4251 // Get the string to print based
4253 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4256 // make sure we got a valid string
4258 if (EFI_ERROR(Status
)){
4261 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4262 return EFI_NOT_FOUND
;
4266 // Chop off trailing stuff we dont need
4268 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4269 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4273 // Print this out to the console
4275 if (PrintCommandText
) {
4276 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4278 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4281 SHELL_FREE_NON_NULL(OutText
);
4287 Function to delete a file by name
4289 @param[in] FileName Pointer to file name to delete.
4291 @retval EFI_SUCCESS the file was deleted sucessfully
4292 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4294 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4295 @retval EFI_NOT_FOUND The specified file could not be found on the
4296 device or the file system could not be found
4298 @retval EFI_NO_MEDIA The device has no medium.
4299 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4300 medium is no longer supported.
4301 @retval EFI_DEVICE_ERROR The device reported an error.
4302 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4303 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4304 @retval EFI_ACCESS_DENIED The file was opened read only.
4305 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4307 @retval other The file failed to open
4311 ShellDeleteFileByName(
4312 IN CONST CHAR16
*FileName
4316 SHELL_FILE_HANDLE FileHandle
;
4318 Status
= ShellFileExists(FileName
);
4320 if (Status
== EFI_SUCCESS
){
4321 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4322 if (Status
== EFI_SUCCESS
){
4323 Status
= ShellDeleteFile(&FileHandle
);
4332 Cleans off all the quotes in the string.
4334 @param[in] OriginalString pointer to the string to be cleaned.
4335 @param[out] CleanString The new string with all quotes removed.
4336 Memory allocated in the function and free
4339 @retval EFI_SUCCESS The operation was successful.
4342 InternalShellStripQuotes (
4343 IN CONST CHAR16
*OriginalString
,
4344 OUT CHAR16
**CleanString
4349 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4350 return EFI_INVALID_PARAMETER
;
4353 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4354 if (*CleanString
== NULL
) {
4355 return EFI_OUT_OF_RESOURCES
;
4358 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4359 if (*Walker
== L
'\"') {
4360 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));