2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLib.h"
16 #include <ShellBase.h>
17 #include <Library/SortLib.h>
19 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
24 SHELL_PARAM_ITEM EmptyParamList
[] = {
27 SHELL_PARAM_ITEM SfoParamList
[] = {
31 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
32 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
33 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
34 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
35 EFI_HANDLE mEfiShellEnvironment2Handle
;
36 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
39 Check if a Unicode character is a hexadecimal character.
41 This internal function checks if a Unicode character is a
42 numeric character. The valid hexadecimal characters are
43 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
45 @param Char The character to check against.
47 @retval TRUE If the Char is a hexadecmial character.
48 @retval FALSE If the Char is not a hexadecmial character.
53 ShellIsHexaDecimalDigitCharacter (
57 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
61 Check if a Unicode character is a decimal character.
63 This internal function checks if a Unicode character is a
64 decimal character. The valid characters are
68 @param Char The character to check against.
70 @retval TRUE If the Char is a hexadecmial character.
71 @retval FALSE If the Char is not a hexadecmial character.
76 ShellIsDecimalDigitCharacter (
80 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
84 Helper function to find ShellEnvironment2 for constructor.
86 @param[in] ImageHandle A copy of the calling image's handle.
88 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
93 IN EFI_HANDLE ImageHandle
103 Status
= gBS
->OpenProtocol(ImageHandle
,
104 &gEfiShellEnvironment2Guid
,
105 (VOID
**)&mEfiShellEnvironment2
,
108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
111 // look for the mEfiShellEnvironment2 protocol at a higher level
113 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
115 // figure out how big of a buffer we need.
117 Status
= gBS
->LocateHandle (ByProtocol
,
118 &gEfiShellEnvironment2Guid
,
119 NULL
, // ignored for ByProtocol
124 // maybe it's not there???
126 if (Status
== EFI_BUFFER_TOO_SMALL
) {
127 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
128 if (Buffer
== NULL
) {
129 return (EFI_OUT_OF_RESOURCES
);
131 Status
= gBS
->LocateHandle (ByProtocol
,
132 &gEfiShellEnvironment2Guid
,
133 NULL
, // ignored for ByProtocol
138 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
140 // now parse the list of returned handles
142 Status
= EFI_NOT_FOUND
;
143 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
144 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
145 &gEfiShellEnvironment2Guid
,
146 (VOID
**)&mEfiShellEnvironment2
,
149 EFI_OPEN_PROTOCOL_GET_PROTOCOL
151 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
152 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
153 Status
= EFI_SUCCESS
;
159 if (Buffer
!= NULL
) {
166 Function to do most of the work of the constructor. Allows for calling
167 multiple times without complete re-initialization.
169 @param[in] ImageHandle A copy of the ImageHandle.
170 @param[in] SystemTable A pointer to the SystemTable for the application.
172 @retval EFI_SUCCESS The operationw as successful.
176 ShellLibConstructorWorker (
177 IN EFI_HANDLE ImageHandle
,
178 IN EFI_SYSTEM_TABLE
*SystemTable
184 // UEFI 2.0 shell interfaces (used preferentially)
186 Status
= gBS
->OpenProtocol(
188 &gEfiShellProtocolGuid
,
189 (VOID
**)&gEfiShellProtocol
,
192 EFI_OPEN_PROTOCOL_GET_PROTOCOL
194 if (EFI_ERROR(Status
)) {
196 // Search for the shell protocol
198 Status
= gBS
->LocateProtocol(
199 &gEfiShellProtocolGuid
,
201 (VOID
**)&gEfiShellProtocol
203 if (EFI_ERROR(Status
)) {
204 gEfiShellProtocol
= NULL
;
207 Status
= gBS
->OpenProtocol(
209 &gEfiShellParametersProtocolGuid
,
210 (VOID
**)&gEfiShellParametersProtocol
,
213 EFI_OPEN_PROTOCOL_GET_PROTOCOL
215 if (EFI_ERROR(Status
)) {
216 gEfiShellParametersProtocol
= NULL
;
219 if (gEfiShellParametersProtocol
== NULL
|| gEfiShellProtocol
== NULL
) {
221 // Moved to seperate function due to complexity
223 Status
= ShellFindSE2(ImageHandle
);
225 if (EFI_ERROR(Status
)) {
226 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
227 mEfiShellEnvironment2
= NULL
;
229 Status
= gBS
->OpenProtocol(ImageHandle
,
230 &gEfiShellInterfaceGuid
,
231 (VOID
**)&mEfiShellInterface
,
234 EFI_OPEN_PROTOCOL_GET_PROTOCOL
236 if (EFI_ERROR(Status
)) {
237 mEfiShellInterface
= NULL
;
242 // only success getting 2 of either the old or new, but no 1/2 and 1/2
244 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
245 (gEfiShellProtocol
!= NULL
&& gEfiShellParametersProtocol
!= NULL
) ) {
246 if (gEfiShellProtocol
!= NULL
) {
247 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
248 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
249 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
250 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
251 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
252 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
253 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
254 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
255 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
256 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
258 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
259 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
260 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
261 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
262 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
263 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
264 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
265 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
266 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
267 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
269 return (EFI_SUCCESS
);
271 return (EFI_NOT_FOUND
);
274 Constructor for the Shell library.
276 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
278 @param ImageHandle the image handle of the process
279 @param SystemTable the EFI System Table pointer
281 @retval EFI_SUCCESS the initialization was complete sucessfully
282 @return others an error ocurred during initialization
286 ShellLibConstructor (
287 IN EFI_HANDLE ImageHandle
,
288 IN EFI_SYSTEM_TABLE
*SystemTable
291 mEfiShellEnvironment2
= NULL
;
292 gEfiShellProtocol
= NULL
;
293 gEfiShellParametersProtocol
= NULL
;
294 mEfiShellInterface
= NULL
;
295 mEfiShellEnvironment2Handle
= NULL
;
298 // verify that auto initialize is not set false
300 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
301 return (EFI_SUCCESS
);
304 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
308 Destructor for the library. free any resources.
310 @param[in] ImageHandle A copy of the ImageHandle.
311 @param[in] SystemTable A pointer to the SystemTable for the application.
313 @retval EFI_SUCCESS The operation was successful.
314 @return An error from the CloseProtocol function.
319 IN EFI_HANDLE ImageHandle
,
320 IN EFI_SYSTEM_TABLE
*SystemTable
323 if (mEfiShellEnvironment2
!= NULL
) {
324 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
325 &gEfiShellEnvironment2Guid
,
328 mEfiShellEnvironment2
= NULL
;
330 if (mEfiShellInterface
!= NULL
) {
331 gBS
->CloseProtocol(ImageHandle
,
332 &gEfiShellInterfaceGuid
,
335 mEfiShellInterface
= NULL
;
337 if (gEfiShellProtocol
!= NULL
) {
338 gBS
->CloseProtocol(ImageHandle
,
339 &gEfiShellProtocolGuid
,
342 gEfiShellProtocol
= NULL
;
344 if (gEfiShellParametersProtocol
!= NULL
) {
345 gBS
->CloseProtocol(ImageHandle
,
346 &gEfiShellParametersProtocolGuid
,
349 gEfiShellParametersProtocol
= NULL
;
351 mEfiShellEnvironment2Handle
= NULL
;
353 return (EFI_SUCCESS
);
357 This function causes the shell library to initialize itself. If the shell library
358 is already initialized it will de-initialize all the current protocol poitners and
359 re-populate them again.
361 When the library is used with PcdShellLibAutoInitialize set to true this function
362 will return EFI_SUCCESS and perform no actions.
364 This function is intended for internal access for shell commands only.
366 @retval EFI_SUCCESS the initialization was complete sucessfully
375 // if auto initialize is not false then skip
377 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
378 return (EFI_SUCCESS
);
382 // deinit the current stuff
384 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
387 // init the new stuff
389 return (ShellLibConstructorWorker(gImageHandle
, gST
));
393 This function will retrieve the information about the file for the handle
394 specified and store it in allocated pool memory.
396 This function allocates a buffer to store the file's information. It is the
397 caller's responsibility to free the buffer
399 @param FileHandle The file handle of the file for which information is
402 @retval NULL information could not be retrieved.
404 @return the information about the file
409 IN SHELL_FILE_HANDLE FileHandle
412 return (FileFunctionMap
.GetFileInfo(FileHandle
));
416 This function sets the information about the file for the opened handle
419 @param[in] FileHandle The file handle of the file for which information
422 @param[in] FileInfo The information to set.
424 @retval EFI_SUCCESS The information was set.
425 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
426 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
427 @retval EFI_NO_MEDIA The device has no medium.
428 @retval EFI_DEVICE_ERROR The device reported an error.
429 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
430 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
431 @retval EFI_ACCESS_DENIED The file was opened read only.
432 @retval EFI_VOLUME_FULL The volume is full.
437 IN SHELL_FILE_HANDLE FileHandle
,
438 IN EFI_FILE_INFO
*FileInfo
441 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
445 This function will open a file or directory referenced by DevicePath.
447 This function opens a file with the open mode according to the file path. The
448 Attributes is valid only for EFI_FILE_MODE_CREATE.
450 @param FilePath on input the device path to the file. On output
451 the remaining device path.
452 @param DeviceHandle pointer to the system device handle.
453 @param FileHandle pointer to the file handle.
454 @param OpenMode the mode to open the file with.
455 @param Attributes the file's file attributes.
457 @retval EFI_SUCCESS The information was set.
458 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
459 @retval EFI_UNSUPPORTED Could not open the file path.
460 @retval EFI_NOT_FOUND The specified file could not be found on the
461 device or the file system could not be found on
463 @retval EFI_NO_MEDIA The device has no medium.
464 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
465 medium is no longer supported.
466 @retval EFI_DEVICE_ERROR The device reported an error.
467 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
468 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
469 @retval EFI_ACCESS_DENIED The file was opened read only.
470 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
472 @retval EFI_VOLUME_FULL The volume is full.
476 ShellOpenFileByDevicePath(
477 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
478 OUT EFI_HANDLE
*DeviceHandle
,
479 OUT SHELL_FILE_HANDLE
*FileHandle
,
486 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
487 EFI_FILE_PROTOCOL
*Handle1
;
488 EFI_FILE_PROTOCOL
*Handle2
;
489 CHAR16
*FnafPathName
;
492 if (FilePath
== NULL
|| FileHandle
== NULL
|| DeviceHandle
== NULL
) {
493 return (EFI_INVALID_PARAMETER
);
497 // which shell interface should we use
499 if (gEfiShellProtocol
!= NULL
) {
501 // use UEFI Shell 2.0 method.
503 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
504 if (FileName
== NULL
) {
505 return (EFI_INVALID_PARAMETER
);
507 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
514 // use old shell method.
516 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
519 if (EFI_ERROR (Status
)) {
522 Status
= gBS
->OpenProtocol(*DeviceHandle
,
523 &gEfiSimpleFileSystemProtocolGuid
,
524 (VOID
**)&EfiSimpleFileSystemProtocol
,
527 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
528 if (EFI_ERROR (Status
)) {
531 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
532 if (EFI_ERROR (Status
)) {
538 // go down directories one node at a time.
540 while (!IsDevicePathEnd (*FilePath
)) {
542 // For file system access each node should be a file path component
544 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
545 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
548 return (EFI_INVALID_PARAMETER
);
551 // Open this file path node
557 // File Name Alignment Fix (FNAF)
558 // Handle2->Open may be incapable of handling a unaligned CHAR16 data.
559 // The structure pointed to by FilePath may be not CHAR16 aligned.
560 // This code copies the potentially unaligned PathName data from the
561 // FilePath structure to the aligned FnafPathName for use in the
562 // calls to Handl2->Open.
566 // Determine length of PathName, in bytes.
568 PathLen
= DevicePathNodeLength (*FilePath
) - SIZE_OF_FILEPATH_DEVICE_PATH
;
571 // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment
572 // Copy bytes from possibly unaligned location to aligned location
574 FnafPathName
= AllocateCopyPool(PathLen
, (UINT8
*)((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
);
575 if (FnafPathName
== NULL
) {
576 return EFI_OUT_OF_RESOURCES
;
580 // Try to test opening an existing file
582 Status
= Handle2
->Open (
586 OpenMode
&~EFI_FILE_MODE_CREATE
,
591 // see if the error was that it needs to be created
593 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
594 Status
= Handle2
->Open (
604 // Free the alignment buffer
606 FreePool(FnafPathName
);
609 // Close the last node
611 Handle2
->Close (Handle2
);
613 if (EFI_ERROR(Status
)) {
620 *FilePath
= NextDevicePathNode (*FilePath
);
624 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
626 *FileHandle
= (VOID
*)Handle1
;
627 return (EFI_SUCCESS
);
631 This function will open a file or directory referenced by filename.
633 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
634 otherwise, the Filehandle is NULL. The Attributes is valid only for
635 EFI_FILE_MODE_CREATE.
637 if FileName is NULL then ASSERT()
639 @param FileName pointer to file name
640 @param FileHandle pointer to the file handle.
641 @param OpenMode the mode to open the file with.
642 @param Attributes the file's file attributes.
644 @retval EFI_SUCCESS The information was set.
645 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
646 @retval EFI_UNSUPPORTED Could not open the file path.
647 @retval EFI_NOT_FOUND The specified file could not be found on the
648 device or the file system could not be found
650 @retval EFI_NO_MEDIA The device has no medium.
651 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
652 medium is no longer supported.
653 @retval EFI_DEVICE_ERROR The device reported an error.
654 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
655 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
656 @retval EFI_ACCESS_DENIED The file was opened read only.
657 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
659 @retval EFI_VOLUME_FULL The volume is full.
664 IN CONST CHAR16
*FileName
,
665 OUT SHELL_FILE_HANDLE
*FileHandle
,
670 EFI_HANDLE DeviceHandle
;
671 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
673 EFI_FILE_INFO
*FileInfo
;
676 // ASSERT if FileName is NULL
678 ASSERT(FileName
!= NULL
);
680 if (FileName
== NULL
) {
681 return (EFI_INVALID_PARAMETER
);
684 if (gEfiShellProtocol
!= NULL
) {
685 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
&& (Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
686 return ShellCreateDirectory(FileName
, FileHandle
);
689 // Use UEFI Shell 2.0 method
691 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
694 if (StrCmp(FileName
, L
"NUL") != 0 && !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
695 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
696 ASSERT(FileInfo
!= NULL
);
697 FileInfo
->Attribute
= Attributes
;
698 Status
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
704 // Using EFI Shell version
705 // this means convert name to path and call that function
706 // since this will use EFI method again that will open it.
708 ASSERT(mEfiShellEnvironment2
!= NULL
);
709 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
710 if (FilePath
!= NULL
) {
711 return (ShellOpenFileByDevicePath(&FilePath
,
717 return (EFI_DEVICE_ERROR
);
720 This function create a directory
722 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
723 otherwise, the Filehandle is NULL. If the directory already existed, this
724 function opens the existing directory.
726 @param DirectoryName pointer to directory name
727 @param FileHandle pointer to the file handle.
729 @retval EFI_SUCCESS The information was set.
730 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
731 @retval EFI_UNSUPPORTED Could not open the file path.
732 @retval EFI_NOT_FOUND The specified file could not be found on the
733 device or the file system could not be found
735 @retval EFI_NO_MEDIA The device has no medium.
736 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
737 medium is no longer supported.
738 @retval EFI_DEVICE_ERROR The device reported an error.
739 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
740 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
741 @retval EFI_ACCESS_DENIED The file was opened read only.
742 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
744 @retval EFI_VOLUME_FULL The volume is full.
745 @sa ShellOpenFileByName
749 ShellCreateDirectory(
750 IN CONST CHAR16
*DirectoryName
,
751 OUT SHELL_FILE_HANDLE
*FileHandle
754 if (gEfiShellProtocol
!= NULL
) {
756 // Use UEFI Shell 2.0 method
758 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
763 return (ShellOpenFileByName(DirectoryName
,
765 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
772 This function reads information from an opened file.
774 If FileHandle is not a directory, the function reads the requested number of
775 bytes from the file at the file's current position and returns them in Buffer.
776 If the read goes beyond the end of the file, the read length is truncated to the
777 end of the file. The file's current position is increased by the number of bytes
778 returned. If FileHandle is a directory, the function reads the directory entry
779 at the file's current position and returns the entry in Buffer. If the Buffer
780 is not large enough to hold the current directory entry, then
781 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
782 BufferSize is set to be the size of the buffer needed to read the entry. On
783 success, the current position is updated to the next directory entry. If there
784 are no more directory entries, the read returns a zero-length buffer.
785 EFI_FILE_INFO is the structure returned as the directory entry.
787 @param FileHandle the opened file handle
788 @param BufferSize on input the size of buffer in bytes. on return
789 the number of bytes written.
790 @param Buffer the buffer to put read data into.
792 @retval EFI_SUCCESS Data was read.
793 @retval EFI_NO_MEDIA The device has no media.
794 @retval EFI_DEVICE_ERROR The device reported an error.
795 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
796 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
803 IN SHELL_FILE_HANDLE FileHandle
,
804 IN OUT UINTN
*BufferSize
,
808 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
813 Write data to a file.
815 This function writes the specified number of bytes to the file at the current
816 file position. The current file position is advanced the actual number of bytes
817 written, which is returned in BufferSize. Partial writes only occur when there
818 has been a data error during the write attempt (such as "volume space full").
819 The file is automatically grown to hold the data if required. Direct writes to
820 opened directories are not supported.
822 @param FileHandle The opened file for writing
823 @param BufferSize on input the number of bytes in Buffer. On output
824 the number of bytes written.
825 @param Buffer the buffer containing data to write is stored.
827 @retval EFI_SUCCESS Data was written.
828 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
829 @retval EFI_NO_MEDIA The device has no media.
830 @retval EFI_DEVICE_ERROR The device reported an error.
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
832 @retval EFI_WRITE_PROTECTED The device is write-protected.
833 @retval EFI_ACCESS_DENIED The file was open for read only.
834 @retval EFI_VOLUME_FULL The volume is full.
839 IN SHELL_FILE_HANDLE FileHandle
,
840 IN OUT UINTN
*BufferSize
,
844 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
848 Close an open file handle.
850 This function closes a specified file handle. All "dirty" cached file data is
851 flushed to the device, and the file is closed. In all cases the handle is
854 @param FileHandle the file handle to close.
856 @retval EFI_SUCCESS the file handle was closed sucessfully.
861 IN SHELL_FILE_HANDLE
*FileHandle
864 return (FileFunctionMap
.CloseFile(*FileHandle
));
868 Delete a file and close the handle
870 This function closes and deletes a file. In all cases the file handle is closed.
871 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
872 returned, but the handle is still closed.
874 @param FileHandle the file handle to delete
876 @retval EFI_SUCCESS the file was closed sucessfully
877 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
879 @retval INVALID_PARAMETER One of the parameters has an invalid value.
884 IN SHELL_FILE_HANDLE
*FileHandle
887 return (FileFunctionMap
.DeleteFile(*FileHandle
));
891 Set the current position in a file.
893 This function sets the current file position for the handle to the position
894 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
895 absolute positioning is supported, and seeking past the end of the file is
896 allowed (a subsequent write would grow the file). Seeking to position
897 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
898 If FileHandle is a directory, the only position that may be set is zero. This
899 has the effect of starting the read process of the directory entries over.
901 @param FileHandle The file handle on which the position is being set
902 @param Position Byte position from begining of file
904 @retval EFI_SUCCESS Operation completed sucessfully.
905 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
907 @retval INVALID_PARAMETER One of the parameters has an invalid value.
911 ShellSetFilePosition (
912 IN SHELL_FILE_HANDLE FileHandle
,
916 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
920 Gets a file's current position
922 This function retrieves the current file position for the file handle. For
923 directories, the current file position has no meaning outside of the file
924 system driver and as such the operation is not supported. An error is returned
925 if FileHandle is a directory.
927 @param FileHandle The open file handle on which to get the position.
928 @param Position Byte position from begining of file.
930 @retval EFI_SUCCESS the operation completed sucessfully.
931 @retval INVALID_PARAMETER One of the parameters has an invalid value.
932 @retval EFI_UNSUPPORTED the request is not valid on directories.
936 ShellGetFilePosition (
937 IN SHELL_FILE_HANDLE FileHandle
,
941 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
944 Flushes data on a file
946 This function flushes all modified data associated with a file to a device.
948 @param FileHandle The file handle on which to flush data
950 @retval EFI_SUCCESS The data was flushed.
951 @retval EFI_NO_MEDIA The device has no media.
952 @retval EFI_DEVICE_ERROR The device reported an error.
953 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
954 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
955 @retval EFI_ACCESS_DENIED The file was opened for read only.
960 IN SHELL_FILE_HANDLE FileHandle
963 return (FileFunctionMap
.FlushFile(FileHandle
));
966 /** Retrieve first entry from a directory.
968 This function takes an open directory handle and gets information from the
969 first entry in the directory. A buffer is allocated to contain
970 the information and a pointer to the buffer is returned in *Buffer. The
971 caller can use ShellFindNextFile() to get subsequent directory entries.
973 The buffer will be freed by ShellFindNextFile() when the last directory
974 entry is read. Otherwise, the caller must free the buffer, using FreePool,
975 when finished with it.
977 @param[in] DirHandle The file handle of the directory to search.
978 @param[out] Buffer The pointer to the buffer for the file's information.
980 @retval EFI_SUCCESS Found the first file.
981 @retval EFI_NOT_FOUND Cannot find the directory.
982 @retval EFI_NO_MEDIA The device has no media.
983 @retval EFI_DEVICE_ERROR The device reported an error.
984 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
985 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
991 IN SHELL_FILE_HANDLE DirHandle
,
992 OUT EFI_FILE_INFO
**Buffer
996 // pass to file handle lib
998 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1000 /** Retrieve next entries from a directory.
1002 To use this function, the caller must first call the ShellFindFirstFile()
1003 function to get the first directory entry. Subsequent directory entries are
1004 retrieved by using the ShellFindNextFile() function. This function can
1005 be called several times to get each entry from the directory. If the call of
1006 ShellFindNextFile() retrieved the last directory entry, the next call of
1007 this function will set *NoFile to TRUE and free the buffer.
1009 @param[in] DirHandle The file handle of the directory.
1010 @param[out] Buffer The pointer to buffer for file's information.
1011 @param[out] NoFile The pointer to boolean when last file is found.
1013 @retval EFI_SUCCESS Found the next file, or reached last file
1014 @retval EFI_NO_MEDIA The device has no media.
1015 @retval EFI_DEVICE_ERROR The device reported an error.
1016 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1021 IN SHELL_FILE_HANDLE DirHandle
,
1022 OUT EFI_FILE_INFO
*Buffer
,
1027 // pass to file handle lib
1029 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1032 Retrieve the size of a file.
1034 if FileHandle is NULL then ASSERT()
1035 if Size is NULL then ASSERT()
1037 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1040 @param FileHandle file handle from which size is retrieved
1041 @param Size pointer to size
1043 @retval EFI_SUCCESS operation was completed sucessfully
1044 @retval EFI_DEVICE_ERROR cannot access the file
1049 IN SHELL_FILE_HANDLE FileHandle
,
1053 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1056 Retrieves the status of the break execution flag
1058 this function is useful to check whether the application is being asked to halt by the shell.
1060 @retval TRUE the execution break is enabled
1061 @retval FALSE the execution break is not enabled
1065 ShellGetExecutionBreakFlag(
1070 // Check for UEFI Shell 2.0 protocols
1072 if (gEfiShellProtocol
!= NULL
) {
1075 // We are using UEFI Shell 2.0; see if the event has been triggered
1077 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1084 // using EFI Shell; call the function to check
1086 if (mEfiShellEnvironment2
!= NULL
) {
1087 return (mEfiShellEnvironment2
->GetExecutionBreak());
1093 return the value of an environment variable
1095 this function gets the value of the environment variable set by the
1096 ShellSetEnvironmentVariable function
1098 @param EnvKey The key name of the environment variable.
1100 @retval NULL the named environment variable does not exist.
1101 @return != NULL pointer to the value of the environment variable
1105 ShellGetEnvironmentVariable (
1106 IN CONST CHAR16
*EnvKey
1110 // Check for UEFI Shell 2.0 protocols
1112 if (gEfiShellProtocol
!= NULL
) {
1113 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1117 // Check for EFI shell
1119 if (mEfiShellEnvironment2
!= NULL
) {
1120 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1126 set the value of an environment variable
1128 This function changes the current value of the specified environment variable. If the
1129 environment variable exists and the Value is an empty string, then the environment
1130 variable is deleted. If the environment variable exists and the Value is not an empty
1131 string, then the value of the environment variable is changed. If the environment
1132 variable does not exist and the Value is an empty string, there is no action. If the
1133 environment variable does not exist and the Value is a non-empty string, then the
1134 environment variable is created and assigned the specified value.
1136 This is not supported pre-UEFI Shell 2.0.
1138 @param EnvKey The key name of the environment variable.
1139 @param EnvVal The Value of the environment variable
1140 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1142 @retval EFI_SUCCESS the operation was completed sucessfully
1143 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1147 ShellSetEnvironmentVariable (
1148 IN CONST CHAR16
*EnvKey
,
1149 IN CONST CHAR16
*EnvVal
,
1154 // Check for UEFI Shell 2.0 protocols
1156 if (gEfiShellProtocol
!= NULL
) {
1157 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1161 // This feature does not exist under EFI shell
1163 return (EFI_UNSUPPORTED
);
1167 Cause the shell to parse and execute a command line.
1169 This function creates a nested instance of the shell and executes the specified
1170 command (CommandLine) with the specified environment (Environment). Upon return,
1171 the status code returned by the specified command is placed in StatusCode.
1172 If Environment is NULL, then the current environment is used and all changes made
1173 by the commands executed will be reflected in the current environment. If the
1174 Environment is non-NULL, then the changes made will be discarded.
1175 The CommandLine is executed from the current working directory on the current
1178 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1179 environment. The values pointed to by the parameters will be unchanged by the
1180 ShellExecute() function. The Output parameter has no effect in a
1181 UEFI Shell 2.0 environment.
1183 @param[in] ParentHandle The parent image starting the operation.
1184 @param[in] CommandLine The pointer to a NULL terminated command line.
1185 @param[in] Output True to display debug output. False to hide it.
1186 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1187 in the form "x=y". If NULL, the current set is used.
1188 @param[out] Status The status of the run command line.
1190 @retval EFI_SUCCESS The operation completed sucessfully. Status
1191 contains the status code returned.
1192 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1193 @retval EFI_OUT_OF_RESOURCES Out of resources.
1194 @retval EFI_UNSUPPORTED The operation is not allowed.
1199 IN EFI_HANDLE
*ParentHandle
,
1200 IN CHAR16
*CommandLine OPTIONAL
,
1201 IN BOOLEAN Output OPTIONAL
,
1202 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1203 OUT EFI_STATUS
*Status OPTIONAL
1206 EFI_STATUS CmdStatus
;
1208 // Check for UEFI Shell 2.0 protocols
1210 if (gEfiShellProtocol
!= NULL
) {
1212 // Call UEFI Shell 2.0 version (not using Output parameter)
1214 return (gEfiShellProtocol
->Execute(ParentHandle
,
1216 EnvironmentVariables
,
1221 // Check for EFI shell
1223 if (mEfiShellEnvironment2
!= NULL
) {
1225 // Call EFI Shell version.
1226 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1228 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1232 // No Status output parameter so just use the returned status
1234 if (Status
!= NULL
) {
1235 *Status
= CmdStatus
;
1238 // If there was an error, we can't tell if it was from the command or from
1239 // the Execute() function, so we'll just assume the shell ran successfully
1240 // and the error came from the command.
1245 return (EFI_UNSUPPORTED
);
1249 Retreives the current directory path
1251 If the DeviceName is NULL, it returns the current device's current directory
1252 name. If the DeviceName is not NULL, it returns the current directory name
1255 @param DeviceName the name of the drive to get directory on
1257 @retval NULL the directory does not exist
1258 @return != NULL the directory
1262 ShellGetCurrentDir (
1263 IN CHAR16
* CONST DeviceName OPTIONAL
1267 // Check for UEFI Shell 2.0 protocols
1269 if (gEfiShellProtocol
!= NULL
) {
1270 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1274 // Check for EFI shell
1276 if (mEfiShellEnvironment2
!= NULL
) {
1277 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1283 sets (enabled or disabled) the page break mode
1285 when page break mode is enabled the screen will stop scrolling
1286 and wait for operator input before scrolling a subsequent screen.
1288 @param CurrentState TRUE to enable and FALSE to disable
1292 ShellSetPageBreakMode (
1293 IN BOOLEAN CurrentState
1297 // check for enabling
1299 if (CurrentState
!= 0x00) {
1301 // check for UEFI Shell 2.0
1303 if (gEfiShellProtocol
!= NULL
) {
1305 // Enable with UEFI 2.0 Shell
1307 gEfiShellProtocol
->EnablePageBreak();
1311 // Check for EFI shell
1313 if (mEfiShellEnvironment2
!= NULL
) {
1315 // Enable with EFI Shell
1317 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1323 // check for UEFI Shell 2.0
1325 if (gEfiShellProtocol
!= NULL
) {
1327 // Disable with UEFI 2.0 Shell
1329 gEfiShellProtocol
->DisablePageBreak();
1333 // Check for EFI shell
1335 if (mEfiShellEnvironment2
!= NULL
) {
1337 // Disable with EFI Shell
1339 mEfiShellEnvironment2
->DisablePageBreak ();
1347 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1348 /// This allows for the struct to be populated.
1355 SHELL_FILE_HANDLE Handle
;
1356 EFI_FILE_INFO
*Info
;
1357 } EFI_SHELL_FILE_INFO_NO_CONST
;
1360 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1362 if OldStyleFileList is NULL then ASSERT()
1364 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1365 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1366 the ShellCloseFileMetaArg function.
1368 @param[in] FileList the EFI shell list type
1369 @param[in, out] ListHead the list to add to
1371 @retval the resultant head of the double linked new format list;
1375 InternalShellConvertFileListType (
1376 IN LIST_ENTRY
*FileList
,
1377 IN OUT LIST_ENTRY
*ListHead
1380 SHELL_FILE_ARG
*OldInfo
;
1382 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1387 ASSERT(FileList
!= NULL
);
1388 ASSERT(ListHead
!= NULL
);
1391 // enumerate through each member of the old list and copy
1393 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1394 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1395 ASSERT(OldInfo
!= NULL
);
1398 // Skip ones that failed to open...
1400 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1405 // make sure the old list was valid
1407 ASSERT(OldInfo
->Info
!= NULL
);
1408 ASSERT(OldInfo
->FullName
!= NULL
);
1409 ASSERT(OldInfo
->FileName
!= NULL
);
1412 // allocate a new EFI_SHELL_FILE_INFO object
1414 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1415 if (NewInfo
== NULL
) {
1416 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1422 // copy the simple items
1424 NewInfo
->Handle
= OldInfo
->Handle
;
1425 NewInfo
->Status
= OldInfo
->Status
;
1427 // old shell checks for 0 not NULL
1428 OldInfo
->Handle
= 0;
1431 // allocate new space to copy strings and structure
1433 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1434 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1435 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1438 // make sure all the memory allocations were sucessful
1440 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1441 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1447 // Copt the strings and structure
1449 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1450 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1451 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1454 // add that to the list
1456 InsertTailList(ListHead
, &NewInfo
->Link
);
1461 Opens a group of files based on a path.
1463 This function uses the Arg to open all the matching files. Each matched
1464 file has a SHELL_FILE_INFO structure to record the file information. These
1465 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1466 structures from ListHead to access each file. This function supports wildcards
1467 and will process '?' and '*' as such. the list must be freed with a call to
1468 ShellCloseFileMetaArg().
1470 If you are NOT appending to an existing list *ListHead must be NULL. If
1471 *ListHead is NULL then it must be callee freed.
1473 @param Arg pointer to path string
1474 @param OpenMode mode to open files with
1475 @param ListHead head of linked list of results
1477 @retval EFI_SUCCESS the operation was sucessful and the list head
1478 contains the list of opened files
1479 @return != EFI_SUCCESS the operation failed
1481 @sa InternalShellConvertFileListType
1485 ShellOpenFileMetaArg (
1488 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1492 LIST_ENTRY mOldStyleFileList
;
1495 // ASSERT that Arg and ListHead are not NULL
1497 ASSERT(Arg
!= NULL
);
1498 ASSERT(ListHead
!= NULL
);
1501 // Check for UEFI Shell 2.0 protocols
1503 if (gEfiShellProtocol
!= NULL
) {
1504 if (*ListHead
== NULL
) {
1505 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1506 if (*ListHead
== NULL
) {
1507 return (EFI_OUT_OF_RESOURCES
);
1509 InitializeListHead(&((*ListHead
)->Link
));
1511 Status
= gEfiShellProtocol
->OpenFileList(Arg
,
1514 if (EFI_ERROR(Status
)) {
1515 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1517 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1519 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1520 FreePool(*ListHead
);
1522 return (EFI_NOT_FOUND
);
1528 // Check for EFI shell
1530 if (mEfiShellEnvironment2
!= NULL
) {
1532 // make sure the list head is initialized
1534 InitializeListHead(&mOldStyleFileList
);
1537 // Get the EFI Shell list of files
1539 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1540 if (EFI_ERROR(Status
)) {
1545 if (*ListHead
== NULL
) {
1546 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1547 if (*ListHead
== NULL
) {
1548 return (EFI_OUT_OF_RESOURCES
);
1550 InitializeListHead(&((*ListHead
)->Link
));
1554 // Convert that to equivalent of UEFI Shell 2.0 structure
1556 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1559 // Free the EFI Shell version that was converted.
1561 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1563 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1564 FreePool(*ListHead
);
1566 Status
= EFI_NOT_FOUND
;
1571 return (EFI_UNSUPPORTED
);
1574 Free the linked list returned from ShellOpenFileMetaArg.
1576 if ListHead is NULL then ASSERT().
1578 @param ListHead the pointer to free.
1580 @retval EFI_SUCCESS the operation was sucessful.
1584 ShellCloseFileMetaArg (
1585 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1591 // ASSERT that ListHead is not NULL
1593 ASSERT(ListHead
!= NULL
);
1596 // Check for UEFI Shell 2.0 protocols
1598 if (gEfiShellProtocol
!= NULL
) {
1599 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1600 } else if (mEfiShellEnvironment2
!= NULL
) {
1602 // Since this is EFI Shell version we need to free our internally made copy
1605 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1606 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1607 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1608 RemoveEntryList(Node
);
1609 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1610 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1611 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1612 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1613 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1615 SHELL_FREE_NON_NULL(*ListHead
);
1619 return (EFI_UNSUPPORTED
);
1623 Find a file by searching the CWD and then the path.
1625 If FileName is NULL then ASSERT.
1627 If the return value is not NULL then the memory must be caller freed.
1629 @param FileName Filename string.
1631 @retval NULL the file was not found
1632 @return !NULL the full path to the file.
1637 IN CONST CHAR16
*FileName
1641 SHELL_FILE_HANDLE Handle
;
1645 CONST CHAR16
*Walker
;
1652 // First make sure its not an absolute path.
1654 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1655 if (!EFI_ERROR(Status
)){
1656 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1657 ASSERT(RetVal
== NULL
);
1658 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1659 ShellCloseFile(&Handle
);
1662 ShellCloseFile(&Handle
);
1666 Path
= ShellGetEnvironmentVariable(L
"cwd");
1668 Size
= StrSize(Path
);
1669 Size
+= StrSize(FileName
);
1670 TestPath
= AllocateZeroPool(Size
);
1671 if (TestPath
== NULL
) {
1674 StrCpy(TestPath
, Path
);
1675 StrCat(TestPath
, FileName
);
1676 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1677 if (!EFI_ERROR(Status
)){
1678 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1679 ASSERT(RetVal
== NULL
);
1680 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1681 ShellCloseFile(&Handle
);
1685 ShellCloseFile(&Handle
);
1690 Path
= ShellGetEnvironmentVariable(L
"path");
1692 Size
= StrSize(Path
)+sizeof(CHAR16
);
1693 Size
+= StrSize(FileName
);
1694 TestPath
= AllocateZeroPool(Size
);
1695 if (TestPath
== NULL
) {
1698 Walker
= (CHAR16
*)Path
;
1700 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1701 if (TestPath
!= NULL
) {
1702 TempChar
= StrStr(TestPath
, L
";");
1703 if (TempChar
!= NULL
) {
1704 *TempChar
= CHAR_NULL
;
1706 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1707 StrCat(TestPath
, L
"\\");
1709 if (FileName
[0] == L
'\\') {
1712 StrCat(TestPath
, FileName
);
1713 if (StrStr(Walker
, L
";") != NULL
) {
1714 Walker
= StrStr(Walker
, L
";") + 1;
1718 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1719 if (!EFI_ERROR(Status
)){
1720 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1721 ASSERT(RetVal
== NULL
);
1722 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1723 ShellCloseFile(&Handle
);
1726 ShellCloseFile(&Handle
);
1730 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1737 Find a file by searching the CWD and then the path with a variable set of file
1738 extensions. If the file is not found it will append each extension in the list
1739 in the order provided and return the first one that is successful.
1741 If FileName is NULL, then ASSERT.
1742 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1744 If the return value is not NULL then the memory must be caller freed.
1746 @param[in] FileName Filename string.
1747 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1749 @retval NULL The file was not found.
1750 @retval !NULL The path to the file.
1754 ShellFindFilePathEx (
1755 IN CONST CHAR16
*FileName
,
1756 IN CONST CHAR16
*FileExtension
1761 CONST CHAR16
*ExtensionWalker
;
1766 ASSERT(FileName
!= NULL
);
1767 if (FileExtension
== NULL
) {
1768 return (ShellFindFilePath(FileName
));
1770 RetVal
= ShellFindFilePath(FileName
);
1771 if (RetVal
!= NULL
) {
1774 Size
= StrSize(FileName
);
1775 Size
+= StrSize(FileExtension
);
1776 TestPath
= AllocateZeroPool(Size
);
1777 if (TestPath
== NULL
) {
1780 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1781 StrCpy(TestPath
, FileName
);
1782 if (ExtensionWalker
!= NULL
) {
1783 StrCat(TestPath
, ExtensionWalker
);
1785 TempChar
= StrStr(TestPath
, L
";");
1786 if (TempChar
!= NULL
) {
1787 *TempChar
= CHAR_NULL
;
1789 RetVal
= ShellFindFilePath(TestPath
);
1790 if (RetVal
!= NULL
) {
1793 ASSERT(ExtensionWalker
!= NULL
);
1794 TempChar2
= StrStr(ExtensionWalker
, L
";");
1803 SHELL_PARAM_TYPE Type
;
1805 UINTN OriginalPosition
;
1806 } SHELL_PARAM_PACKAGE
;
1809 Checks the list of valid arguments and returns TRUE if the item was found. If the
1810 return value is TRUE then the type parameter is set also.
1812 if CheckList is NULL then ASSERT();
1813 if Name is NULL then ASSERT();
1814 if Type is NULL then ASSERT();
1816 @param Name pointer to Name of parameter found
1817 @param CheckList List to check against
1818 @param Type pointer to type of parameter if it was found
1820 @retval TRUE the Parameter was found. Type is valid.
1821 @retval FALSE the Parameter was not found. Type is not valid.
1825 InternalIsOnCheckList (
1826 IN CONST CHAR16
*Name
,
1827 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1828 OUT SHELL_PARAM_TYPE
*Type
1831 SHELL_PARAM_ITEM
*TempListItem
;
1835 // ASSERT that all 3 pointer parameters aren't NULL
1837 ASSERT(CheckList
!= NULL
);
1838 ASSERT(Type
!= NULL
);
1839 ASSERT(Name
!= NULL
);
1842 // question mark and page break mode are always supported
1844 if ((StrCmp(Name
, L
"-?") == 0) ||
1845 (StrCmp(Name
, L
"-b") == 0)
1852 // Enumerate through the list
1854 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1856 // If the Type is TypeStart only check the first characters of the passed in param
1857 // If it matches set the type and return TRUE
1859 if (TempListItem
->Type
== TypeStart
) {
1860 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1861 *Type
= TempListItem
->Type
;
1865 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1866 if (TempString
!= NULL
) {
1867 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1868 *Type
= TempListItem
->Type
;
1869 FreePool(TempString
);
1872 FreePool(TempString
);
1874 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1875 *Type
= TempListItem
->Type
;
1883 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1885 @param[in] Name pointer to Name of parameter found
1886 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1888 @retval TRUE the Parameter is a flag.
1889 @retval FALSE the Parameter not a flag.
1894 IN CONST CHAR16
*Name
,
1895 IN BOOLEAN AlwaysAllowNumbers
1899 // ASSERT that Name isn't NULL
1901 ASSERT(Name
!= NULL
);
1904 // If we accept numbers then dont return TRUE. (they will be values)
1906 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && ShellIsHexaDecimalDigitCharacter(Name
[1])) && AlwaysAllowNumbers
) {
1911 // If the Name has a /, +, or - as the first character return TRUE
1913 if ((Name
[0] == L
'/') ||
1914 (Name
[0] == L
'-') ||
1923 Checks the command line arguments passed against the list of valid ones.
1925 If no initialization is required, then return RETURN_SUCCESS.
1927 @param[in] CheckList pointer to list of parameters to check
1928 @param[out] CheckPackage pointer to pointer to list checked values
1929 @param[out] ProblemParam optional pointer to pointer to unicode string for
1930 the paramater that caused failure. If used then the
1931 caller is responsible for freeing the memory.
1932 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1933 @param[in] Argv pointer to array of parameters
1934 @param[in] Argc Count of parameters in Argv
1935 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
1937 @retval EFI_SUCCESS The operation completed sucessfully.
1938 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1939 @retval EFI_INVALID_PARAMETER A parameter was invalid
1940 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1941 duplicated. the duplicated command line argument
1942 was returned in ProblemParam if provided.
1943 @retval EFI_NOT_FOUND a argument required a value that was missing.
1944 the invalid command line argument was returned in
1945 ProblemParam if provided.
1949 InternalCommandLineParse (
1950 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1951 OUT LIST_ENTRY
**CheckPackage
,
1952 OUT CHAR16
**ProblemParam OPTIONAL
,
1953 IN BOOLEAN AutoPageBreak
,
1954 IN CONST CHAR16
**Argv
,
1956 IN BOOLEAN AlwaysAllowNumbers
1960 SHELL_PARAM_TYPE CurrentItemType
;
1961 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1965 CONST CHAR16
*TempPointer
;
1967 CurrentItemPackage
= NULL
;
1973 // If there is only 1 item we dont need to do anything
1976 *CheckPackage
= NULL
;
1977 return (EFI_SUCCESS
);
1983 ASSERT(CheckList
!= NULL
);
1984 ASSERT(Argv
!= NULL
);
1987 // initialize the linked list
1989 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1990 if (*CheckPackage
== NULL
) {
1991 return (EFI_OUT_OF_RESOURCES
);
1994 InitializeListHead(*CheckPackage
);
1997 // loop through each of the arguments
1999 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2000 if (Argv
[LoopCounter
] == NULL
) {
2002 // do nothing for NULL argv
2004 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2006 // We might have leftover if last parameter didnt have optional value
2008 if (GetItemValue
!= 0) {
2010 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2015 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2016 if (CurrentItemPackage
== NULL
) {
2017 ShellCommandLineFreeVarList(*CheckPackage
);
2018 *CheckPackage
= NULL
;
2019 return (EFI_OUT_OF_RESOURCES
);
2021 CurrentItemPackage
->Name
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
2022 if (CurrentItemPackage
->Name
== NULL
) {
2023 ShellCommandLineFreeVarList(*CheckPackage
);
2024 *CheckPackage
= NULL
;
2025 return (EFI_OUT_OF_RESOURCES
);
2027 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
2028 CurrentItemPackage
->Type
= CurrentItemType
;
2029 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2030 CurrentItemPackage
->Value
= NULL
;
2033 // Does this flag require a value
2035 switch (CurrentItemPackage
->Type
) {
2037 // possibly trigger the next loop(s) to populate the value of this item
2043 case TypeDoubleValue
:
2048 GetItemValue
= (UINTN
)(-1);
2053 // this item has no value expected; we are done
2055 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2056 ASSERT(GetItemValue
== 0);
2059 } else if (GetItemValue
!= 0 && !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
)) {
2060 ASSERT(CurrentItemPackage
!= NULL
);
2062 // get the item VALUE for a previous flag
2064 if (StrStr(Argv
[LoopCounter
], L
" ") == NULL
) {
2065 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
2066 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2067 if (ValueSize
== 0) {
2068 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2070 StrCat(CurrentItemPackage
->Value
, L
" ");
2071 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2073 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2076 // the parameter has spaces. must be quoted.
2078 CurrentItemPackage
->Value
= ReallocatePool(ValueSize
, ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
) + sizeof(CHAR16
) + sizeof(CHAR16
), CurrentItemPackage
->Value
);
2079 ASSERT(CurrentItemPackage
->Value
!= NULL
);
2080 if (ValueSize
== 0) {
2081 StrCpy(CurrentItemPackage
->Value
, L
"\"");
2082 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2083 StrCat(CurrentItemPackage
->Value
, L
"\"");
2085 StrCat(CurrentItemPackage
->Value
, L
" ");
2086 StrCat(CurrentItemPackage
->Value
, L
"\"");
2087 StrCat(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
2088 StrCat(CurrentItemPackage
->Value
, L
"\"");
2090 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2093 if (GetItemValue
== 0) {
2094 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2096 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
) ){ //|| ProblemParam == NULL) {
2098 // add this one as a non-flag
2101 TempPointer
= Argv
[LoopCounter
];
2102 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2103 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2104 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2108 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2109 if (CurrentItemPackage
== NULL
) {
2110 ShellCommandLineFreeVarList(*CheckPackage
);
2111 *CheckPackage
= NULL
;
2112 return (EFI_OUT_OF_RESOURCES
);
2114 CurrentItemPackage
->Name
= NULL
;
2115 CurrentItemPackage
->Type
= TypePosition
;
2116 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(TempPointer
));
2117 if (CurrentItemPackage
->Value
== NULL
) {
2118 ShellCommandLineFreeVarList(*CheckPackage
);
2119 *CheckPackage
= NULL
;
2120 return (EFI_OUT_OF_RESOURCES
);
2122 StrCpy(CurrentItemPackage
->Value
, TempPointer
);
2123 CurrentItemPackage
->OriginalPosition
= Count
++;
2124 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2127 // this was a non-recognised flag... error!
2129 if (ProblemParam
!= NULL
) {
2130 *ProblemParam
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
2131 if (*ProblemParam
!= NULL
) {
2132 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
2135 ShellCommandLineFreeVarList(*CheckPackage
);
2136 *CheckPackage
= NULL
;
2137 return (EFI_VOLUME_CORRUPTED
);
2140 if (GetItemValue
!= 0) {
2142 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2145 // support for AutoPageBreak
2147 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2148 ShellSetPageBreakMode(TRUE
);
2150 return (EFI_SUCCESS
);
2154 Checks the command line arguments passed against the list of valid ones.
2155 Optionally removes NULL values first.
2157 If no initialization is required, then return RETURN_SUCCESS.
2159 @param[in] CheckList The pointer to list of parameters to check.
2160 @param[out] CheckPackage The package of checked values.
2161 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2162 the paramater that caused failure.
2163 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2164 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2166 @retval EFI_SUCCESS The operation completed sucessfully.
2167 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2168 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2169 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2170 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2171 of the command line arguments was returned in
2172 ProblemParam if provided.
2173 @retval EFI_NOT_FOUND A argument required a value that was missing.
2174 The invalid command line argument was returned in
2175 ProblemParam if provided.
2179 ShellCommandLineParseEx (
2180 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2181 OUT LIST_ENTRY
**CheckPackage
,
2182 OUT CHAR16
**ProblemParam OPTIONAL
,
2183 IN BOOLEAN AutoPageBreak
,
2184 IN BOOLEAN AlwaysAllowNumbers
2188 // ASSERT that CheckList and CheckPackage aren't NULL
2190 ASSERT(CheckList
!= NULL
);
2191 ASSERT(CheckPackage
!= NULL
);
2194 // Check for UEFI Shell 2.0 protocols
2196 if (gEfiShellParametersProtocol
!= NULL
) {
2197 return (InternalCommandLineParse(CheckList
,
2201 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2202 gEfiShellParametersProtocol
->Argc
,
2203 AlwaysAllowNumbers
));
2207 // ASSERT That EFI Shell is not required
2209 ASSERT (mEfiShellInterface
!= NULL
);
2210 return (InternalCommandLineParse(CheckList
,
2214 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2215 mEfiShellInterface
->Argc
,
2216 AlwaysAllowNumbers
));
2220 Frees shell variable list that was returned from ShellCommandLineParse.
2222 This function will free all the memory that was used for the CheckPackage
2223 list of postprocessed shell arguments.
2225 this function has no return value.
2227 if CheckPackage is NULL, then return
2229 @param CheckPackage the list to de-allocate
2233 ShellCommandLineFreeVarList (
2234 IN LIST_ENTRY
*CheckPackage
2240 // check for CheckPackage == NULL
2242 if (CheckPackage
== NULL
) {
2247 // for each node in the list
2249 for ( Node
= GetFirstNode(CheckPackage
)
2250 ; !IsListEmpty(CheckPackage
)
2251 ; Node
= GetFirstNode(CheckPackage
)
2254 // Remove it from the list
2256 RemoveEntryList(Node
);
2259 // if it has a name free the name
2261 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2262 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2266 // if it has a value free the value
2268 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2269 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2273 // free the node structure
2275 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2278 // free the list head node
2280 FreePool(CheckPackage
);
2283 Checks for presence of a flag parameter
2285 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2287 if CheckPackage is NULL then return FALSE.
2288 if KeyString is NULL then ASSERT()
2290 @param CheckPackage The package of parsed command line arguments
2291 @param KeyString the Key of the command line argument to check for
2293 @retval TRUE the flag is on the command line
2294 @retval FALSE the flag is not on the command line
2298 ShellCommandLineGetFlag (
2299 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2300 IN CONST CHAR16
* CONST KeyString
2307 // return FALSE for no package or KeyString is NULL
2309 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2314 // enumerate through the list of parametrs
2316 for ( Node
= GetFirstNode(CheckPackage
)
2317 ; !IsNull (CheckPackage
, Node
)
2318 ; Node
= GetNextNode(CheckPackage
, Node
)
2321 // If the Name matches, return TRUE (and there may be NULL name)
2323 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2325 // If Type is TypeStart then only compare the begining of the strings
2327 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2328 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2332 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2333 if (TempString
!= NULL
) {
2334 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2335 FreePool(TempString
);
2338 FreePool(TempString
);
2340 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2348 Returns value from command line argument.
2350 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2352 If CheckPackage is NULL, then return NULL.
2354 @param[in] CheckPackage The package of parsed command line arguments.
2355 @param[in] KeyString The Key of the command line argument to check for.
2357 @retval NULL The flag is not on the command line.
2358 @retval !=NULL The pointer to unicode string of the value.
2362 ShellCommandLineGetValue (
2363 IN CONST LIST_ENTRY
*CheckPackage
,
2364 IN CHAR16
*KeyString
2371 // return NULL 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) {
2393 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
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
);
2400 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2402 FreePool(TempString
);
2404 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2405 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2413 Returns raw value from command line argument.
2415 Raw value parameters are in the form of "value" in a specific position in the list.
2417 If CheckPackage is NULL, then return NULL.
2419 @param[in] CheckPackage The package of parsed command line arguments.
2420 @param[in] Position The position of the value.
2422 @retval NULL The flag is not on the command line.
2423 @retval !=NULL The pointer to unicode string of the value.
2427 ShellCommandLineGetRawValue (
2428 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2435 // check for CheckPackage == NULL
2437 if (CheckPackage
== 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 position matches, return the value
2451 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2452 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2459 returns the number of command line value parameters that were parsed.
2461 this will not include flags.
2463 @param[in] CheckPackage The package of parsed command line arguments.
2465 @retval (UINTN)-1 No parsing has ocurred
2466 @return other The number of value parameters found
2470 ShellCommandLineGetCount(
2471 IN CONST LIST_ENTRY
*CheckPackage
2477 if (CheckPackage
== NULL
) {
2480 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2481 ; !IsNull (CheckPackage
, Node1
)
2482 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2484 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2492 Determins if a parameter is duplicated.
2494 If Param is not NULL then it will point to a callee allocated string buffer
2495 with the parameter value if a duplicate is found.
2497 If CheckPackage is NULL, then ASSERT.
2499 @param[in] CheckPackage The package of parsed command line arguments.
2500 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2502 @retval EFI_SUCCESS No parameters were duplicated.
2503 @retval EFI_DEVICE_ERROR A duplicate was found.
2507 ShellCommandLineCheckDuplicate (
2508 IN CONST LIST_ENTRY
*CheckPackage
,
2515 ASSERT(CheckPackage
!= NULL
);
2517 for ( Node1
= GetFirstNode(CheckPackage
)
2518 ; !IsNull (CheckPackage
, Node1
)
2519 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2521 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2522 ; !IsNull (CheckPackage
, Node2
)
2523 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2525 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) {
2526 if (Param
!= NULL
) {
2528 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2530 return (EFI_DEVICE_ERROR
);
2534 return (EFI_SUCCESS
);
2538 This is a find and replace function. Upon successful return the NewString is a copy of
2539 SourceString with each instance of FindTarget replaced with ReplaceWith.
2541 If SourceString and NewString overlap the behavior is undefined.
2543 If the string would grow bigger than NewSize it will halt and return error.
2545 @param[in] SourceString The string with source buffer.
2546 @param[in, out] NewString The string with resultant buffer.
2547 @param[in] NewSize The size in bytes of NewString.
2548 @param[in] FindTarget The string to look for.
2549 @param[in] ReplaceWith The string to replace FindTarget with.
2550 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2551 immediately before it.
2552 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2554 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2555 @retval EFI_INVALID_PARAMETER NewString was NULL.
2556 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2557 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2558 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2559 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2560 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2561 the new string (truncation occurred).
2562 @retval EFI_SUCCESS The string was successfully copied with replacement.
2566 ShellCopySearchAndReplace(
2567 IN CHAR16 CONST
*SourceString
,
2568 IN OUT CHAR16
*NewString
,
2570 IN CONST CHAR16
*FindTarget
,
2571 IN CONST CHAR16
*ReplaceWith
,
2572 IN CONST BOOLEAN SkipPreCarrot
,
2573 IN CONST BOOLEAN ParameterReplacing
2579 if ( (SourceString
== NULL
)
2580 || (NewString
== NULL
)
2581 || (FindTarget
== NULL
)
2582 || (ReplaceWith
== NULL
)
2583 || (StrLen(FindTarget
) < 1)
2584 || (StrLen(SourceString
) < 1)
2586 return (EFI_INVALID_PARAMETER
);
2589 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2590 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2592 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2593 if (Replace
!= NULL
) {
2594 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2597 if (Replace
== NULL
) {
2598 return (EFI_OUT_OF_RESOURCES
);
2600 NewString
= SetMem16(NewString
, NewSize
, CHAR_NULL
);
2601 while (*SourceString
!= CHAR_NULL
) {
2603 // if we find the FindTarget and either Skip == FALSE or Skip and we
2604 // dont have a carrot do a replace...
2606 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2607 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2609 SourceString
+= StrLen(FindTarget
);
2610 Size
= StrSize(NewString
);
2611 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2613 return (EFI_BUFFER_TOO_SMALL
);
2615 StrCat(NewString
, Replace
);
2617 Size
= StrSize(NewString
);
2618 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2620 return (EFI_BUFFER_TOO_SMALL
);
2622 StrnCat(NewString
, SourceString
, 1);
2627 return (EFI_SUCCESS
);
2631 Internal worker function to output a string.
2633 This function will output a string to the correct StdOut.
2635 @param[in] String The string to print out.
2637 @retval EFI_SUCCESS The operation was sucessful.
2638 @retval !EFI_SUCCESS The operation failed.
2643 IN CONST CHAR16
*String
2647 Size
= StrSize(String
) - sizeof(CHAR16
);
2649 return (EFI_SUCCESS
);
2651 if (gEfiShellParametersProtocol
!= NULL
) {
2652 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2654 if (mEfiShellInterface
!= NULL
) {
2655 if (mEfiShellInterface
->RedirArgc
== 0) {
2657 // Divide in half for old shell. Must be string length not size.
2659 Size
/=2; // Divide in half only when no redirection.
2661 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2664 return (EFI_UNSUPPORTED
);
2668 Print at a specific location on the screen.
2670 This function will move the cursor to a given screen location and print the specified string
2672 If -1 is specified for either the Row or Col the current screen location for BOTH
2675 if either Row or Col is out of range for the current console, then ASSERT
2676 if Format is NULL, then ASSERT
2678 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2679 the following additional flags:
2680 %N - Set output attribute to normal
2681 %H - Set output attribute to highlight
2682 %E - Set output attribute to error
2683 %B - Set output attribute to blue color
2684 %V - Set output attribute to green color
2686 Note: The background color is controlled by the shell command cls.
2688 @param[in] Col the column to print at
2689 @param[in] Row the row to print at
2690 @param[in] Format the format string
2691 @param[in] Marker the marker for the variable argument list
2693 @return EFI_SUCCESS The operation was successful.
2694 @return EFI_DEVICE_ERROR The console device reported an error.
2698 InternalShellPrintWorker(
2699 IN INT32 Col OPTIONAL
,
2700 IN INT32 Row OPTIONAL
,
2701 IN CONST CHAR16
*Format
,
2706 CHAR16
*ResumeLocation
;
2707 CHAR16
*FormatWalker
;
2708 UINTN OriginalAttribute
;
2709 CHAR16
*mPostReplaceFormat
;
2710 CHAR16
*mPostReplaceFormat2
;
2712 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2713 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2715 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2716 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2717 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2718 return (EFI_OUT_OF_RESOURCES
);
2721 Status
= EFI_SUCCESS
;
2722 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2725 // Back and forth each time fixing up 1 of our flags...
2727 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2728 ASSERT_EFI_ERROR(Status
);
2729 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2730 ASSERT_EFI_ERROR(Status
);
2731 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2732 ASSERT_EFI_ERROR(Status
);
2733 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2734 ASSERT_EFI_ERROR(Status
);
2735 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2736 ASSERT_EFI_ERROR(Status
);
2739 // Use the last buffer from replacing to print from...
2741 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2743 if (Col
!= -1 && Row
!= -1) {
2744 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2747 FormatWalker
= mPostReplaceFormat2
;
2748 while (*FormatWalker
!= CHAR_NULL
) {
2750 // Find the next attribute change request
2752 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2753 if (ResumeLocation
!= NULL
) {
2754 *ResumeLocation
= CHAR_NULL
;
2757 // print the current FormatWalker string
2759 if (StrLen(FormatWalker
)>0) {
2760 Status
= InternalPrintTo(FormatWalker
);
2761 if (EFI_ERROR(Status
)) {
2767 // update the attribute
2769 if (ResumeLocation
!= NULL
) {
2770 if (*(ResumeLocation
-1) == L
'^') {
2772 // Move cursor back 1 position to overwrite the ^
2774 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2777 // Print a simple '%' symbol
2779 Status
= InternalPrintTo(L
"%");
2780 ResumeLocation
= ResumeLocation
- 1;
2782 switch (*(ResumeLocation
+1)) {
2784 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2787 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2790 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2793 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2796 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2800 // Print a simple '%' symbol
2802 Status
= InternalPrintTo(L
"%");
2803 if (EFI_ERROR(Status
)) {
2806 ResumeLocation
= ResumeLocation
- 1;
2812 // reset to normal now...
2818 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2820 FormatWalker
= ResumeLocation
+ 2;
2823 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2825 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2826 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2831 Print at a specific location on the screen.
2833 This function will move the cursor to a given screen location and print the specified string.
2835 If -1 is specified for either the Row or Col the current screen location for BOTH
2838 If either Row or Col is out of range for the current console, then ASSERT.
2839 If Format is NULL, then ASSERT.
2841 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2842 the following additional flags:
2843 %N - Set output attribute to normal
2844 %H - Set output attribute to highlight
2845 %E - Set output attribute to error
2846 %B - Set output attribute to blue color
2847 %V - Set output attribute to green color
2849 Note: The background color is controlled by the shell command cls.
2851 @param[in] Col the column to print at
2852 @param[in] Row the row to print at
2853 @param[in] Format the format string
2854 @param[in] ... The variable argument list.
2856 @return EFI_SUCCESS The printing was successful.
2857 @return EFI_DEVICE_ERROR The console device reported an error.
2862 IN INT32 Col OPTIONAL
,
2863 IN INT32 Row OPTIONAL
,
2864 IN CONST CHAR16
*Format
,
2870 if (Format
== NULL
) {
2871 return (EFI_INVALID_PARAMETER
);
2873 VA_START (Marker
, Format
);
2874 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2880 Print at a specific location on the screen.
2882 This function will move the cursor to a given screen location and print the specified string.
2884 If -1 is specified for either the Row or Col the current screen location for BOTH
2887 If either Row or Col is out of range for the current console, then ASSERT.
2888 If Format is NULL, then ASSERT.
2890 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2891 the following additional flags:
2892 %N - Set output attribute to normal.
2893 %H - Set output attribute to highlight.
2894 %E - Set output attribute to error.
2895 %B - Set output attribute to blue color.
2896 %V - Set output attribute to green color.
2898 Note: The background color is controlled by the shell command cls.
2900 @param[in] Col The column to print at.
2901 @param[in] Row The row to print at.
2902 @param[in] Language The language of the string to retrieve. If this parameter
2903 is NULL, then the current platform language is used.
2904 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2905 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2906 @param[in] ... The variable argument list.
2908 @return EFI_SUCCESS The printing was successful.
2909 @return EFI_DEVICE_ERROR The console device reported an error.
2914 IN INT32 Col OPTIONAL
,
2915 IN INT32 Row OPTIONAL
,
2916 IN CONST CHAR8
*Language OPTIONAL
,
2917 IN CONST EFI_STRING_ID HiiFormatStringId
,
2918 IN CONST EFI_HANDLE HiiFormatHandle
,
2923 CHAR16
*HiiFormatString
;
2926 VA_START (Marker
, HiiFormatHandle
);
2927 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
2928 ASSERT(HiiFormatString
!= NULL
);
2930 RetVal
= InternalShellPrintWorker(Col
, Row
, HiiFormatString
, Marker
);
2932 SHELL_FREE_NON_NULL(HiiFormatString
);
2939 Function to determine if a given filename represents a file or a directory.
2941 @param[in] DirName Path to directory to test.
2943 @retval EFI_SUCCESS The Path represents a directory
2944 @retval EFI_NOT_FOUND The Path does not represent a directory
2945 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2946 @return The path failed to open
2951 IN CONST CHAR16
*DirName
2955 SHELL_FILE_HANDLE Handle
;
2956 CHAR16
*TempLocation
;
2957 CHAR16
*TempLocation2
;
2959 ASSERT(DirName
!= NULL
);
2962 TempLocation
= NULL
;
2964 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
2965 if (EFI_ERROR(Status
)) {
2967 // try good logic first.
2969 if (gEfiShellProtocol
!= NULL
) {
2970 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
2971 if (TempLocation
== NULL
) {
2972 ShellCloseFile(&Handle
);
2973 return (EFI_OUT_OF_RESOURCES
);
2975 TempLocation2
= StrStr(TempLocation
, L
":");
2976 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
2977 *(TempLocation2
+1) = CHAR_NULL
;
2979 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
2980 FreePool(TempLocation
);
2981 return (EFI_SUCCESS
);
2983 FreePool(TempLocation
);
2986 // probably a map name?!?!!?
2988 TempLocation
= StrStr(DirName
, L
"\\");
2989 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
2990 return (EFI_SUCCESS
);
2996 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
2997 ShellCloseFile(&Handle
);
2998 return (EFI_SUCCESS
);
3000 ShellCloseFile(&Handle
);
3001 return (EFI_NOT_FOUND
);
3005 Function to determine if a given filename represents a file.
3007 @param[in] Name Path to file to test.
3009 @retval EFI_SUCCESS The Path represents a file.
3010 @retval EFI_NOT_FOUND The Path does not represent a file.
3011 @retval other The path failed to open.
3016 IN CONST CHAR16
*Name
3020 SHELL_FILE_HANDLE Handle
;
3022 ASSERT(Name
!= NULL
);
3026 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3027 if (EFI_ERROR(Status
)) {
3031 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3032 ShellCloseFile(&Handle
);
3033 return (EFI_SUCCESS
);
3035 ShellCloseFile(&Handle
);
3036 return (EFI_NOT_FOUND
);
3040 Function to determine if a given filename represents a file.
3042 This will search the CWD and then the Path.
3044 If Name is NULL, then ASSERT.
3046 @param[in] Name Path to file to test.
3048 @retval EFI_SUCCESS The Path represents a file.
3049 @retval EFI_NOT_FOUND The Path does not represent a file.
3050 @retval other The path failed to open.
3055 IN CONST CHAR16
*Name
3061 if (!EFI_ERROR(ShellIsFile(Name
))) {
3062 return (EFI_SUCCESS
);
3065 NewName
= ShellFindFilePath(Name
);
3066 if (NewName
== NULL
) {
3067 return (EFI_NOT_FOUND
);
3069 Status
= ShellIsFile(NewName
);
3075 Function return the number converted from a hex representation of a number.
3077 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3078 result. Use ShellConvertStringToUint64 instead.
3080 @param[in] String String representation of a number.
3082 @return The unsigned integer result of the conversion.
3083 @retval (UINTN)(-1) An error occured.
3088 IN CONST CHAR16
*String
3093 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3094 return ((UINTN
)RetVal
);
3097 return ((UINTN
)(-1));
3101 Function to determine whether a string is decimal or hex representation of a number
3102 and return the number converted from the string. Spaces are always skipped.
3104 @param[in] String String representation of a number
3107 @retval (UINTN)(-1) An error ocurred.
3112 IN CONST CHAR16
*String
3120 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
)) {
3124 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3125 return ((UINTN
)RetVal
);
3127 return ((UINTN
)(-1));
3131 Safely append with automatic string resizing given length of Destination and
3132 desired length of copy from Source.
3134 append the first D characters of Source to the end of Destination, where D is
3135 the lesser of Count and the StrLen() of Source. If appending those D characters
3136 will fit within Destination (whose Size is given as CurrentSize) and
3137 still leave room for a NULL terminator, then those characters are appended,
3138 starting at the original terminating NULL of Destination, and a new terminating
3141 If appending D characters onto Destination will result in a overflow of the size
3142 given in CurrentSize the string will be grown such that the copy can be performed
3143 and CurrentSize will be updated to the new size.
3145 If Source is NULL, there is nothing to append, just return the current buffer in
3148 if Destination is NULL, then ASSERT()
3149 if Destination's current length (including NULL terminator) is already more then
3150 CurrentSize, then ASSERT()
3152 @param[in, out] Destination The String to append onto
3153 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3154 return possibly the new size (still in bytes). if NULL
3155 then allocate whatever is needed.
3156 @param[in] Source The String to append from
3157 @param[in] Count Maximum number of characters to append. if 0 then
3160 @return Destination return the resultant string.
3165 IN OUT CHAR16
**Destination
,
3166 IN OUT UINTN
*CurrentSize
,
3167 IN CONST CHAR16
*Source
,
3171 UINTN DestinationStartSize
;
3177 ASSERT(Destination
!= NULL
);
3180 // If there's nothing to do then just return Destination
3182 if (Source
== NULL
) {
3183 return (*Destination
);
3187 // allow for un-initialized pointers, based on size being 0
3189 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3190 *Destination
= NULL
;
3194 // allow for NULL pointers address as Destination
3196 if (*Destination
!= NULL
) {
3197 ASSERT(CurrentSize
!= 0);
3198 DestinationStartSize
= StrSize(*Destination
);
3199 ASSERT(DestinationStartSize
<= *CurrentSize
);
3201 DestinationStartSize
= 0;
3202 // ASSERT(*CurrentSize == 0);
3206 // Append all of Source?
3209 Count
= StrLen(Source
);
3213 // Test and grow if required
3215 if (CurrentSize
!= NULL
) {
3216 NewSize
= *CurrentSize
;
3217 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3218 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3219 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3221 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3222 *CurrentSize
= NewSize
;
3225 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
3229 // Now use standard StrnCat on a big enough buffer
3231 if (*Destination
== NULL
) {
3234 return StrnCat(*Destination
, Source
, Count
);
3238 Prompt the user and return the resultant answer to the requestor.
3240 This function will display the requested question on the shell prompt and then
3241 wait for an apropriate answer to be input from the console.
3243 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3244 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3246 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3249 In either case *Response must be callee freed if Response was not NULL;
3251 @param Type What type of question is asked. This is used to filter the input
3252 to prevent invalid answers to question.
3253 @param Prompt Pointer to string prompt to use to request input.
3254 @param Response Pointer to Response which will be populated upon return.
3256 @retval EFI_SUCCESS The operation was sucessful.
3257 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3258 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3259 @return other The operation failed.
3263 ShellPromptForResponse (
3264 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3265 IN CHAR16
*Prompt OPTIONAL
,
3266 IN OUT VOID
**Response OPTIONAL
3272 SHELL_PROMPT_RESPONSE
*Resp
;
3276 Status
= EFI_UNSUPPORTED
;
3280 if (Type
!= ShellPromptResponseTypeFreeform
) {
3281 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3283 return (EFI_OUT_OF_RESOURCES
);
3288 case ShellPromptResponseTypeQuitContinue
:
3289 if (Prompt
!= NULL
) {
3290 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3293 // wait for valid response
3295 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3296 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3297 if (EFI_ERROR(Status
)) {
3300 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3301 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3302 *Resp
= ShellPromptResponseQuit
;
3304 *Resp
= ShellPromptResponseContinue
;
3307 case ShellPromptResponseTypeYesNoCancel
:
3308 if (Prompt
!= NULL
) {
3309 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3312 // wait for valid response
3314 *Resp
= ShellPromptResponseMax
;
3315 while (*Resp
== ShellPromptResponseMax
) {
3316 if (ShellGetExecutionBreakFlag()) {
3317 Status
= EFI_ABORTED
;
3320 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3321 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3322 if (EFI_ERROR(Status
)) {
3325 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3326 switch (Key
.UnicodeChar
) {
3329 *Resp
= ShellPromptResponseYes
;
3333 *Resp
= ShellPromptResponseNo
;
3337 *Resp
= ShellPromptResponseCancel
;
3341 break; case ShellPromptResponseTypeYesNoAllCancel
:
3342 if (Prompt
!= NULL
) {
3343 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3346 // wait for valid response
3348 *Resp
= ShellPromptResponseMax
;
3349 while (*Resp
== ShellPromptResponseMax
) {
3350 if (ShellGetExecutionBreakFlag()) {
3351 Status
= EFI_ABORTED
;
3354 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3355 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3356 if (EFI_ERROR(Status
)) {
3359 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3360 switch (Key
.UnicodeChar
) {
3363 *Resp
= ShellPromptResponseYes
;
3367 *Resp
= ShellPromptResponseNo
;
3371 *Resp
= ShellPromptResponseAll
;
3375 *Resp
= ShellPromptResponseCancel
;
3380 case ShellPromptResponseTypeEnterContinue
:
3381 case ShellPromptResponseTypeAnyKeyContinue
:
3382 if (Prompt
!= NULL
) {
3383 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3386 // wait for valid response
3388 *Resp
= ShellPromptResponseMax
;
3389 while (*Resp
== ShellPromptResponseMax
) {
3390 if (ShellGetExecutionBreakFlag()) {
3391 Status
= EFI_ABORTED
;
3394 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3395 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3396 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3397 if (EFI_ERROR(Status
)) {
3400 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3401 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3402 *Resp
= ShellPromptResponseContinue
;
3406 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3407 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3408 ASSERT_EFI_ERROR(Status
);
3409 *Resp
= ShellPromptResponseContinue
;
3414 case ShellPromptResponseTypeYesNo
:
3415 if (Prompt
!= NULL
) {
3416 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3419 // wait for valid response
3421 *Resp
= ShellPromptResponseMax
;
3422 while (*Resp
== ShellPromptResponseMax
) {
3423 if (ShellGetExecutionBreakFlag()) {
3424 Status
= EFI_ABORTED
;
3427 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3428 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3429 if (EFI_ERROR(Status
)) {
3432 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3433 switch (Key
.UnicodeChar
) {
3436 *Resp
= ShellPromptResponseYes
;
3440 *Resp
= ShellPromptResponseNo
;
3445 case ShellPromptResponseTypeFreeform
:
3446 if (Prompt
!= NULL
) {
3447 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3450 if (ShellGetExecutionBreakFlag()) {
3451 Status
= EFI_ABORTED
;
3454 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3455 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3456 if (EFI_ERROR(Status
)) {
3459 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3460 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3463 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3464 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3468 // This is the location to add new prompt types.
3469 // If your new type loops remember to add ExecutionBreak support.
3475 if (Response
!= NULL
) {
3478 } else if (Buffer
!= NULL
) {
3485 if (Buffer
!= NULL
) {
3490 ShellPrintEx(-1, -1, L
"\r\n");
3495 Prompt the user and return the resultant answer to the requestor.
3497 This function is the same as ShellPromptForResponse, except that the prompt is
3498 automatically pulled from HII.
3500 @param Type What type of question is asked. This is used to filter the input
3501 to prevent invalid answers to question.
3502 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3503 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3504 @param Response Pointer to Response which will be populated upon return.
3506 @retval EFI_SUCCESS the operation was sucessful.
3507 @return other the operation failed.
3509 @sa ShellPromptForResponse
3513 ShellPromptForResponseHii (
3514 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3515 IN CONST EFI_STRING_ID HiiFormatStringId
,
3516 IN CONST EFI_HANDLE HiiFormatHandle
,
3517 IN OUT VOID
**Response
3523 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3524 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3530 Function to determin if an entire string is a valid number.
3532 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3534 @param[in] String The string to evaluate.
3535 @param[in] ForceHex TRUE - always assume hex.
3536 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3538 @retval TRUE It is all numeric (dec/hex) characters.
3539 @retval FALSE There is a non-numeric character.
3543 InternalShellIsHexOrDecimalNumber (
3544 IN CONST CHAR16
*String
,
3545 IN CONST BOOLEAN ForceHex
,
3546 IN CONST BOOLEAN StopAtSpace
3552 // chop off a single negative sign
3554 if (String
!= NULL
&& *String
== L
'-') {
3558 if (String
== NULL
) {
3563 // chop leading zeroes
3565 while(String
!= NULL
&& *String
== L
'0'){
3569 // allow '0x' or '0X', but not 'x' or 'X'
3571 if (String
!= NULL
&& (*String
== L
'x' || *String
== L
'X')) {
3572 if (*(String
-1) != L
'0') {
3574 // we got an x without a preceeding 0
3580 } else if (ForceHex
) {
3587 // loop through the remaining characters and use the lib function
3589 for ( ; String
!= NULL
&& *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3591 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3595 if (!ShellIsDecimalDigitCharacter(*String
)) {
3605 Function to determine if a given filename exists.
3607 @param[in] Name Path to test.
3609 @retval EFI_SUCCESS The Path represents a file.
3610 @retval EFI_NOT_FOUND The Path does not represent a file.
3611 @retval other The path failed to open.
3616 IN CONST CHAR16
*Name
3620 EFI_SHELL_FILE_INFO
*List
;
3622 ASSERT(Name
!= NULL
);
3625 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3626 if (EFI_ERROR(Status
)) {
3630 ShellCloseFileMetaArg(&List
);
3632 return (EFI_SUCCESS
);
3636 Convert a Unicode character to upper case only if
3637 it maps to a valid small-case ASCII character.
3639 This internal function only deal with Unicode character
3640 which maps to a valid small-case ASCII character, i.e.
3641 L'a' to L'z'. For other Unicode character, the input character
3642 is returned directly.
3644 @param Char The character to convert.
3646 @retval LowerCharacter If the Char is with range L'a' to L'z'.
3647 @retval Unchanged Otherwise.
3652 InternalShellCharToUpper (
3656 if (Char
>= L
'a' && Char
<= L
'z') {
3657 return (CHAR16
) (Char
- (L
'a' - L
'A'));
3664 Convert a Unicode character to numerical value.
3666 This internal function only deal with Unicode character
3667 which maps to a valid hexadecimal ASII character, i.e.
3668 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3669 Unicode character, the value returned does not make sense.
3671 @param Char The character to convert.
3673 @return The numerical value converted.
3678 InternalShellHexCharToUintn (
3682 if (ShellIsDecimalDigitCharacter (Char
)) {
3686 return (UINTN
) (10 + InternalShellCharToUpper (Char
) - L
'A');
3690 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3692 This function returns a value of type UINTN by interpreting the contents
3693 of the Unicode string specified by String as a hexadecimal number.
3694 The format of the input Unicode string String is:
3696 [spaces][zeros][x][hexadecimal digits].
3698 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3699 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3700 If "x" appears in the input string, it must be prefixed with at least one 0.
3701 The function will ignore the pad space, which includes spaces or tab characters,
3702 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3703 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3704 first valid hexadecimal digit. Then, the function stops at the first character that is
3705 a not a valid hexadecimal character or NULL, whichever one comes first.
3707 If String has only pad spaces, then zero is returned.
3708 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3709 then zero is returned.
3711 @param[in] String A pointer to a Null-terminated Unicode string.
3712 @param[out] Value Upon a successful return the value of the conversion.
3713 @param[in] StopAtSpace FALSE to skip spaces.
3715 @retval EFI_SUCCESS The conversion was successful.
3716 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3717 @retval EFI_DEVICE_ERROR An overflow occured.
3721 InternalShellStrHexToUint64 (
3722 IN CONST CHAR16
*String
,
3724 IN CONST BOOLEAN StopAtSpace
3729 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3730 return (EFI_INVALID_PARAMETER
);
3734 // Ignore the pad spaces (space or tab)
3736 while ((*String
== L
' ') || (*String
== L
'\t')) {
3741 // Ignore leading Zeros after the spaces
3743 while (*String
== L
'0') {
3747 if (InternalShellCharToUpper (*String
) == L
'X') {
3748 if (*(String
- 1) != L
'0') {
3760 // Skip spaces if requested
3762 while (StopAtSpace
&& *String
== L
' ') {
3766 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3768 // If the Hex Number represented by String overflows according
3769 // to the range defined by UINTN, then ASSERT().
3771 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3772 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3773 return (EFI_DEVICE_ERROR
);
3776 Result
= (LShiftU64(Result
, 4));
3777 Result
+= InternalShellHexCharToUintn (*String
);
3781 // stop at spaces if requested
3783 if (StopAtSpace
&& *String
== L
' ') {
3789 return (EFI_SUCCESS
);
3793 Convert a Null-terminated Unicode decimal string to a value of
3796 This function returns a value of type UINT64 by interpreting the contents
3797 of the Unicode string specified by String as a decimal number. The format
3798 of the input Unicode string String is:
3800 [spaces] [decimal digits].
3802 The valid decimal digit character is in the range [0-9]. The
3803 function will ignore the pad space, which includes spaces or
3804 tab characters, before [decimal digits]. The running zero in the
3805 beginning of [decimal digits] will be ignored. Then, the function
3806 stops at the first character that is a not a valid decimal character
3807 or a Null-terminator, whichever one comes first.
3809 If String has only pad spaces, then 0 is returned.
3810 If String has no pad spaces or valid decimal digits,
3813 @param[in] String A pointer to a Null-terminated Unicode string.
3814 @param[out] Value Upon a successful return the value of the conversion.
3815 @param[in] StopAtSpace FALSE to skip spaces.
3817 @retval EFI_SUCCESS The conversion was successful.
3818 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3819 @retval EFI_DEVICE_ERROR An overflow occured.
3823 InternalShellStrDecimalToUint64 (
3824 IN CONST CHAR16
*String
,
3826 IN CONST BOOLEAN StopAtSpace
3831 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3832 return (EFI_INVALID_PARAMETER
);
3836 // Ignore the pad spaces (space or tab)
3838 while ((*String
== L
' ') || (*String
== L
'\t')) {
3843 // Ignore leading Zeros after the spaces
3845 while (*String
== L
'0') {
3852 // Skip spaces if requested
3854 while (StopAtSpace
&& *String
== L
' ') {
3857 while (ShellIsDecimalDigitCharacter (*String
)) {
3859 // If the number represented by String overflows according
3860 // to the range defined by UINT64, then ASSERT().
3863 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3864 return (EFI_DEVICE_ERROR
);
3867 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3871 // Stop at spaces if requested
3873 if (StopAtSpace
&& *String
== L
' ') {
3880 return (EFI_SUCCESS
);
3884 Function to verify and convert a string to its numerical value.
3886 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3888 @param[in] String The string to evaluate.
3889 @param[out] Value Upon a successful return the value of the conversion.
3890 @param[in] ForceHex TRUE - always assume hex.
3891 @param[in] StopAtSpace FALSE to skip spaces.
3893 @retval EFI_SUCCESS The conversion was successful.
3894 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3895 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3899 ShellConvertStringToUint64(
3900 IN CONST CHAR16
*String
,
3902 IN CONST BOOLEAN ForceHex
,
3903 IN CONST BOOLEAN StopAtSpace
3907 CONST CHAR16
*Walker
;
3913 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
)) {
3916 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
)) {
3917 return (EFI_INVALID_PARAMETER
);
3920 return (EFI_INVALID_PARAMETER
);
3925 // Chop off leading spaces
3927 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
3930 // make sure we have something left that is numeric.
3932 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
)) {
3933 return (EFI_INVALID_PARAMETER
);
3937 // do the conversion.
3939 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
3940 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
3942 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
3945 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
3946 return (EFI_NOT_FOUND
);
3949 if (Value
!= NULL
) {
3957 Function to determin if an entire string is a valid number.
3959 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3961 @param[in] String The string to evaluate.
3962 @param[in] ForceHex TRUE - always assume hex.
3963 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3965 @retval TRUE It is all numeric (dec/hex) characters.
3966 @retval FALSE There is a non-numeric character.
3970 ShellIsHexOrDecimalNumber (
3971 IN CONST CHAR16
*String
,
3972 IN CONST BOOLEAN ForceHex
,
3973 IN CONST BOOLEAN StopAtSpace
3976 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
3983 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
3984 buffer. The returned buffer must be callee freed.
3986 If the position upon start is 0, then the Ascii Boolean will be set. This should be
3987 maintained and not changed for all operations with the same file.
3989 @param[in] Handle SHELL_FILE_HANDLE to read from.
3990 @param[in, out] Ascii Boolean value for indicating whether the file is
3991 Ascii (TRUE) or UCS2 (FALSE).
3993 @return The line of text from the file.
3994 @retval NULL There was not enough memory available.
3996 @sa ShellFileHandleReadLine
4000 ShellFileHandleReturnLine(
4001 IN SHELL_FILE_HANDLE Handle
,
4002 IN OUT BOOLEAN
*Ascii
4012 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4013 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4014 RetVal
= AllocateZeroPool(Size
);
4015 if (RetVal
== NULL
) {
4018 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4021 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4029 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4031 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4032 maintained and not changed for all operations with the same file.
4034 @param[in] Handle SHELL_FILE_HANDLE to read from.
4035 @param[in, out] Buffer The pointer to buffer to read into.
4036 @param[in, out] Size The pointer to number of bytes in Buffer.
4037 @param[in] Truncate If the buffer is large enough, this has no effect.
4038 If the buffer is is too small and Truncate is TRUE,
4039 the line will be truncated.
4040 If the buffer is is too small and Truncate is FALSE,
4041 then no read will occur.
4043 @param[in, out] Ascii Boolean value for indicating whether the file is
4044 Ascii (TRUE) or UCS2 (FALSE).
4046 @retval EFI_SUCCESS The operation was successful. The line is stored in
4048 @retval EFI_INVALID_PARAMETER Handle was NULL.
4049 @retval EFI_INVALID_PARAMETER Size was NULL.
4050 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4051 Size was updated to the minimum space required.
4055 ShellFileHandleReadLine(
4056 IN SHELL_FILE_HANDLE Handle
,
4057 IN OUT CHAR16
*Buffer
,
4059 IN BOOLEAN Truncate
,
4060 IN OUT BOOLEAN
*Ascii
4067 UINT64 OriginalFilePosition
;
4073 return (EFI_INVALID_PARAMETER
);
4075 if (Buffer
== NULL
) {
4078 *Buffer
= CHAR_NULL
;
4080 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4081 if (OriginalFilePosition
== 0) {
4082 CharSize
= sizeof(CHAR16
);
4083 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4084 ASSERT_EFI_ERROR(Status
);
4085 if (CharBuffer
== gUnicodeFileTag
) {
4089 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4093 for (CountSoFar
= 0;;CountSoFar
++){
4096 CharSize
= sizeof(CHAR8
);
4098 CharSize
= sizeof(CHAR16
);
4100 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4101 if ( EFI_ERROR(Status
)
4103 || (CharBuffer
== L
'\n' && !(*Ascii
))
4104 || (CharBuffer
== '\n' && *Ascii
)
4109 // if we have space save it...
4111 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4112 ASSERT(Buffer
!= NULL
);
4113 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4114 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4119 // if we ran out of space tell when...
4121 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4122 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4124 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4126 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4128 return (EFI_BUFFER_TOO_SMALL
);
4130 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4131 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4138 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4140 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4141 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4142 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4143 the help content only.
4144 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4145 @retval EFI_NOT_FOUND The help data could not be found.
4146 @retval EFI_SUCCESS The operation was successful.
4151 IN CONST CHAR16
*CommandToGetHelpOn
,
4152 IN CONST CHAR16
*SectionToGetHelpOn
,
4153 IN BOOLEAN PrintCommandText
4162 // Get the string to print based
4164 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4167 // make sure we got a valid string
4169 if (EFI_ERROR(Status
)){
4172 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4173 return EFI_NOT_FOUND
;
4177 // Chop off trailing stuff we dont need
4179 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4180 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4184 // Print this out to the console
4186 if (PrintCommandText
) {
4187 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4189 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4192 SHELL_FREE_NON_NULL(OutText
);
4198 Function to delete a file by name
4200 @param[in] FileName Pointer to file name to delete.
4202 @retval EFI_SUCCESS the file was deleted sucessfully
4203 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4205 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4206 @retval EFI_NOT_FOUND The specified file could not be found on the
4207 device or the file system could not be found
4209 @retval EFI_NO_MEDIA The device has no medium.
4210 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4211 medium is no longer supported.
4212 @retval EFI_DEVICE_ERROR The device reported an error.
4213 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4214 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4215 @retval EFI_ACCESS_DENIED The file was opened read only.
4216 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4218 @retval other The file failed to open
4222 ShellDeleteFileByName(
4223 IN CONST CHAR16
*FileName
4227 SHELL_FILE_HANDLE FileHandle
;
4229 Status
= ShellFileExists(FileName
);
4231 if (Status
== EFI_SUCCESS
){
4232 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4233 if (Status
== EFI_SUCCESS
){
4234 Status
= ShellDeleteFile(&FileHandle
);