2 Provides interface to shell functionality for shell commands and applications.
4 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
5 Copyright 2016 Dell Inc.
6 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "UefiShellLib.h"
18 #include <ShellBase.h>
19 #include <Library/SortLib.h>
20 #include <Library/BaseLib.h>
22 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
27 SHELL_PARAM_ITEM EmptyParamList
[] = {
30 SHELL_PARAM_ITEM SfoParamList
[] = {
34 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
35 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
36 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
37 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
38 EFI_HANDLE mEfiShellEnvironment2Handle
;
39 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
40 EFI_UNICODE_COLLATION_PROTOCOL
*mUnicodeCollationProtocol
;
43 Check if a Unicode character is a hexadecimal character.
45 This internal function checks if a Unicode character is a
46 numeric character. The valid hexadecimal characters are
47 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
49 @param Char The character to check against.
51 @retval TRUE If the Char is a hexadecmial character.
52 @retval FALSE If the Char is not a hexadecmial character.
57 ShellIsHexaDecimalDigitCharacter (
61 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
65 Check if a Unicode character is a decimal character.
67 This internal function checks if a Unicode character is a
68 decimal character. The valid characters are
72 @param Char The character to check against.
74 @retval TRUE If the Char is a hexadecmial character.
75 @retval FALSE If the Char is not a hexadecmial character.
80 ShellIsDecimalDigitCharacter (
84 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
88 Helper function to find ShellEnvironment2 for constructor.
90 @param[in] ImageHandle A copy of the calling image's handle.
92 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
96 IN EFI_HANDLE ImageHandle
106 Status
= gBS
->OpenProtocol(ImageHandle
,
107 &gEfiShellEnvironment2Guid
,
108 (VOID
**)&mEfiShellEnvironment2
,
111 EFI_OPEN_PROTOCOL_GET_PROTOCOL
114 // look for the mEfiShellEnvironment2 protocol at a higher level
116 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
118 // figure out how big of a buffer we need.
120 Status
= gBS
->LocateHandle (ByProtocol
,
121 &gEfiShellEnvironment2Guid
,
122 NULL
, // ignored for ByProtocol
127 // maybe it's not there???
129 if (Status
== EFI_BUFFER_TOO_SMALL
) {
130 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
131 if (Buffer
== NULL
) {
132 return (EFI_OUT_OF_RESOURCES
);
134 Status
= gBS
->LocateHandle (ByProtocol
,
135 &gEfiShellEnvironment2Guid
,
136 NULL
, // ignored for ByProtocol
141 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
143 // now parse the list of returned handles
145 Status
= EFI_NOT_FOUND
;
146 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
147 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
148 &gEfiShellEnvironment2Guid
,
149 (VOID
**)&mEfiShellEnvironment2
,
152 EFI_OPEN_PROTOCOL_GET_PROTOCOL
154 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
155 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
156 Status
= EFI_SUCCESS
;
162 if (Buffer
!= NULL
) {
169 Function to do most of the work of the constructor. Allows for calling
170 multiple times without complete re-initialization.
172 @param[in] ImageHandle A copy of the ImageHandle.
173 @param[in] SystemTable A pointer to the SystemTable for the application.
175 @retval EFI_SUCCESS The operationw as successful.
178 ShellLibConstructorWorker (
179 IN EFI_HANDLE ImageHandle
,
180 IN EFI_SYSTEM_TABLE
*SystemTable
186 // UEFI 2.0 shell interfaces (used preferentially)
188 Status
= gBS
->OpenProtocol(
190 &gEfiShellProtocolGuid
,
191 (VOID
**)&gEfiShellProtocol
,
194 EFI_OPEN_PROTOCOL_GET_PROTOCOL
196 if (EFI_ERROR(Status
)) {
198 // Search for the shell protocol
200 Status
= gBS
->LocateProtocol(
201 &gEfiShellProtocolGuid
,
203 (VOID
**)&gEfiShellProtocol
205 if (EFI_ERROR(Status
)) {
206 gEfiShellProtocol
= NULL
;
209 Status
= gBS
->OpenProtocol(
211 &gEfiShellParametersProtocolGuid
,
212 (VOID
**)&gEfiShellParametersProtocol
,
215 EFI_OPEN_PROTOCOL_GET_PROTOCOL
217 if (EFI_ERROR(Status
)) {
218 gEfiShellParametersProtocol
= NULL
;
221 if (gEfiShellParametersProtocol
== NULL
|| gEfiShellProtocol
== NULL
) {
223 // Moved to seperate function due to complexity
225 Status
= ShellFindSE2(ImageHandle
);
227 if (EFI_ERROR(Status
)) {
228 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
229 mEfiShellEnvironment2
= NULL
;
231 Status
= gBS
->OpenProtocol(ImageHandle
,
232 &gEfiShellInterfaceGuid
,
233 (VOID
**)&mEfiShellInterface
,
236 EFI_OPEN_PROTOCOL_GET_PROTOCOL
238 if (EFI_ERROR(Status
)) {
239 mEfiShellInterface
= NULL
;
244 // only success getting 2 of either the old or new, but no 1/2 and 1/2
246 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
247 (gEfiShellProtocol
!= NULL
&& gEfiShellParametersProtocol
!= NULL
) ) {
248 if (gEfiShellProtocol
!= NULL
) {
249 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
250 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
251 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
252 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
253 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
254 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
255 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
256 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
257 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
258 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
260 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
261 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
262 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
263 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
264 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
265 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
266 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
267 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
268 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
269 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
271 return (EFI_SUCCESS
);
273 return (EFI_NOT_FOUND
);
276 Constructor for the Shell library.
278 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
280 @param ImageHandle the image handle of the process
281 @param SystemTable the EFI System Table pointer
283 @retval EFI_SUCCESS the initialization was complete sucessfully
284 @return others an error ocurred during initialization
288 ShellLibConstructor (
289 IN EFI_HANDLE ImageHandle
,
290 IN EFI_SYSTEM_TABLE
*SystemTable
293 mEfiShellEnvironment2
= NULL
;
294 gEfiShellProtocol
= NULL
;
295 gEfiShellParametersProtocol
= NULL
;
296 mEfiShellInterface
= NULL
;
297 mEfiShellEnvironment2Handle
= NULL
;
298 mUnicodeCollationProtocol
= NULL
;
301 // verify that auto initialize is not set false
303 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
304 return (EFI_SUCCESS
);
307 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
311 Destructor for the library. free any resources.
313 @param[in] ImageHandle A copy of the ImageHandle.
314 @param[in] SystemTable A pointer to the SystemTable for the application.
316 @retval EFI_SUCCESS The operation was successful.
317 @return An error from the CloseProtocol function.
322 IN EFI_HANDLE ImageHandle
,
323 IN EFI_SYSTEM_TABLE
*SystemTable
326 if (mEfiShellEnvironment2
!= NULL
) {
327 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
328 &gEfiShellEnvironment2Guid
,
331 mEfiShellEnvironment2
= NULL
;
333 if (mEfiShellInterface
!= NULL
) {
334 gBS
->CloseProtocol(ImageHandle
,
335 &gEfiShellInterfaceGuid
,
338 mEfiShellInterface
= NULL
;
340 if (gEfiShellProtocol
!= NULL
) {
341 gBS
->CloseProtocol(ImageHandle
,
342 &gEfiShellProtocolGuid
,
345 gEfiShellProtocol
= NULL
;
347 if (gEfiShellParametersProtocol
!= NULL
) {
348 gBS
->CloseProtocol(ImageHandle
,
349 &gEfiShellParametersProtocolGuid
,
352 gEfiShellParametersProtocol
= NULL
;
354 mEfiShellEnvironment2Handle
= NULL
;
356 return (EFI_SUCCESS
);
360 This function causes the shell library to initialize itself. If the shell library
361 is already initialized it will de-initialize all the current protocol poitners and
362 re-populate them again.
364 When the library is used with PcdShellLibAutoInitialize set to true this function
365 will return EFI_SUCCESS and perform no actions.
367 This function is intended for internal access for shell commands only.
369 @retval EFI_SUCCESS the initialization was complete sucessfully
380 // if auto initialize is not false then skip
382 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
383 return (EFI_SUCCESS
);
387 // deinit the current stuff
389 Status
= ShellLibDestructor (gImageHandle
, gST
);
390 ASSERT_EFI_ERROR (Status
);
393 // init the new stuff
395 return (ShellLibConstructorWorker(gImageHandle
, gST
));
399 This function will retrieve the information about the file for the handle
400 specified and store it in allocated pool memory.
402 This function allocates a buffer to store the file's information. It is the
403 caller's responsibility to free the buffer
405 @param FileHandle The file handle of the file for which information is
408 @retval NULL information could not be retrieved.
410 @return the information about the file
415 IN SHELL_FILE_HANDLE FileHandle
418 return (FileFunctionMap
.GetFileInfo(FileHandle
));
422 This function sets the information about the file for the opened handle
425 @param[in] FileHandle The file handle of the file for which information
428 @param[in] FileInfo The information to set.
430 @retval EFI_SUCCESS The information was set.
431 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
432 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
433 @retval EFI_NO_MEDIA The device has no medium.
434 @retval EFI_DEVICE_ERROR The device reported an error.
435 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
436 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
437 @retval EFI_ACCESS_DENIED The file was opened read only.
438 @retval EFI_VOLUME_FULL The volume is full.
443 IN SHELL_FILE_HANDLE FileHandle
,
444 IN EFI_FILE_INFO
*FileInfo
447 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
451 This function will open a file or directory referenced by DevicePath.
453 This function opens a file with the open mode according to the file path. The
454 Attributes is valid only for EFI_FILE_MODE_CREATE.
456 @param FilePath on input the device path to the file. On output
457 the remaining device path.
458 @param DeviceHandle pointer to the system device handle.
459 @param FileHandle pointer to the file handle.
460 @param OpenMode the mode to open the file with.
461 @param Attributes the file's file attributes.
463 @retval EFI_SUCCESS The information was set.
464 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
465 @retval EFI_UNSUPPORTED Could not open the file path.
466 @retval EFI_NOT_FOUND The specified file could not be found on the
467 device or the file system could not be found on
469 @retval EFI_NO_MEDIA The device has no medium.
470 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
471 medium is no longer supported.
472 @retval EFI_DEVICE_ERROR The device reported an error.
473 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
474 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
475 @retval EFI_ACCESS_DENIED The file was opened read only.
476 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
478 @retval EFI_VOLUME_FULL The volume is full.
482 ShellOpenFileByDevicePath(
483 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
484 OUT EFI_HANDLE
*DeviceHandle
,
485 OUT SHELL_FILE_HANDLE
*FileHandle
,
492 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
493 EFI_FILE_PROTOCOL
*Handle1
;
494 EFI_FILE_PROTOCOL
*Handle2
;
495 CHAR16
*FnafPathName
;
498 if (FilePath
== NULL
|| FileHandle
== NULL
|| DeviceHandle
== NULL
) {
499 return (EFI_INVALID_PARAMETER
);
503 // which shell interface should we use
505 if (gEfiShellProtocol
!= NULL
) {
507 // use UEFI Shell 2.0 method.
509 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
510 if (FileName
== NULL
) {
511 return (EFI_INVALID_PARAMETER
);
513 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
520 // use old shell method.
522 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
525 if (EFI_ERROR (Status
)) {
528 Status
= gBS
->OpenProtocol(*DeviceHandle
,
529 &gEfiSimpleFileSystemProtocolGuid
,
530 (VOID
**)&EfiSimpleFileSystemProtocol
,
533 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
534 if (EFI_ERROR (Status
)) {
537 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
538 if (EFI_ERROR (Status
)) {
544 // go down directories one node at a time.
546 while (!IsDevicePathEnd (*FilePath
)) {
548 // For file system access each node should be a file path component
550 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
551 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
554 return (EFI_INVALID_PARAMETER
);
557 // Open this file path node
563 // File Name Alignment Fix (FNAF)
564 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
565 // The structure pointed to by FilePath may be not CHAR16 aligned.
566 // This code copies the potentially unaligned PathName data from the
567 // FilePath structure to the aligned FnafPathName for use in the
568 // calls to Handl2->Open.
572 // Determine length of PathName, in bytes.
574 PathLen
= DevicePathNodeLength (*FilePath
) - SIZE_OF_FILEPATH_DEVICE_PATH
;
577 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
578 // Copy bytes from possibly unaligned location to aligned location
580 FnafPathName
= AllocateCopyPool(PathLen
, (UINT8
*)((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
);
581 if (FnafPathName
== NULL
) {
582 return EFI_OUT_OF_RESOURCES
;
586 // Try to test opening an existing file
588 Status
= Handle2
->Open (
592 OpenMode
&~EFI_FILE_MODE_CREATE
,
597 // see if the error was that it needs to be created
599 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
600 Status
= Handle2
->Open (
610 // Free the alignment buffer
612 FreePool(FnafPathName
);
615 // Close the last node
617 Handle2
->Close (Handle2
);
619 if (EFI_ERROR(Status
)) {
626 *FilePath
= NextDevicePathNode (*FilePath
);
630 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
632 *FileHandle
= (VOID
*)Handle1
;
633 return (EFI_SUCCESS
);
637 This function will open a file or directory referenced by filename.
639 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
640 otherwise, the Filehandle is NULL. The Attributes is valid only for
641 EFI_FILE_MODE_CREATE.
643 if FileName is NULL then ASSERT()
645 @param FileName pointer to file name
646 @param FileHandle pointer to the file handle.
647 @param OpenMode the mode to open the file with.
648 @param Attributes the file's file attributes.
650 @retval EFI_SUCCESS The information was set.
651 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
652 @retval EFI_UNSUPPORTED Could not open the file path.
653 @retval EFI_NOT_FOUND The specified file could not be found on the
654 device or the file system could not be found
656 @retval EFI_NO_MEDIA The device has no medium.
657 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
658 medium is no longer supported.
659 @retval EFI_DEVICE_ERROR The device reported an error.
660 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
661 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
662 @retval EFI_ACCESS_DENIED The file was opened read only.
663 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
665 @retval EFI_VOLUME_FULL The volume is full.
670 IN CONST CHAR16
*FileName
,
671 OUT SHELL_FILE_HANDLE
*FileHandle
,
676 EFI_HANDLE DeviceHandle
;
677 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
679 EFI_FILE_INFO
*FileInfo
;
680 CHAR16
*FileNameCopy
;
684 // ASSERT if FileName is NULL
686 ASSERT(FileName
!= NULL
);
688 if (FileName
== NULL
) {
689 return (EFI_INVALID_PARAMETER
);
692 if (gEfiShellProtocol
!= NULL
) {
693 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
696 // Create only a directory
698 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
699 return ShellCreateDirectory(FileName
, FileHandle
);
703 // Create the directory to create the file in
705 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
706 if (FileName
== NULL
) {
707 return (EFI_OUT_OF_RESOURCES
);
709 PathCleanUpDirectories (FileNameCopy
);
710 if (PathRemoveLastItem (FileNameCopy
)) {
711 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy
, FileHandle
))) {
712 ShellCloseFile (FileHandle
);
715 SHELL_FREE_NON_NULL (FileNameCopy
);
719 // Use UEFI Shell 2.0 method to create the file
721 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
724 if (EFI_ERROR(Status
)) {
728 if (mUnicodeCollationProtocol
== NULL
) {
729 Status
= gBS
->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&mUnicodeCollationProtocol
);
730 if (EFI_ERROR (Status
)) {
731 gEfiShellProtocol
->CloseFile (*FileHandle
);
736 if ((mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NUL") != 0) &&
737 (mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NULL") != 0) &&
738 !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
739 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
740 ASSERT(FileInfo
!= NULL
);
741 FileInfo
->Attribute
= Attributes
;
742 Status2
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
744 if (EFI_ERROR (Status2
)) {
745 gEfiShellProtocol
->CloseFile(*FileHandle
);
752 // Using EFI Shell version
753 // this means convert name to path and call that function
754 // since this will use EFI method again that will open it.
756 ASSERT(mEfiShellEnvironment2
!= NULL
);
757 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
758 if (FilePath
!= NULL
) {
759 return (ShellOpenFileByDevicePath(&FilePath
,
765 return (EFI_DEVICE_ERROR
);
768 This function create a directory
770 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
771 otherwise, the Filehandle is NULL. If the directory already existed, this
772 function opens the existing directory.
774 @param DirectoryName pointer to directory name
775 @param FileHandle pointer to the file handle.
777 @retval EFI_SUCCESS The information was set.
778 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
779 @retval EFI_UNSUPPORTED Could not open the file path.
780 @retval EFI_NOT_FOUND The specified file could not be found on the
781 device or the file system could not be found
783 @retval EFI_NO_MEDIA The device has no medium.
784 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
785 medium is no longer supported.
786 @retval EFI_DEVICE_ERROR The device reported an error.
787 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
788 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
789 @retval EFI_ACCESS_DENIED The file was opened read only.
790 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
792 @retval EFI_VOLUME_FULL The volume is full.
793 @sa ShellOpenFileByName
797 ShellCreateDirectory(
798 IN CONST CHAR16
*DirectoryName
,
799 OUT SHELL_FILE_HANDLE
*FileHandle
802 if (gEfiShellProtocol
!= NULL
) {
804 // Use UEFI Shell 2.0 method
806 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
811 return (ShellOpenFileByName(DirectoryName
,
813 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
820 This function reads information from an opened file.
822 If FileHandle is not a directory, the function reads the requested number of
823 bytes from the file at the file's current position and returns them in Buffer.
824 If the read goes beyond the end of the file, the read length is truncated to the
825 end of the file. The file's current position is increased by the number of bytes
826 returned. If FileHandle is a directory, the function reads the directory entry
827 at the file's current position and returns the entry in Buffer. If the Buffer
828 is not large enough to hold the current directory entry, then
829 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
830 BufferSize is set to be the size of the buffer needed to read the entry. On
831 success, the current position is updated to the next directory entry. If there
832 are no more directory entries, the read returns a zero-length buffer.
833 EFI_FILE_INFO is the structure returned as the directory entry.
835 @param FileHandle the opened file handle
836 @param BufferSize on input the size of buffer in bytes. on return
837 the number of bytes written.
838 @param Buffer the buffer to put read data into.
840 @retval EFI_SUCCESS Data was read.
841 @retval EFI_NO_MEDIA The device has no media.
842 @retval EFI_DEVICE_ERROR The device reported an error.
843 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
844 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
851 IN SHELL_FILE_HANDLE FileHandle
,
852 IN OUT UINTN
*BufferSize
,
856 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
861 Write data to a file.
863 This function writes the specified number of bytes to the file at the current
864 file position. The current file position is advanced the actual number of bytes
865 written, which is returned in BufferSize. Partial writes only occur when there
866 has been a data error during the write attempt (such as "volume space full").
867 The file is automatically grown to hold the data if required. Direct writes to
868 opened directories are not supported.
870 @param FileHandle The opened file for writing
871 @param BufferSize on input the number of bytes in Buffer. On output
872 the number of bytes written.
873 @param Buffer the buffer containing data to write is stored.
875 @retval EFI_SUCCESS Data was written.
876 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
877 @retval EFI_NO_MEDIA The device has no media.
878 @retval EFI_DEVICE_ERROR The device reported an error.
879 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
880 @retval EFI_WRITE_PROTECTED The device is write-protected.
881 @retval EFI_ACCESS_DENIED The file was open for read only.
882 @retval EFI_VOLUME_FULL The volume is full.
887 IN SHELL_FILE_HANDLE FileHandle
,
888 IN OUT UINTN
*BufferSize
,
892 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
896 Close an open file handle.
898 This function closes a specified file handle. All "dirty" cached file data is
899 flushed to the device, and the file is closed. In all cases the handle is
902 @param FileHandle the file handle to close.
904 @retval EFI_SUCCESS the file handle was closed sucessfully.
909 IN SHELL_FILE_HANDLE
*FileHandle
912 return (FileFunctionMap
.CloseFile(*FileHandle
));
916 Delete a file and close the handle
918 This function closes and deletes a file. In all cases the file handle is closed.
919 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
920 returned, but the handle is still closed.
922 @param FileHandle the file handle to delete
924 @retval EFI_SUCCESS the file was closed sucessfully
925 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
927 @retval INVALID_PARAMETER One of the parameters has an invalid value.
932 IN SHELL_FILE_HANDLE
*FileHandle
935 return (FileFunctionMap
.DeleteFile(*FileHandle
));
939 Set the current position in a file.
941 This function sets the current file position for the handle to the position
942 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
943 absolute positioning is supported, and seeking past the end of the file is
944 allowed (a subsequent write would grow the file). Seeking to position
945 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
946 If FileHandle is a directory, the only position that may be set is zero. This
947 has the effect of starting the read process of the directory entries over.
949 @param FileHandle The file handle on which the position is being set
950 @param Position Byte position from begining of file
952 @retval EFI_SUCCESS Operation completed sucessfully.
953 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
955 @retval INVALID_PARAMETER One of the parameters has an invalid value.
959 ShellSetFilePosition (
960 IN SHELL_FILE_HANDLE FileHandle
,
964 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
968 Gets a file's current position
970 This function retrieves the current file position for the file handle. For
971 directories, the current file position has no meaning outside of the file
972 system driver and as such the operation is not supported. An error is returned
973 if FileHandle is a directory.
975 @param FileHandle The open file handle on which to get the position.
976 @param Position Byte position from begining of file.
978 @retval EFI_SUCCESS the operation completed sucessfully.
979 @retval INVALID_PARAMETER One of the parameters has an invalid value.
980 @retval EFI_UNSUPPORTED the request is not valid on directories.
984 ShellGetFilePosition (
985 IN SHELL_FILE_HANDLE FileHandle
,
989 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
992 Flushes data on a file
994 This function flushes all modified data associated with a file to a device.
996 @param FileHandle The file handle on which to flush data
998 @retval EFI_SUCCESS The data was flushed.
999 @retval EFI_NO_MEDIA The device has no media.
1000 @retval EFI_DEVICE_ERROR The device reported an error.
1001 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1002 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1003 @retval EFI_ACCESS_DENIED The file was opened for read only.
1008 IN SHELL_FILE_HANDLE FileHandle
1011 return (FileFunctionMap
.FlushFile(FileHandle
));
1014 /** Retrieve first entry from a directory.
1016 This function takes an open directory handle and gets information from the
1017 first entry in the directory. A buffer is allocated to contain
1018 the information and a pointer to the buffer is returned in *Buffer. The
1019 caller can use ShellFindNextFile() to get subsequent directory entries.
1021 The buffer will be freed by ShellFindNextFile() when the last directory
1022 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1023 when finished with it.
1025 @param[in] DirHandle The file handle of the directory to search.
1026 @param[out] Buffer The pointer to the buffer for the file's information.
1028 @retval EFI_SUCCESS Found the first file.
1029 @retval EFI_NOT_FOUND Cannot find the directory.
1030 @retval EFI_NO_MEDIA The device has no media.
1031 @retval EFI_DEVICE_ERROR The device reported an error.
1032 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1033 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1038 ShellFindFirstFile (
1039 IN SHELL_FILE_HANDLE DirHandle
,
1040 OUT EFI_FILE_INFO
**Buffer
1044 // pass to file handle lib
1046 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1048 /** Retrieve next entries from a directory.
1050 To use this function, the caller must first call the ShellFindFirstFile()
1051 function to get the first directory entry. Subsequent directory entries are
1052 retrieved by using the ShellFindNextFile() function. This function can
1053 be called several times to get each entry from the directory. If the call of
1054 ShellFindNextFile() retrieved the last directory entry, the next call of
1055 this function will set *NoFile to TRUE and free the buffer.
1057 @param[in] DirHandle The file handle of the directory.
1058 @param[out] Buffer The pointer to buffer for file's information.
1059 @param[out] NoFile The pointer to boolean when last file is found.
1061 @retval EFI_SUCCESS Found the next file, or reached last file
1062 @retval EFI_NO_MEDIA The device has no media.
1063 @retval EFI_DEVICE_ERROR The device reported an error.
1064 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1069 IN SHELL_FILE_HANDLE DirHandle
,
1070 OUT EFI_FILE_INFO
*Buffer
,
1075 // pass to file handle lib
1077 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1080 Retrieve the size of a file.
1082 if FileHandle is NULL then ASSERT()
1083 if Size is NULL then ASSERT()
1085 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1088 @param FileHandle file handle from which size is retrieved
1089 @param Size pointer to size
1091 @retval EFI_SUCCESS operation was completed sucessfully
1092 @retval EFI_DEVICE_ERROR cannot access the file
1097 IN SHELL_FILE_HANDLE FileHandle
,
1101 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1104 Retrieves the status of the break execution flag
1106 this function is useful to check whether the application is being asked to halt by the shell.
1108 @retval TRUE the execution break is enabled
1109 @retval FALSE the execution break is not enabled
1113 ShellGetExecutionBreakFlag(
1118 // Check for UEFI Shell 2.0 protocols
1120 if (gEfiShellProtocol
!= NULL
) {
1123 // We are using UEFI Shell 2.0; see if the event has been triggered
1125 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1132 // using EFI Shell; call the function to check
1134 if (mEfiShellEnvironment2
!= NULL
) {
1135 return (mEfiShellEnvironment2
->GetExecutionBreak());
1141 return the value of an environment variable
1143 this function gets the value of the environment variable set by the
1144 ShellSetEnvironmentVariable function
1146 @param EnvKey The key name of the environment variable.
1148 @retval NULL the named environment variable does not exist.
1149 @return != NULL pointer to the value of the environment variable
1153 ShellGetEnvironmentVariable (
1154 IN CONST CHAR16
*EnvKey
1158 // Check for UEFI Shell 2.0 protocols
1160 if (gEfiShellProtocol
!= NULL
) {
1161 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1165 // Check for EFI shell
1167 if (mEfiShellEnvironment2
!= NULL
) {
1168 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1174 set the value of an environment variable
1176 This function changes the current value of the specified environment variable. If the
1177 environment variable exists and the Value is an empty string, then the environment
1178 variable is deleted. If the environment variable exists and the Value is not an empty
1179 string, then the value of the environment variable is changed. If the environment
1180 variable does not exist and the Value is an empty string, there is no action. If the
1181 environment variable does not exist and the Value is a non-empty string, then the
1182 environment variable is created and assigned the specified value.
1184 This is not supported pre-UEFI Shell 2.0.
1186 @param EnvKey The key name of the environment variable.
1187 @param EnvVal The Value of the environment variable
1188 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1190 @retval EFI_SUCCESS the operation was completed sucessfully
1191 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1195 ShellSetEnvironmentVariable (
1196 IN CONST CHAR16
*EnvKey
,
1197 IN CONST CHAR16
*EnvVal
,
1202 // Check for UEFI Shell 2.0 protocols
1204 if (gEfiShellProtocol
!= NULL
) {
1205 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1209 // This feature does not exist under EFI shell
1211 return (EFI_UNSUPPORTED
);
1215 Cause the shell to parse and execute a command line.
1217 This function creates a nested instance of the shell and executes the specified
1218 command (CommandLine) with the specified environment (Environment). Upon return,
1219 the status code returned by the specified command is placed in StatusCode.
1220 If Environment is NULL, then the current environment is used and all changes made
1221 by the commands executed will be reflected in the current environment. If the
1222 Environment is non-NULL, then the changes made will be discarded.
1223 The CommandLine is executed from the current working directory on the current
1226 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1227 environment. The values pointed to by the parameters will be unchanged by the
1228 ShellExecute() function. The Output parameter has no effect in a
1229 UEFI Shell 2.0 environment.
1231 @param[in] ParentHandle The parent image starting the operation.
1232 @param[in] CommandLine The pointer to a NULL terminated command line.
1233 @param[in] Output True to display debug output. False to hide it.
1234 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1235 in the form "x=y". If NULL, the current set is used.
1236 @param[out] Status The status of the run command line.
1238 @retval EFI_SUCCESS The operation completed sucessfully. Status
1239 contains the status code returned.
1240 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1241 @retval EFI_OUT_OF_RESOURCES Out of resources.
1242 @retval EFI_UNSUPPORTED The operation is not allowed.
1247 IN EFI_HANDLE
*ParentHandle
,
1248 IN CHAR16
*CommandLine OPTIONAL
,
1249 IN BOOLEAN Output OPTIONAL
,
1250 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1251 OUT EFI_STATUS
*Status OPTIONAL
1254 EFI_STATUS CmdStatus
;
1256 // Check for UEFI Shell 2.0 protocols
1258 if (gEfiShellProtocol
!= NULL
) {
1260 // Call UEFI Shell 2.0 version (not using Output parameter)
1262 return (gEfiShellProtocol
->Execute(ParentHandle
,
1264 EnvironmentVariables
,
1269 // Check for EFI shell
1271 if (mEfiShellEnvironment2
!= NULL
) {
1273 // Call EFI Shell version.
1274 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1276 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1280 // No Status output parameter so just use the returned status
1282 if (Status
!= NULL
) {
1283 *Status
= CmdStatus
;
1286 // If there was an error, we can't tell if it was from the command or from
1287 // the Execute() function, so we'll just assume the shell ran successfully
1288 // and the error came from the command.
1293 return (EFI_UNSUPPORTED
);
1297 Retreives the current directory path
1299 If the DeviceName is NULL, it returns the current device's current directory
1300 name. If the DeviceName is not NULL, it returns the current directory name
1303 Note that the current directory string should exclude the tailing backslash character.
1305 @param DeviceName the name of the drive to get directory on
1307 @retval NULL the directory does not exist
1308 @return != NULL the directory
1312 ShellGetCurrentDir (
1313 IN CHAR16
* CONST DeviceName OPTIONAL
1317 // Check for UEFI Shell 2.0 protocols
1319 if (gEfiShellProtocol
!= NULL
) {
1320 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1324 // Check for EFI shell
1326 if (mEfiShellEnvironment2
!= NULL
) {
1327 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1333 sets (enabled or disabled) the page break mode
1335 when page break mode is enabled the screen will stop scrolling
1336 and wait for operator input before scrolling a subsequent screen.
1338 @param CurrentState TRUE to enable and FALSE to disable
1342 ShellSetPageBreakMode (
1343 IN BOOLEAN CurrentState
1347 // check for enabling
1349 if (CurrentState
!= 0x00) {
1351 // check for UEFI Shell 2.0
1353 if (gEfiShellProtocol
!= NULL
) {
1355 // Enable with UEFI 2.0 Shell
1357 gEfiShellProtocol
->EnablePageBreak();
1361 // Check for EFI shell
1363 if (mEfiShellEnvironment2
!= NULL
) {
1365 // Enable with EFI Shell
1367 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1373 // check for UEFI Shell 2.0
1375 if (gEfiShellProtocol
!= NULL
) {
1377 // Disable with UEFI 2.0 Shell
1379 gEfiShellProtocol
->DisablePageBreak();
1383 // Check for EFI shell
1385 if (mEfiShellEnvironment2
!= NULL
) {
1387 // Disable with EFI Shell
1389 mEfiShellEnvironment2
->DisablePageBreak ();
1397 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1398 /// This allows for the struct to be populated.
1405 SHELL_FILE_HANDLE Handle
;
1406 EFI_FILE_INFO
*Info
;
1407 } EFI_SHELL_FILE_INFO_NO_CONST
;
1410 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1412 if OldStyleFileList is NULL then ASSERT()
1414 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1415 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1416 the ShellCloseFileMetaArg function.
1418 @param[in] FileList the EFI shell list type
1419 @param[in, out] ListHead the list to add to
1421 @retval the resultant head of the double linked new format list;
1424 InternalShellConvertFileListType (
1425 IN LIST_ENTRY
*FileList
,
1426 IN OUT LIST_ENTRY
*ListHead
1429 SHELL_FILE_ARG
*OldInfo
;
1431 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1436 ASSERT(FileList
!= NULL
);
1437 ASSERT(ListHead
!= NULL
);
1440 // enumerate through each member of the old list and copy
1442 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1443 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1444 ASSERT(OldInfo
!= NULL
);
1447 // Skip ones that failed to open...
1449 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1454 // make sure the old list was valid
1456 ASSERT(OldInfo
->Info
!= NULL
);
1457 ASSERT(OldInfo
->FullName
!= NULL
);
1458 ASSERT(OldInfo
->FileName
!= NULL
);
1461 // allocate a new EFI_SHELL_FILE_INFO object
1463 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1464 if (NewInfo
== NULL
) {
1465 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1471 // copy the simple items
1473 NewInfo
->Handle
= OldInfo
->Handle
;
1474 NewInfo
->Status
= OldInfo
->Status
;
1476 // old shell checks for 0 not NULL
1477 OldInfo
->Handle
= 0;
1480 // allocate new space to copy strings and structure
1482 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1483 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1484 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1487 // make sure all the memory allocations were sucessful
1489 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1491 // Free the partially allocated new node
1493 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1494 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1495 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1496 SHELL_FREE_NON_NULL(NewInfo
);
1499 // Free the previously converted stuff
1501 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1507 // add that to the list
1509 InsertTailList(ListHead
, &NewInfo
->Link
);
1514 Opens a group of files based on a path.
1516 This function uses the Arg to open all the matching files. Each matched
1517 file has a SHELL_FILE_INFO structure to record the file information. These
1518 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1519 structures from ListHead to access each file. This function supports wildcards
1520 and will process '?' and '*' as such. the list must be freed with a call to
1521 ShellCloseFileMetaArg().
1523 If you are NOT appending to an existing list *ListHead must be NULL. If
1524 *ListHead is NULL then it must be callee freed.
1526 @param Arg pointer to path string
1527 @param OpenMode mode to open files with
1528 @param ListHead head of linked list of results
1530 @retval EFI_SUCCESS the operation was sucessful and the list head
1531 contains the list of opened files
1532 @return != EFI_SUCCESS the operation failed
1534 @sa InternalShellConvertFileListType
1538 ShellOpenFileMetaArg (
1541 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1545 LIST_ENTRY mOldStyleFileList
;
1546 CHAR16
*CleanFilePathStr
;
1549 // ASSERT that Arg and ListHead are not NULL
1551 ASSERT(Arg
!= NULL
);
1552 ASSERT(ListHead
!= NULL
);
1554 CleanFilePathStr
= NULL
;
1556 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1557 if (EFI_ERROR (Status
)) {
1562 // Check for UEFI Shell 2.0 protocols
1564 if (gEfiShellProtocol
!= NULL
) {
1565 if (*ListHead
== NULL
) {
1566 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1567 if (*ListHead
== NULL
) {
1568 FreePool(CleanFilePathStr
);
1569 return (EFI_OUT_OF_RESOURCES
);
1571 InitializeListHead(&((*ListHead
)->Link
));
1573 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1576 if (EFI_ERROR(Status
)) {
1577 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1579 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1581 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1582 FreePool(*ListHead
);
1583 FreePool(CleanFilePathStr
);
1585 return (EFI_NOT_FOUND
);
1587 FreePool(CleanFilePathStr
);
1592 // Check for EFI shell
1594 if (mEfiShellEnvironment2
!= NULL
) {
1596 // make sure the list head is initialized
1598 InitializeListHead(&mOldStyleFileList
);
1601 // Get the EFI Shell list of files
1603 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1604 if (EFI_ERROR(Status
)) {
1606 FreePool(CleanFilePathStr
);
1610 if (*ListHead
== NULL
) {
1611 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1612 if (*ListHead
== NULL
) {
1613 FreePool(CleanFilePathStr
);
1614 return (EFI_OUT_OF_RESOURCES
);
1616 InitializeListHead(&((*ListHead
)->Link
));
1620 // Convert that to equivalent of UEFI Shell 2.0 structure
1622 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1625 // Free the EFI Shell version that was converted.
1627 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1629 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1630 FreePool(*ListHead
);
1632 Status
= EFI_NOT_FOUND
;
1634 FreePool(CleanFilePathStr
);
1638 FreePool(CleanFilePathStr
);
1639 return (EFI_UNSUPPORTED
);
1642 Free the linked list returned from ShellOpenFileMetaArg.
1644 if ListHead is NULL then ASSERT().
1646 @param ListHead the pointer to free.
1648 @retval EFI_SUCCESS the operation was sucessful.
1652 ShellCloseFileMetaArg (
1653 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1659 // ASSERT that ListHead is not NULL
1661 ASSERT(ListHead
!= NULL
);
1664 // Check for UEFI Shell 2.0 protocols
1666 if (gEfiShellProtocol
!= NULL
) {
1667 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1668 } else if (mEfiShellEnvironment2
!= NULL
) {
1670 // Since this is EFI Shell version we need to free our internally made copy
1673 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1674 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1675 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1676 RemoveEntryList(Node
);
1677 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1678 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1679 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1680 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1681 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1683 SHELL_FREE_NON_NULL(*ListHead
);
1687 return (EFI_UNSUPPORTED
);
1691 Find a file by searching the CWD and then the path.
1693 If FileName is NULL then ASSERT.
1695 If the return value is not NULL then the memory must be caller freed.
1697 @param FileName Filename string.
1699 @retval NULL the file was not found
1700 @return !NULL the full path to the file.
1705 IN CONST CHAR16
*FileName
1709 SHELL_FILE_HANDLE Handle
;
1713 CONST CHAR16
*Walker
;
1720 // First make sure its not an absolute path.
1722 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1723 if (!EFI_ERROR(Status
)){
1724 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1725 ASSERT(RetVal
== NULL
);
1726 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1727 ShellCloseFile(&Handle
);
1730 ShellCloseFile(&Handle
);
1734 Path
= ShellGetEnvironmentVariable(L
"cwd");
1736 Size
= StrSize(Path
) + sizeof(CHAR16
);
1737 Size
+= StrSize(FileName
);
1738 TestPath
= AllocateZeroPool(Size
);
1739 if (TestPath
== NULL
) {
1742 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), Path
);
1743 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1744 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1745 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1746 if (!EFI_ERROR(Status
)){
1747 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1748 ASSERT(RetVal
== NULL
);
1749 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1750 ShellCloseFile(&Handle
);
1754 ShellCloseFile(&Handle
);
1759 Path
= ShellGetEnvironmentVariable(L
"path");
1761 Size
= StrSize(Path
)+sizeof(CHAR16
);
1762 Size
+= StrSize(FileName
);
1763 TestPath
= AllocateZeroPool(Size
);
1764 if (TestPath
== NULL
) {
1767 Walker
= (CHAR16
*)Path
;
1769 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1770 if (TestPath
!= NULL
) {
1771 TempChar
= StrStr(TestPath
, L
";");
1772 if (TempChar
!= NULL
) {
1773 *TempChar
= CHAR_NULL
;
1775 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1776 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1778 if (FileName
[0] == L
'\\') {
1781 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1782 if (StrStr(Walker
, L
";") != NULL
) {
1783 Walker
= StrStr(Walker
, L
";") + 1;
1787 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1788 if (!EFI_ERROR(Status
)){
1789 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1790 ASSERT(RetVal
== NULL
);
1791 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1792 ShellCloseFile(&Handle
);
1795 ShellCloseFile(&Handle
);
1799 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1806 Find a file by searching the CWD and then the path with a variable set of file
1807 extensions. If the file is not found it will append each extension in the list
1808 in the order provided and return the first one that is successful.
1810 If FileName is NULL, then ASSERT.
1811 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1813 If the return value is not NULL then the memory must be caller freed.
1815 @param[in] FileName Filename string.
1816 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1818 @retval NULL The file was not found.
1819 @retval !NULL The path to the file.
1823 ShellFindFilePathEx (
1824 IN CONST CHAR16
*FileName
,
1825 IN CONST CHAR16
*FileExtension
1830 CONST CHAR16
*ExtensionWalker
;
1835 ASSERT(FileName
!= NULL
);
1836 if (FileExtension
== NULL
) {
1837 return (ShellFindFilePath(FileName
));
1839 RetVal
= ShellFindFilePath(FileName
);
1840 if (RetVal
!= NULL
) {
1843 Size
= StrSize(FileName
);
1844 Size
+= StrSize(FileExtension
);
1845 TestPath
= AllocateZeroPool(Size
);
1846 if (TestPath
== NULL
) {
1849 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1850 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1851 if (ExtensionWalker
!= NULL
) {
1852 StrCatS(TestPath
, Size
/sizeof(CHAR16
), ExtensionWalker
);
1854 TempChar
= StrStr(TestPath
, L
";");
1855 if (TempChar
!= NULL
) {
1856 *TempChar
= CHAR_NULL
;
1858 RetVal
= ShellFindFilePath(TestPath
);
1859 if (RetVal
!= NULL
) {
1862 ASSERT(ExtensionWalker
!= NULL
);
1863 TempChar2
= StrStr(ExtensionWalker
, L
";");
1872 SHELL_PARAM_TYPE Type
;
1874 UINTN OriginalPosition
;
1875 } SHELL_PARAM_PACKAGE
;
1878 Checks the list of valid arguments and returns TRUE if the item was found. If the
1879 return value is TRUE then the type parameter is set also.
1881 if CheckList is NULL then ASSERT();
1882 if Name is NULL then ASSERT();
1883 if Type is NULL then ASSERT();
1885 @param Name pointer to Name of parameter found
1886 @param CheckList List to check against
1887 @param Type pointer to type of parameter if it was found
1889 @retval TRUE the Parameter was found. Type is valid.
1890 @retval FALSE the Parameter was not found. Type is not valid.
1893 InternalIsOnCheckList (
1894 IN CONST CHAR16
*Name
,
1895 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1896 OUT SHELL_PARAM_TYPE
*Type
1899 SHELL_PARAM_ITEM
*TempListItem
;
1903 // ASSERT that all 3 pointer parameters aren't NULL
1905 ASSERT(CheckList
!= NULL
);
1906 ASSERT(Type
!= NULL
);
1907 ASSERT(Name
!= NULL
);
1910 // question mark and page break mode are always supported
1912 if ((StrCmp(Name
, L
"-?") == 0) ||
1913 (StrCmp(Name
, L
"-b") == 0)
1920 // Enumerate through the list
1922 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1924 // If the Type is TypeStart only check the first characters of the passed in param
1925 // If it matches set the type and return TRUE
1927 if (TempListItem
->Type
== TypeStart
) {
1928 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1929 *Type
= TempListItem
->Type
;
1933 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1934 if (TempString
!= NULL
) {
1935 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1936 *Type
= TempListItem
->Type
;
1937 FreePool(TempString
);
1940 FreePool(TempString
);
1942 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1943 *Type
= TempListItem
->Type
;
1951 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1953 @param[in] Name pointer to Name of parameter found
1954 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1955 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1957 @retval TRUE the Parameter is a flag.
1958 @retval FALSE the Parameter not a flag.
1962 IN CONST CHAR16
*Name
,
1963 IN CONST BOOLEAN AlwaysAllowNumbers
,
1964 IN CONST BOOLEAN TimeNumbers
1968 // ASSERT that Name isn't NULL
1970 ASSERT(Name
!= NULL
);
1973 // If we accept numbers then dont return TRUE. (they will be values)
1975 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
1980 // If the Name has a /, +, or - as the first character return TRUE
1982 if ((Name
[0] == L
'/') ||
1983 (Name
[0] == L
'-') ||
1992 Checks the command line arguments passed against the list of valid ones.
1994 If no initialization is required, then return RETURN_SUCCESS.
1996 @param[in] CheckList pointer to list of parameters to check
1997 @param[out] CheckPackage pointer to pointer to list checked values
1998 @param[out] ProblemParam optional pointer to pointer to unicode string for
1999 the paramater that caused failure. If used then the
2000 caller is responsible for freeing the memory.
2001 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2002 @param[in] Argv pointer to array of parameters
2003 @param[in] Argc Count of parameters in Argv
2004 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
2006 @retval EFI_SUCCESS The operation completed sucessfully.
2007 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2008 @retval EFI_INVALID_PARAMETER A parameter was invalid
2009 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2010 duplicated. the duplicated command line argument
2011 was returned in ProblemParam if provided.
2012 @retval EFI_NOT_FOUND a argument required a value that was missing.
2013 the invalid command line argument was returned in
2014 ProblemParam if provided.
2017 InternalCommandLineParse (
2018 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2019 OUT LIST_ENTRY
**CheckPackage
,
2020 OUT CHAR16
**ProblemParam OPTIONAL
,
2021 IN BOOLEAN AutoPageBreak
,
2022 IN CONST CHAR16
**Argv
,
2024 IN BOOLEAN AlwaysAllowNumbers
2028 SHELL_PARAM_TYPE CurrentItemType
;
2029 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2033 CONST CHAR16
*TempPointer
;
2034 UINTN CurrentValueSize
;
2037 CurrentItemPackage
= NULL
;
2043 // If there is only 1 item we dont need to do anything
2046 *CheckPackage
= NULL
;
2047 return (EFI_SUCCESS
);
2053 ASSERT(CheckList
!= NULL
);
2054 ASSERT(Argv
!= NULL
);
2057 // initialize the linked list
2059 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2060 if (*CheckPackage
== NULL
) {
2061 return (EFI_OUT_OF_RESOURCES
);
2064 InitializeListHead(*CheckPackage
);
2067 // loop through each of the arguments
2069 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2070 if (Argv
[LoopCounter
] == NULL
) {
2072 // do nothing for NULL argv
2074 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2076 // We might have leftover if last parameter didnt have optional value
2078 if (GetItemValue
!= 0) {
2080 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2085 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2086 if (CurrentItemPackage
== NULL
) {
2087 ShellCommandLineFreeVarList(*CheckPackage
);
2088 *CheckPackage
= NULL
;
2089 return (EFI_OUT_OF_RESOURCES
);
2091 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2092 if (CurrentItemPackage
->Name
== NULL
) {
2093 ShellCommandLineFreeVarList(*CheckPackage
);
2094 *CheckPackage
= NULL
;
2095 return (EFI_OUT_OF_RESOURCES
);
2097 CurrentItemPackage
->Type
= CurrentItemType
;
2098 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2099 CurrentItemPackage
->Value
= NULL
;
2102 // Does this flag require a value
2104 switch (CurrentItemPackage
->Type
) {
2106 // possibly trigger the next loop(s) to populate the value of this item
2113 case TypeDoubleValue
:
2118 GetItemValue
= (UINTN
)(-1);
2123 // this item has no value expected; we are done
2125 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2126 ASSERT(GetItemValue
== 0);
2129 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, (BOOLEAN
)(CurrentItemPackage
->Type
== TypeTimeValue
))) {
2131 // get the item VALUE for a previous flag
2133 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2134 NewValue
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2135 if (NewValue
== NULL
) {
2136 SHELL_FREE_NON_NULL (CurrentItemPackage
->Value
);
2137 SHELL_FREE_NON_NULL (CurrentItemPackage
);
2138 ShellCommandLineFreeVarList (*CheckPackage
);
2139 *CheckPackage
= NULL
;
2140 return EFI_OUT_OF_RESOURCES
;
2142 CurrentItemPackage
->Value
= NewValue
;
2143 if (ValueSize
== 0) {
2144 StrCpyS( CurrentItemPackage
->Value
,
2145 CurrentValueSize
/sizeof(CHAR16
),
2149 StrCatS( CurrentItemPackage
->Value
,
2150 CurrentValueSize
/sizeof(CHAR16
),
2153 StrCatS( CurrentItemPackage
->Value
,
2154 CurrentValueSize
/sizeof(CHAR16
),
2158 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2161 if (GetItemValue
== 0) {
2162 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2164 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2166 // add this one as a non-flag
2169 TempPointer
= Argv
[LoopCounter
];
2170 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2171 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2172 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2176 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2177 if (CurrentItemPackage
== NULL
) {
2178 ShellCommandLineFreeVarList(*CheckPackage
);
2179 *CheckPackage
= NULL
;
2180 return (EFI_OUT_OF_RESOURCES
);
2182 CurrentItemPackage
->Name
= NULL
;
2183 CurrentItemPackage
->Type
= TypePosition
;
2184 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2185 if (CurrentItemPackage
->Value
== NULL
) {
2186 ShellCommandLineFreeVarList(*CheckPackage
);
2187 *CheckPackage
= NULL
;
2188 return (EFI_OUT_OF_RESOURCES
);
2190 CurrentItemPackage
->OriginalPosition
= Count
++;
2191 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2194 // this was a non-recognised flag... error!
2196 if (ProblemParam
!= NULL
) {
2197 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2199 ShellCommandLineFreeVarList(*CheckPackage
);
2200 *CheckPackage
= NULL
;
2201 return (EFI_VOLUME_CORRUPTED
);
2204 if (GetItemValue
!= 0) {
2206 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2209 // support for AutoPageBreak
2211 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2212 ShellSetPageBreakMode(TRUE
);
2214 return (EFI_SUCCESS
);
2218 Checks the command line arguments passed against the list of valid ones.
2219 Optionally removes NULL values first.
2221 If no initialization is required, then return RETURN_SUCCESS.
2223 @param[in] CheckList The pointer to list of parameters to check.
2224 @param[out] CheckPackage The package of checked values.
2225 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2226 the paramater that caused failure.
2227 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2228 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2230 @retval EFI_SUCCESS The operation completed sucessfully.
2231 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2232 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2233 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2234 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2235 of the command line arguments was returned in
2236 ProblemParam if provided.
2237 @retval EFI_NOT_FOUND A argument required a value that was missing.
2238 The invalid command line argument was returned in
2239 ProblemParam if provided.
2243 ShellCommandLineParseEx (
2244 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2245 OUT LIST_ENTRY
**CheckPackage
,
2246 OUT CHAR16
**ProblemParam OPTIONAL
,
2247 IN BOOLEAN AutoPageBreak
,
2248 IN BOOLEAN AlwaysAllowNumbers
2252 // ASSERT that CheckList and CheckPackage aren't NULL
2254 ASSERT(CheckList
!= NULL
);
2255 ASSERT(CheckPackage
!= NULL
);
2258 // Check for UEFI Shell 2.0 protocols
2260 if (gEfiShellParametersProtocol
!= NULL
) {
2261 return (InternalCommandLineParse(CheckList
,
2265 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2266 gEfiShellParametersProtocol
->Argc
,
2267 AlwaysAllowNumbers
));
2271 // ASSERT That EFI Shell is not required
2273 ASSERT (mEfiShellInterface
!= NULL
);
2274 return (InternalCommandLineParse(CheckList
,
2278 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2279 mEfiShellInterface
->Argc
,
2280 AlwaysAllowNumbers
));
2284 Frees shell variable list that was returned from ShellCommandLineParse.
2286 This function will free all the memory that was used for the CheckPackage
2287 list of postprocessed shell arguments.
2289 this function has no return value.
2291 if CheckPackage is NULL, then return
2293 @param CheckPackage the list to de-allocate
2297 ShellCommandLineFreeVarList (
2298 IN LIST_ENTRY
*CheckPackage
2304 // check for CheckPackage == NULL
2306 if (CheckPackage
== NULL
) {
2311 // for each node in the list
2313 for ( Node
= GetFirstNode(CheckPackage
)
2314 ; !IsListEmpty(CheckPackage
)
2315 ; Node
= GetFirstNode(CheckPackage
)
2318 // Remove it from the list
2320 RemoveEntryList(Node
);
2323 // if it has a name free the name
2325 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2326 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2330 // if it has a value free the value
2332 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2333 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2337 // free the node structure
2339 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2342 // free the list head node
2344 FreePool(CheckPackage
);
2347 Checks for presence of a flag parameter
2349 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2351 if CheckPackage is NULL then return FALSE.
2352 if KeyString is NULL then ASSERT()
2354 @param CheckPackage The package of parsed command line arguments
2355 @param KeyString the Key of the command line argument to check for
2357 @retval TRUE the flag is on the command line
2358 @retval FALSE the flag is not on the command line
2362 ShellCommandLineGetFlag (
2363 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2364 IN CONST CHAR16
* CONST KeyString
2371 // return FALSE for no package or KeyString is NULL
2373 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2378 // enumerate through the list of parametrs
2380 for ( Node
= GetFirstNode(CheckPackage
)
2381 ; !IsNull (CheckPackage
, Node
)
2382 ; Node
= GetNextNode(CheckPackage
, Node
)
2385 // If the Name matches, return TRUE (and there may be NULL name)
2387 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2389 // If Type is TypeStart then only compare the begining of the strings
2391 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2392 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2396 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2397 if (TempString
!= NULL
) {
2398 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2399 FreePool(TempString
);
2402 FreePool(TempString
);
2404 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2412 Returns value from command line argument.
2414 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2416 If CheckPackage is NULL, then return NULL.
2418 @param[in] CheckPackage The package of parsed command line arguments.
2419 @param[in] KeyString The Key of the command line argument to check for.
2421 @retval NULL The flag is not on the command line.
2422 @retval !=NULL The pointer to unicode string of the value.
2426 ShellCommandLineGetValue (
2427 IN CONST LIST_ENTRY
*CheckPackage
,
2428 IN CHAR16
*KeyString
2435 // return NULL for no package or KeyString is NULL
2437 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2442 // enumerate through the list of parametrs
2444 for ( Node
= GetFirstNode(CheckPackage
)
2445 ; !IsNull (CheckPackage
, Node
)
2446 ; Node
= GetNextNode(CheckPackage
, Node
)
2449 // If the Name matches, return TRUE (and there may be NULL name)
2451 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2453 // If Type is TypeStart then only compare the begining of the strings
2455 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2456 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2457 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2460 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2461 if (TempString
!= NULL
) {
2462 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2463 FreePool(TempString
);
2464 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2466 FreePool(TempString
);
2468 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2469 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2477 Returns raw value from command line argument.
2479 Raw value parameters are in the form of "value" in a specific position in the list.
2481 If CheckPackage is NULL, then return NULL.
2483 @param[in] CheckPackage The package of parsed command line arguments.
2484 @param[in] Position The position of the value.
2486 @retval NULL The flag is not on the command line.
2487 @retval !=NULL The pointer to unicode string of the value.
2491 ShellCommandLineGetRawValue (
2492 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2499 // check for CheckPackage == NULL
2501 if (CheckPackage
== NULL
) {
2506 // enumerate through the list of parametrs
2508 for ( Node
= GetFirstNode(CheckPackage
)
2509 ; !IsNull (CheckPackage
, Node
)
2510 ; Node
= GetNextNode(CheckPackage
, Node
)
2513 // If the position matches, return the value
2515 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2516 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2523 returns the number of command line value parameters that were parsed.
2525 this will not include flags.
2527 @param[in] CheckPackage The package of parsed command line arguments.
2529 @retval (UINTN)-1 No parsing has ocurred
2530 @return other The number of value parameters found
2534 ShellCommandLineGetCount(
2535 IN CONST LIST_ENTRY
*CheckPackage
2541 if (CheckPackage
== NULL
) {
2544 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2545 ; !IsNull (CheckPackage
, Node1
)
2546 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2548 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2556 Determines if a parameter is duplicated.
2558 If Param is not NULL then it will point to a callee allocated string buffer
2559 with the parameter value if a duplicate is found.
2561 If CheckPackage is NULL, then ASSERT.
2563 @param[in] CheckPackage The package of parsed command line arguments.
2564 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2566 @retval EFI_SUCCESS No parameters were duplicated.
2567 @retval EFI_DEVICE_ERROR A duplicate was found.
2571 ShellCommandLineCheckDuplicate (
2572 IN CONST LIST_ENTRY
*CheckPackage
,
2579 ASSERT(CheckPackage
!= NULL
);
2581 for ( Node1
= GetFirstNode(CheckPackage
)
2582 ; !IsNull (CheckPackage
, Node1
)
2583 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2585 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2586 ; !IsNull (CheckPackage
, Node2
)
2587 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2589 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) {
2590 if (Param
!= NULL
) {
2592 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2594 return (EFI_DEVICE_ERROR
);
2598 return (EFI_SUCCESS
);
2602 This is a find and replace function. Upon successful return the NewString is a copy of
2603 SourceString with each instance of FindTarget replaced with ReplaceWith.
2605 If SourceString and NewString overlap the behavior is undefined.
2607 If the string would grow bigger than NewSize it will halt and return error.
2609 @param[in] SourceString The string with source buffer.
2610 @param[in, out] NewString The string with resultant buffer.
2611 @param[in] NewSize The size in bytes of NewString.
2612 @param[in] FindTarget The string to look for.
2613 @param[in] ReplaceWith The string to replace FindTarget with.
2614 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2615 immediately before it.
2616 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2618 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2619 @retval EFI_INVALID_PARAMETER NewString was NULL.
2620 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2621 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2622 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2623 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2624 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2625 the new string (truncation occurred).
2626 @retval EFI_SUCCESS The string was successfully copied with replacement.
2630 ShellCopySearchAndReplace(
2631 IN CHAR16 CONST
*SourceString
,
2632 IN OUT CHAR16
*NewString
,
2634 IN CONST CHAR16
*FindTarget
,
2635 IN CONST CHAR16
*ReplaceWith
,
2636 IN CONST BOOLEAN SkipPreCarrot
,
2637 IN CONST BOOLEAN ParameterReplacing
2643 if ( (SourceString
== NULL
)
2644 || (NewString
== NULL
)
2645 || (FindTarget
== NULL
)
2646 || (ReplaceWith
== NULL
)
2647 || (StrLen(FindTarget
) < 1)
2648 || (StrLen(SourceString
) < 1)
2650 return (EFI_INVALID_PARAMETER
);
2653 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2654 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2656 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2657 if (Replace
!= NULL
) {
2658 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2661 if (Replace
== NULL
) {
2662 return (EFI_OUT_OF_RESOURCES
);
2664 NewString
= ZeroMem(NewString
, NewSize
);
2665 while (*SourceString
!= CHAR_NULL
) {
2667 // if we find the FindTarget and either Skip == FALSE or Skip and we
2668 // dont have a carrot do a replace...
2670 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2671 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2673 SourceString
+= StrLen(FindTarget
);
2674 Size
= StrSize(NewString
);
2675 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2677 return (EFI_BUFFER_TOO_SMALL
);
2679 StrCatS(NewString
, NewSize
/sizeof(CHAR16
), Replace
);
2681 Size
= StrSize(NewString
);
2682 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2684 return (EFI_BUFFER_TOO_SMALL
);
2686 StrnCatS(NewString
, NewSize
/sizeof(CHAR16
), SourceString
, 1);
2691 return (EFI_SUCCESS
);
2695 Internal worker function to output a string.
2697 This function will output a string to the correct StdOut.
2699 @param[in] String The string to print out.
2701 @retval EFI_SUCCESS The operation was sucessful.
2702 @retval !EFI_SUCCESS The operation failed.
2706 IN CONST CHAR16
*String
2710 Size
= StrSize(String
) - sizeof(CHAR16
);
2712 return (EFI_SUCCESS
);
2714 if (gEfiShellParametersProtocol
!= NULL
) {
2715 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2717 if (mEfiShellInterface
!= NULL
) {
2718 if (mEfiShellInterface
->RedirArgc
== 0) {
2720 // Divide in half for old shell. Must be string length not size.
2722 Size
/=2; // Divide in half only when no redirection.
2724 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2727 return (EFI_UNSUPPORTED
);
2731 Print at a specific location on the screen.
2733 This function will move the cursor to a given screen location and print the specified string
2735 If -1 is specified for either the Row or Col the current screen location for BOTH
2738 if either Row or Col is out of range for the current console, then ASSERT
2739 if Format is NULL, then ASSERT
2741 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2742 the following additional flags:
2743 %N - Set output attribute to normal
2744 %H - Set output attribute to highlight
2745 %E - Set output attribute to error
2746 %B - Set output attribute to blue color
2747 %V - Set output attribute to green color
2749 Note: The background color is controlled by the shell command cls.
2751 @param[in] Col the column to print at
2752 @param[in] Row the row to print at
2753 @param[in] Format the format string
2754 @param[in] Marker the marker for the variable argument list
2756 @return EFI_SUCCESS The operation was successful.
2757 @return EFI_DEVICE_ERROR The console device reported an error.
2760 InternalShellPrintWorker(
2761 IN INT32 Col OPTIONAL
,
2762 IN INT32 Row OPTIONAL
,
2763 IN CONST CHAR16
*Format
,
2768 CHAR16
*ResumeLocation
;
2769 CHAR16
*FormatWalker
;
2770 UINTN OriginalAttribute
;
2771 CHAR16
*mPostReplaceFormat
;
2772 CHAR16
*mPostReplaceFormat2
;
2774 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2775 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2777 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2778 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2779 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2780 return (EFI_OUT_OF_RESOURCES
);
2783 Status
= EFI_SUCCESS
;
2784 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2787 // Back and forth each time fixing up 1 of our flags...
2789 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2790 ASSERT_EFI_ERROR(Status
);
2791 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2792 ASSERT_EFI_ERROR(Status
);
2793 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2794 ASSERT_EFI_ERROR(Status
);
2795 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2796 ASSERT_EFI_ERROR(Status
);
2797 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2798 ASSERT_EFI_ERROR(Status
);
2801 // Use the last buffer from replacing to print from...
2803 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2805 if (Col
!= -1 && Row
!= -1) {
2806 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2809 FormatWalker
= mPostReplaceFormat2
;
2810 while (*FormatWalker
!= CHAR_NULL
) {
2812 // Find the next attribute change request
2814 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2815 if (ResumeLocation
!= NULL
) {
2816 *ResumeLocation
= CHAR_NULL
;
2819 // print the current FormatWalker string
2821 if (StrLen(FormatWalker
)>0) {
2822 Status
= InternalPrintTo(FormatWalker
);
2823 if (EFI_ERROR(Status
)) {
2829 // update the attribute
2831 if (ResumeLocation
!= NULL
) {
2832 if (*(ResumeLocation
-1) == L
'^') {
2834 // Move cursor back 1 position to overwrite the ^
2836 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2839 // Print a simple '%' symbol
2841 Status
= InternalPrintTo(L
"%");
2842 ResumeLocation
= ResumeLocation
- 1;
2844 switch (*(ResumeLocation
+1)) {
2846 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2849 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2852 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2855 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2858 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2862 // Print a simple '%' symbol
2864 Status
= InternalPrintTo(L
"%");
2865 if (EFI_ERROR(Status
)) {
2868 ResumeLocation
= ResumeLocation
- 1;
2874 // reset to normal now...
2880 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2882 FormatWalker
= ResumeLocation
+ 2;
2885 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2887 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2888 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2893 Print at a specific location on the screen.
2895 This function will move the cursor to a given screen location and print the specified string.
2897 If -1 is specified for either the Row or Col the current screen location for BOTH
2900 If either Row or Col is out of range for the current console, then ASSERT.
2901 If Format is NULL, then ASSERT.
2903 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2904 the following additional flags:
2905 %N - Set output attribute to normal
2906 %H - Set output attribute to highlight
2907 %E - Set output attribute to error
2908 %B - Set output attribute to blue color
2909 %V - Set output attribute to green color
2911 Note: The background color is controlled by the shell command cls.
2913 @param[in] Col the column to print at
2914 @param[in] Row the row to print at
2915 @param[in] Format the format string
2916 @param[in] ... The variable argument list.
2918 @return EFI_SUCCESS The printing was successful.
2919 @return EFI_DEVICE_ERROR The console device reported an error.
2924 IN INT32 Col OPTIONAL
,
2925 IN INT32 Row OPTIONAL
,
2926 IN CONST CHAR16
*Format
,
2932 if (Format
== NULL
) {
2933 return (EFI_INVALID_PARAMETER
);
2935 VA_START (Marker
, Format
);
2936 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2942 Print at a specific location on the screen.
2944 This function will move the cursor to a given screen location and print the specified string.
2946 If -1 is specified for either the Row or Col the current screen location for BOTH
2949 If either Row or Col is out of range for the current console, then ASSERT.
2950 If Format is NULL, then ASSERT.
2952 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2953 the following additional flags:
2954 %N - Set output attribute to normal.
2955 %H - Set output attribute to highlight.
2956 %E - Set output attribute to error.
2957 %B - Set output attribute to blue color.
2958 %V - Set output attribute to green color.
2960 Note: The background color is controlled by the shell command cls.
2962 @param[in] Col The column to print at.
2963 @param[in] Row The row to print at.
2964 @param[in] Language The language of the string to retrieve. If this parameter
2965 is NULL, then the current platform language is used.
2966 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2967 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2968 @param[in] ... The variable argument list.
2970 @return EFI_SUCCESS The printing was successful.
2971 @return EFI_DEVICE_ERROR The console device reported an error.
2976 IN INT32 Col OPTIONAL
,
2977 IN INT32 Row OPTIONAL
,
2978 IN CONST CHAR8
*Language OPTIONAL
,
2979 IN CONST EFI_STRING_ID HiiFormatStringId
,
2980 IN CONST EFI_HANDLE HiiFormatHandle
,
2985 CHAR16
*HiiFormatString
;
2988 RetVal
= EFI_DEVICE_ERROR
;
2990 VA_START (Marker
, HiiFormatHandle
);
2991 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2992 if (HiiFormatString
!= NULL
) {
2993 RetVal
= InternalShellPrintWorker (Col
, Row
, HiiFormatString
, Marker
);
2994 SHELL_FREE_NON_NULL (HiiFormatString
);
3002 Function to determine if a given filename represents a file or a directory.
3004 @param[in] DirName Path to directory to test.
3006 @retval EFI_SUCCESS The Path represents a directory
3007 @retval EFI_NOT_FOUND The Path does not represent a directory
3008 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3009 @return The path failed to open
3014 IN CONST CHAR16
*DirName
3018 SHELL_FILE_HANDLE Handle
;
3019 CHAR16
*TempLocation
;
3020 CHAR16
*TempLocation2
;
3022 ASSERT(DirName
!= NULL
);
3025 TempLocation
= NULL
;
3027 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3028 if (EFI_ERROR(Status
)) {
3030 // try good logic first.
3032 if (gEfiShellProtocol
!= NULL
) {
3033 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3034 if (TempLocation
== NULL
) {
3035 ShellCloseFile(&Handle
);
3036 return (EFI_OUT_OF_RESOURCES
);
3038 TempLocation2
= StrStr(TempLocation
, L
":");
3039 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3040 *(TempLocation2
+1) = CHAR_NULL
;
3042 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3043 FreePool(TempLocation
);
3044 return (EFI_SUCCESS
);
3046 FreePool(TempLocation
);
3049 // probably a map name?!?!!?
3051 TempLocation
= StrStr(DirName
, L
"\\");
3052 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3053 return (EFI_SUCCESS
);
3059 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3060 ShellCloseFile(&Handle
);
3061 return (EFI_SUCCESS
);
3063 ShellCloseFile(&Handle
);
3064 return (EFI_NOT_FOUND
);
3068 Function to determine if a given filename represents a file.
3070 @param[in] Name Path to file to test.
3072 @retval EFI_SUCCESS The Path represents a file.
3073 @retval EFI_NOT_FOUND The Path does not represent a file.
3074 @retval other The path failed to open.
3079 IN CONST CHAR16
*Name
3083 SHELL_FILE_HANDLE Handle
;
3085 ASSERT(Name
!= NULL
);
3089 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3090 if (EFI_ERROR(Status
)) {
3094 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3095 ShellCloseFile(&Handle
);
3096 return (EFI_SUCCESS
);
3098 ShellCloseFile(&Handle
);
3099 return (EFI_NOT_FOUND
);
3103 Function to determine if a given filename represents a file.
3105 This will search the CWD and then the Path.
3107 If Name is NULL, then ASSERT.
3109 @param[in] Name Path to file to test.
3111 @retval EFI_SUCCESS The Path represents a file.
3112 @retval EFI_NOT_FOUND The Path does not represent a file.
3113 @retval other The path failed to open.
3118 IN CONST CHAR16
*Name
3124 if (!EFI_ERROR(ShellIsFile(Name
))) {
3125 return (EFI_SUCCESS
);
3128 NewName
= ShellFindFilePath(Name
);
3129 if (NewName
== NULL
) {
3130 return (EFI_NOT_FOUND
);
3132 Status
= ShellIsFile(NewName
);
3138 Function return the number converted from a hex representation of a number.
3140 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3141 result. Use ShellConvertStringToUint64 instead.
3143 @param[in] String String representation of a number.
3145 @return The unsigned integer result of the conversion.
3146 @retval (UINTN)(-1) An error occured.
3151 IN CONST CHAR16
*String
3156 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3157 return ((UINTN
)RetVal
);
3160 return ((UINTN
)(-1));
3164 Function to determine whether a string is decimal or hex representation of a number
3165 and return the number converted from the string. Spaces are always skipped.
3167 @param[in] String String representation of a number
3170 @retval (UINTN)(-1) An error ocurred.
3175 IN CONST CHAR16
*String
3183 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3187 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3188 return ((UINTN
)RetVal
);
3190 return ((UINTN
)(-1));
3194 Safely append with automatic string resizing given length of Destination and
3195 desired length of copy from Source.
3197 append the first D characters of Source to the end of Destination, where D is
3198 the lesser of Count and the StrLen() of Source. If appending those D characters
3199 will fit within Destination (whose Size is given as CurrentSize) and
3200 still leave room for a NULL terminator, then those characters are appended,
3201 starting at the original terminating NULL of Destination, and a new terminating
3204 If appending D characters onto Destination will result in a overflow of the size
3205 given in CurrentSize the string will be grown such that the copy can be performed
3206 and CurrentSize will be updated to the new size.
3208 If Source is NULL, there is nothing to append, just return the current buffer in
3211 if Destination is NULL, then ASSERT()
3212 if Destination's current length (including NULL terminator) is already more then
3213 CurrentSize, then ASSERT()
3215 @param[in, out] Destination The String to append onto
3216 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3217 return possibly the new size (still in bytes). if NULL
3218 then allocate whatever is needed.
3219 @param[in] Source The String to append from
3220 @param[in] Count Maximum number of characters to append. if 0 then
3223 @return Destination return the resultant string.
3228 IN OUT CHAR16
**Destination
,
3229 IN OUT UINTN
*CurrentSize
,
3230 IN CONST CHAR16
*Source
,
3234 UINTN DestinationStartSize
;
3240 ASSERT(Destination
!= NULL
);
3243 // If there's nothing to do then just return Destination
3245 if (Source
== NULL
) {
3246 return (*Destination
);
3250 // allow for un-initialized pointers, based on size being 0
3252 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3253 *Destination
= NULL
;
3257 // allow for NULL pointers address as Destination
3259 if (*Destination
!= NULL
) {
3260 ASSERT(CurrentSize
!= 0);
3261 DestinationStartSize
= StrSize(*Destination
);
3262 ASSERT(DestinationStartSize
<= *CurrentSize
);
3264 DestinationStartSize
= 0;
3265 // ASSERT(*CurrentSize == 0);
3269 // Append all of Source?
3272 Count
= StrLen(Source
);
3276 // Test and grow if required
3278 if (CurrentSize
!= NULL
) {
3279 NewSize
= *CurrentSize
;
3280 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3281 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3282 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3284 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3285 *CurrentSize
= NewSize
;
3288 NewSize
= (Count
+1)*sizeof(CHAR16
);
3289 *Destination
= AllocateZeroPool(NewSize
);
3293 // Now use standard StrnCat on a big enough buffer
3295 if (*Destination
== NULL
) {
3299 StrnCatS(*Destination
, NewSize
/sizeof(CHAR16
), Source
, Count
);
3300 return *Destination
;
3304 Prompt the user and return the resultant answer to the requestor.
3306 This function will display the requested question on the shell prompt and then
3307 wait for an appropriate answer to be input from the console.
3309 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3310 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3312 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3315 In either case *Response must be callee freed if Response was not NULL;
3317 @param Type What type of question is asked. This is used to filter the input
3318 to prevent invalid answers to question.
3319 @param Prompt Pointer to string prompt to use to request input.
3320 @param Response Pointer to Response which will be populated upon return.
3322 @retval EFI_SUCCESS The operation was sucessful.
3323 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3324 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3325 @return other The operation failed.
3329 ShellPromptForResponse (
3330 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3331 IN CHAR16
*Prompt OPTIONAL
,
3332 IN OUT VOID
**Response OPTIONAL
3338 SHELL_PROMPT_RESPONSE
*Resp
;
3342 Status
= EFI_UNSUPPORTED
;
3346 if (Type
!= ShellPromptResponseTypeFreeform
) {
3347 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3349 return (EFI_OUT_OF_RESOURCES
);
3354 case ShellPromptResponseTypeQuitContinue
:
3355 if (Prompt
!= NULL
) {
3356 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3359 // wait for valid response
3361 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3362 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3363 if (EFI_ERROR(Status
)) {
3366 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3367 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3368 *Resp
= ShellPromptResponseQuit
;
3370 *Resp
= ShellPromptResponseContinue
;
3373 case ShellPromptResponseTypeYesNoCancel
:
3374 if (Prompt
!= NULL
) {
3375 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3378 // wait for valid response
3380 *Resp
= ShellPromptResponseMax
;
3381 while (*Resp
== ShellPromptResponseMax
) {
3382 if (ShellGetExecutionBreakFlag()) {
3383 Status
= EFI_ABORTED
;
3386 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3387 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3388 if (EFI_ERROR(Status
)) {
3391 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3392 switch (Key
.UnicodeChar
) {
3395 *Resp
= ShellPromptResponseYes
;
3399 *Resp
= ShellPromptResponseNo
;
3403 *Resp
= ShellPromptResponseCancel
;
3407 break; case ShellPromptResponseTypeYesNoAllCancel
:
3408 if (Prompt
!= NULL
) {
3409 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3412 // wait for valid response
3414 *Resp
= ShellPromptResponseMax
;
3415 while (*Resp
== ShellPromptResponseMax
) {
3416 if (ShellGetExecutionBreakFlag()) {
3417 Status
= EFI_ABORTED
;
3420 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3421 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3422 if (EFI_ERROR(Status
)) {
3425 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3426 switch (Key
.UnicodeChar
) {
3429 *Resp
= ShellPromptResponseYes
;
3433 *Resp
= ShellPromptResponseNo
;
3437 *Resp
= ShellPromptResponseAll
;
3441 *Resp
= ShellPromptResponseCancel
;
3446 case ShellPromptResponseTypeEnterContinue
:
3447 case ShellPromptResponseTypeAnyKeyContinue
:
3448 if (Prompt
!= NULL
) {
3449 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3452 // wait for valid response
3454 *Resp
= ShellPromptResponseMax
;
3455 while (*Resp
== ShellPromptResponseMax
) {
3456 if (ShellGetExecutionBreakFlag()) {
3457 Status
= EFI_ABORTED
;
3460 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3461 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3462 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3463 if (EFI_ERROR(Status
)) {
3466 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3467 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3468 *Resp
= ShellPromptResponseContinue
;
3472 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3473 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3474 ASSERT_EFI_ERROR(Status
);
3475 *Resp
= ShellPromptResponseContinue
;
3480 case ShellPromptResponseTypeYesNo
:
3481 if (Prompt
!= NULL
) {
3482 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3485 // wait for valid response
3487 *Resp
= ShellPromptResponseMax
;
3488 while (*Resp
== ShellPromptResponseMax
) {
3489 if (ShellGetExecutionBreakFlag()) {
3490 Status
= EFI_ABORTED
;
3493 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3494 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3495 if (EFI_ERROR(Status
)) {
3498 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3499 switch (Key
.UnicodeChar
) {
3502 *Resp
= ShellPromptResponseYes
;
3506 *Resp
= ShellPromptResponseNo
;
3511 case ShellPromptResponseTypeFreeform
:
3512 if (Prompt
!= NULL
) {
3513 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3516 if (ShellGetExecutionBreakFlag()) {
3517 Status
= EFI_ABORTED
;
3520 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3521 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3522 if (EFI_ERROR(Status
)) {
3525 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3526 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3529 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3530 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3534 // This is the location to add new prompt types.
3535 // If your new type loops remember to add ExecutionBreak support.
3541 if (Response
!= NULL
) {
3544 } else if (Buffer
!= NULL
) {
3551 if (Buffer
!= NULL
) {
3556 ShellPrintEx(-1, -1, L
"\r\n");
3561 Prompt the user and return the resultant answer to the requestor.
3563 This function is the same as ShellPromptForResponse, except that the prompt is
3564 automatically pulled from HII.
3566 @param Type What type of question is asked. This is used to filter the input
3567 to prevent invalid answers to question.
3568 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3569 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3570 @param Response Pointer to Response which will be populated upon return.
3572 @retval EFI_SUCCESS the operation was sucessful.
3573 @return other the operation failed.
3575 @sa ShellPromptForResponse
3579 ShellPromptForResponseHii (
3580 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3581 IN CONST EFI_STRING_ID HiiFormatStringId
,
3582 IN CONST EFI_HANDLE HiiFormatHandle
,
3583 IN OUT VOID
**Response
3589 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3590 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3596 Function to determin if an entire string is a valid number.
3598 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3600 @param[in] String The string to evaluate.
3601 @param[in] ForceHex TRUE - always assume hex.
3602 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3603 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3605 @retval TRUE It is all numeric (dec/hex) characters.
3606 @retval FALSE There is a non-numeric character.
3609 InternalShellIsHexOrDecimalNumber (
3610 IN CONST CHAR16
*String
,
3611 IN CONST BOOLEAN ForceHex
,
3612 IN CONST BOOLEAN StopAtSpace
,
3613 IN CONST BOOLEAN TimeNumbers
3619 // chop off a single negative sign
3621 if (String
!= NULL
&& *String
== L
'-') {
3625 if (String
== NULL
) {
3630 // chop leading zeroes
3632 while(String
!= NULL
&& *String
== L
'0'){
3636 // allow '0x' or '0X', but not 'x' or 'X'
3638 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3639 if (*(String
-1) != L
'0') {
3641 // we got an x without a preceeding 0
3647 } else if (ForceHex
) {
3654 // loop through the remaining characters and use the lib function
3656 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3657 if (TimeNumbers
&& (String
[0] == L
':')) {
3661 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3665 if (!ShellIsDecimalDigitCharacter(*String
)) {
3675 Function to determine if a given filename exists.
3677 @param[in] Name Path to test.
3679 @retval EFI_SUCCESS The Path represents a file.
3680 @retval EFI_NOT_FOUND The Path does not represent a file.
3681 @retval other The path failed to open.
3686 IN CONST CHAR16
*Name
3690 EFI_SHELL_FILE_INFO
*List
;
3692 ASSERT(Name
!= NULL
);
3695 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3696 if (EFI_ERROR(Status
)) {
3700 ShellCloseFileMetaArg(&List
);
3702 return (EFI_SUCCESS
);
3706 Convert a Unicode character to upper case only if
3707 it maps to a valid small-case ASCII character.
3709 This internal function only deal with Unicode character
3710 which maps to a valid small-case ASCII character, i.e.
3711 L'a' to L'z'. For other Unicode character, the input character
3712 is returned directly.
3714 @param Char The character to convert.
3716 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3717 @retval Unchanged Otherwise.
3721 InternalShellCharToUpper (
3725 if (Char
>= L
'a' && Char
<= L
'z') {
3726 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3733 Convert a Unicode character to numerical value.
3735 This internal function only deal with Unicode character
3736 which maps to a valid hexadecimal ASII character, i.e.
3737 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3738 Unicode character, the value returned does not make sense.
3740 @param Char The character to convert.
3742 @return The numerical value converted.
3746 InternalShellHexCharToUintn (
3750 if (ShellIsDecimalDigitCharacter (Char
)) {
3754 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3758 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3760 This function returns a value of type UINT64 by interpreting the contents
3761 of the Unicode string specified by String as a hexadecimal number.
3762 The format of the input Unicode string String is:
3764 [spaces][zeros][x][hexadecimal digits].
3766 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3767 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3768 If "x" appears in the input string, it must be prefixed with at least one 0.
3769 The function will ignore the pad space, which includes spaces or tab characters,
3770 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3771 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3772 first valid hexadecimal digit. Then, the function stops at the first character that is
3773 a not a valid hexadecimal character or NULL, whichever one comes first.
3775 If String has only pad spaces, then zero is returned.
3776 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3777 then zero is returned.
3779 @param[in] String A pointer to a Null-terminated Unicode string.
3780 @param[out] Value Upon a successful return the value of the conversion.
3781 @param[in] StopAtSpace FALSE to skip spaces.
3783 @retval EFI_SUCCESS The conversion was successful.
3784 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3785 @retval EFI_DEVICE_ERROR An overflow occured.
3788 InternalShellStrHexToUint64 (
3789 IN CONST CHAR16
*String
,
3791 IN CONST BOOLEAN StopAtSpace
3796 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3797 return (EFI_INVALID_PARAMETER
);
3801 // Ignore the pad spaces (space or tab)
3803 while ((*String
== L
' ') || (*String
== L
'\t')) {
3808 // Ignore leading Zeros after the spaces
3810 while (*String
== L
'0') {
3814 if (InternalShellCharToUpper (*String
) == L
'X') {
3815 if (*(String
- 1) != L
'0') {
3827 // there is a space where there should't be
3829 if (*String
== L
' ') {
3830 return (EFI_INVALID_PARAMETER
);
3833 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3835 // If the Hex Number represented by String overflows according
3836 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3838 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3839 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3840 return (EFI_DEVICE_ERROR
);
3843 Result
= (LShiftU64(Result
, 4));
3844 Result
+= InternalShellHexCharToUintn (*String
);
3848 // stop at spaces if requested
3850 if (StopAtSpace
&& *String
== L
' ') {
3856 return (EFI_SUCCESS
);
3860 Convert a Null-terminated Unicode decimal string to a value of
3863 This function returns a value of type UINT64 by interpreting the contents
3864 of the Unicode string specified by String as a decimal number. The format
3865 of the input Unicode string String is:
3867 [spaces] [decimal digits].
3869 The valid decimal digit character is in the range [0-9]. The
3870 function will ignore the pad space, which includes spaces or
3871 tab characters, before [decimal digits]. The running zero in the
3872 beginning of [decimal digits] will be ignored. Then, the function
3873 stops at the first character that is a not a valid decimal character
3874 or a Null-terminator, whichever one comes first.
3876 If String has only pad spaces, then 0 is returned.
3877 If String has no pad spaces or valid decimal digits,
3880 @param[in] String A pointer to a Null-terminated Unicode string.
3881 @param[out] Value Upon a successful return the value of the conversion.
3882 @param[in] StopAtSpace FALSE to skip spaces.
3884 @retval EFI_SUCCESS The conversion was successful.
3885 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3886 @retval EFI_DEVICE_ERROR An overflow occured.
3889 InternalShellStrDecimalToUint64 (
3890 IN CONST CHAR16
*String
,
3892 IN CONST BOOLEAN StopAtSpace
3897 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3898 return (EFI_INVALID_PARAMETER
);
3902 // Ignore the pad spaces (space or tab)
3904 while ((*String
== L
' ') || (*String
== L
'\t')) {
3909 // Ignore leading Zeros after the spaces
3911 while (*String
== L
'0') {
3918 // Stop upon space if requested
3919 // (if the whole value was 0)
3921 if (StopAtSpace
&& *String
== L
' ') {
3923 return (EFI_SUCCESS
);
3926 while (ShellIsDecimalDigitCharacter (*String
)) {
3928 // If the number represented by String overflows according
3929 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3932 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3933 return (EFI_DEVICE_ERROR
);
3936 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3940 // Stop at spaces if requested
3942 if (StopAtSpace
&& *String
== L
' ') {
3949 return (EFI_SUCCESS
);
3953 Function to verify and convert a string to its numerical value.
3955 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3957 @param[in] String The string to evaluate.
3958 @param[out] Value Upon a successful return the value of the conversion.
3959 @param[in] ForceHex TRUE - always assume hex.
3960 @param[in] StopAtSpace FALSE to skip spaces.
3962 @retval EFI_SUCCESS The conversion was successful.
3963 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3964 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3968 ShellConvertStringToUint64(
3969 IN CONST CHAR16
*String
,
3971 IN CONST BOOLEAN ForceHex
,
3972 IN CONST BOOLEAN StopAtSpace
3976 CONST CHAR16
*Walker
;
3982 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3985 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3986 return (EFI_INVALID_PARAMETER
);
3989 return (EFI_INVALID_PARAMETER
);
3994 // Chop off leading spaces
3996 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
3999 // make sure we have something left that is numeric.
4001 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
4002 return (EFI_INVALID_PARAMETER
);
4006 // do the conversion.
4008 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
4009 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
4011 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
4014 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
4015 return (EFI_NOT_FOUND
);
4018 if (Value
!= NULL
) {
4026 Function to determin if an entire string is a valid number.
4028 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4030 @param[in] String The string to evaluate.
4031 @param[in] ForceHex TRUE - always assume hex.
4032 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4034 @retval TRUE It is all numeric (dec/hex) characters.
4035 @retval FALSE There is a non-numeric character.
4039 ShellIsHexOrDecimalNumber (
4040 IN CONST CHAR16
*String
,
4041 IN CONST BOOLEAN ForceHex
,
4042 IN CONST BOOLEAN StopAtSpace
4045 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4052 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4053 buffer. The returned buffer must be callee freed.
4055 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4056 maintained and not changed for all operations with the same file.
4058 @param[in] Handle SHELL_FILE_HANDLE to read from.
4059 @param[in, out] Ascii Boolean value for indicating whether the file is
4060 Ascii (TRUE) or UCS2 (FALSE).
4062 @return The line of text from the file.
4063 @retval NULL There was not enough memory available.
4065 @sa ShellFileHandleReadLine
4069 ShellFileHandleReturnLine(
4070 IN SHELL_FILE_HANDLE Handle
,
4071 IN OUT BOOLEAN
*Ascii
4081 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4082 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4083 RetVal
= AllocateZeroPool(Size
);
4084 if (RetVal
== NULL
) {
4087 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4090 if (Status
== EFI_END_OF_FILE
&& RetVal
!= NULL
&& *RetVal
!= CHAR_NULL
) {
4091 Status
= EFI_SUCCESS
;
4093 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4101 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4103 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4104 maintained and not changed for all operations with the same file.
4106 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4109 @param[in] Handle SHELL_FILE_HANDLE to read from.
4110 @param[in, out] Buffer The pointer to buffer to read into. If this function
4111 returns EFI_SUCCESS, then on output Buffer will
4112 contain a UCS2 string, even if the file being
4114 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4115 On output, unchanged unless Buffer is too small
4116 to contain the next line of the file. In that
4117 case Size is set to the number of bytes needed
4118 to hold the next line of the file (as a UCS2
4119 string, even if it is an ASCII file).
4120 @param[in] Truncate If the buffer is large enough, this has no effect.
4121 If the buffer is is too small and Truncate is TRUE,
4122 the line will be truncated.
4123 If the buffer is is too small and Truncate is FALSE,
4124 then no read will occur.
4126 @param[in, out] Ascii Boolean value for indicating whether the file is
4127 Ascii (TRUE) or UCS2 (FALSE).
4129 @retval EFI_SUCCESS The operation was successful. The line is stored in
4131 @retval EFI_END_OF_FILE There are no more lines in the file.
4132 @retval EFI_INVALID_PARAMETER Handle was NULL.
4133 @retval EFI_INVALID_PARAMETER Size was NULL.
4134 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4135 Size was updated to the minimum space required.
4139 ShellFileHandleReadLine(
4140 IN SHELL_FILE_HANDLE Handle
,
4141 IN OUT CHAR16
*Buffer
,
4143 IN BOOLEAN Truncate
,
4144 IN OUT BOOLEAN
*Ascii
4151 UINT64 OriginalFilePosition
;
4157 return (EFI_INVALID_PARAMETER
);
4159 if (Buffer
== NULL
) {
4162 *Buffer
= CHAR_NULL
;
4164 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4165 if (OriginalFilePosition
== 0) {
4166 CharSize
= sizeof(CHAR16
);
4167 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4168 ASSERT_EFI_ERROR(Status
);
4169 if (CharBuffer
== gUnicodeFileTag
) {
4173 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4178 CharSize
= sizeof(CHAR8
);
4180 CharSize
= sizeof(CHAR16
);
4182 for (CountSoFar
= 0;;CountSoFar
++){
4184 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4185 if ( EFI_ERROR(Status
)
4187 || (CharBuffer
== L
'\n' && !(*Ascii
))
4188 || (CharBuffer
== '\n' && *Ascii
)
4190 if (CharSize
== 0) {
4191 Status
= EFI_END_OF_FILE
;
4196 // if we have space save it...
4198 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4199 ASSERT(Buffer
!= NULL
);
4200 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4201 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4206 // if we ran out of space tell when...
4208 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4209 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4211 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4213 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4215 return (EFI_BUFFER_TOO_SMALL
);
4217 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4218 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4225 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4227 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4228 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4229 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4230 the help content only.
4231 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4232 @retval EFI_NOT_FOUND The help data could not be found.
4233 @retval EFI_SUCCESS The operation was successful.
4238 IN CONST CHAR16
*CommandToGetHelpOn
,
4239 IN CONST CHAR16
*SectionToGetHelpOn
,
4240 IN BOOLEAN PrintCommandText
4249 // Get the string to print based
4251 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4254 // make sure we got a valid string
4256 if (EFI_ERROR(Status
)){
4259 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4260 return EFI_NOT_FOUND
;
4264 // Chop off trailing stuff we dont need
4266 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4267 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4271 // Print this out to the console
4273 if (PrintCommandText
) {
4274 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4276 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4279 SHELL_FREE_NON_NULL(OutText
);
4285 Function to delete a file by name
4287 @param[in] FileName Pointer to file name to delete.
4289 @retval EFI_SUCCESS the file was deleted sucessfully
4290 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4292 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4293 @retval EFI_NOT_FOUND The specified file could not be found on the
4294 device or the file system could not be found
4296 @retval EFI_NO_MEDIA The device has no medium.
4297 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4298 medium is no longer supported.
4299 @retval EFI_DEVICE_ERROR The device reported an error.
4300 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4301 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4302 @retval EFI_ACCESS_DENIED The file was opened read only.
4303 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4305 @retval other The file failed to open
4309 ShellDeleteFileByName(
4310 IN CONST CHAR16
*FileName
4314 SHELL_FILE_HANDLE FileHandle
;
4316 Status
= ShellFileExists(FileName
);
4318 if (Status
== EFI_SUCCESS
){
4319 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4320 if (Status
== EFI_SUCCESS
){
4321 Status
= ShellDeleteFile(&FileHandle
);
4330 Cleans off all the quotes in the string.
4332 @param[in] OriginalString pointer to the string to be cleaned.
4333 @param[out] CleanString The new string with all quotes removed.
4334 Memory allocated in the function and free
4337 @retval EFI_SUCCESS The operation was successful.
4340 InternalShellStripQuotes (
4341 IN CONST CHAR16
*OriginalString
,
4342 OUT CHAR16
**CleanString
4347 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4348 return EFI_INVALID_PARAMETER
;
4351 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4352 if (*CleanString
== NULL
) {
4353 return EFI_OUT_OF_RESOURCES
;
4356 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4357 if (*Walker
== L
'\"') {
4358 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));