2 Provides interface to shell functionality for shell commands and applications.
4 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
5 Copyright 2016-2018 Dell Technologies.<BR>
6 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "UefiShellLib.h"
12 #include <Library/SortLib.h>
13 #include <Library/BaseLib.h>
18 SHELL_PARAM_ITEM EmptyParamList
[] = {
21 SHELL_PARAM_ITEM SfoParamList
[] = {
25 EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
26 EFI_SHELL_INTERFACE
*mEfiShellInterface
;
27 EFI_SHELL_PROTOCOL
*gEfiShellProtocol
;
28 EFI_SHELL_PARAMETERS_PROTOCOL
*gEfiShellParametersProtocol
;
29 EFI_HANDLE mEfiShellEnvironment2Handle
;
30 FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
31 EFI_UNICODE_COLLATION_PROTOCOL
*mUnicodeCollationProtocol
;
34 Return a clean, fully-qualified version of an input path. If the return value
35 is non-NULL the caller must free the memory when it is no longer needed.
37 If asserts are disabled, and if the input parameter is NULL, NULL is returned.
39 If there is not enough memory available to create the fully-qualified path or
40 a copy of the input path, NULL is returned.
42 If there is no working directory, a clean copy of Path is returned.
44 Otherwise, the current file system or working directory (as appropriate) is
45 prepended to Path and the resulting path is cleaned and returned.
47 NOTE: If the input path is an empty string, then the current working directory
48 (if it exists) is returned. In other words, an empty input path is treated
49 exactly the same as ".".
51 @param[in] Path A pointer to some file or directory path.
53 @retval NULL The input path is NULL or out of memory.
55 @retval non-NULL A pointer to a clean, fully-qualified version of Path.
56 If there is no working directory, then a pointer to a
57 clean, but not necessarily fully-qualified version of
58 Path. The caller must free this memory when it is no
67 CONST CHAR16
*WorkingPath
;
68 CONST CHAR16
*InputPath
;
70 CHAR16
*InputFileSystem
;
71 UINTN FileSystemCharCount
;
72 CHAR16
*FullyQualifiedPath
;
75 FullyQualifiedPath
= NULL
;
79 // Handle erroneous input when asserts are disabled.
85 // In paths that contain ":", like fs0:dir/file.ext and fs2:\fqpath\file.ext,
86 // we have to consider the file system part separately from the "path" part.
87 // If there is a file system in the path, we have to get the current working
88 // directory for that file system. Then we need to use the part of the path
89 // following the ":". If a path does not contain ":", we use it as given.
91 InputPath
= StrStr(Path
, L
":");
92 if (InputPath
!= NULL
) {
94 FileSystemCharCount
= ((UINTN
)InputPath
- (UINTN
)Path
+ sizeof(CHAR16
)) / sizeof(CHAR16
);
95 InputFileSystem
= AllocateCopyPool(FileSystemCharCount
* sizeof(CHAR16
), Path
);
96 if (InputFileSystem
!= NULL
) {
97 InputFileSystem
[FileSystemCharCount
- 1] = CHAR_NULL
;
99 WorkingPath
= ShellGetCurrentDir(InputFileSystem
);
100 SHELL_FREE_NON_NULL(InputFileSystem
);
103 WorkingPath
= ShellGetEnvironmentVariable(L
"cwd");
106 if (WorkingPath
== NULL
) {
108 // With no working directory, all we can do is copy and clean the input path.
110 FullyQualifiedPath
= AllocateCopyPool(StrSize(Path
), Path
);
113 // Allocate space for both strings plus one more character.
115 Size
= StrSize(WorkingPath
) + StrSize(InputPath
);
116 FullyQualifiedPath
= AllocateZeroPool(Size
);
117 if (FullyQualifiedPath
== NULL
) {
119 // Try to copy and clean just the input. No harm if not enough memory.
121 FullyQualifiedPath
= AllocateCopyPool(StrSize(Path
), Path
);
123 if (*InputPath
== L
'\\' || *InputPath
== L
'/') {
125 // Absolute path: start with the current working directory, then
126 // truncate the new path after the file system part.
128 StrCpyS(FullyQualifiedPath
, Size
/sizeof(CHAR16
), WorkingPath
);
129 CharPtr
= StrStr(FullyQualifiedPath
, L
":");
130 if (CharPtr
!= NULL
) {
131 *(CharPtr
+ 1) = CHAR_NULL
;
135 // Relative path: start with the working directory and append "\".
137 StrCpyS(FullyQualifiedPath
, Size
/sizeof(CHAR16
), WorkingPath
);
138 StrCatS(FullyQualifiedPath
, Size
/sizeof(CHAR16
), L
"\\");
141 // Now append the absolute or relative path.
143 StrCatS(FullyQualifiedPath
, Size
/sizeof(CHAR16
), InputPath
);
147 PathCleanUpDirectories(FullyQualifiedPath
);
149 return FullyQualifiedPath
;
153 Check if a Unicode character is a hexadecimal character.
155 This internal function checks if a Unicode character is a
156 numeric character. The valid hexadecimal characters are
157 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
159 @param Char The character to check against.
161 @retval TRUE If the Char is a hexadecmial character.
162 @retval FALSE If the Char is not a hexadecmial character.
167 ShellIsHexaDecimalDigitCharacter (
171 return (BOOLEAN
) ((Char
>= L
'0' && Char
<= L
'9') || (Char
>= L
'A' && Char
<= L
'F') || (Char
>= L
'a' && Char
<= L
'f'));
175 Check if a Unicode character is a decimal character.
177 This internal function checks if a Unicode character is a
178 decimal character. The valid characters are
182 @param Char The character to check against.
184 @retval TRUE If the Char is a hexadecmial character.
185 @retval FALSE If the Char is not a hexadecmial character.
190 ShellIsDecimalDigitCharacter (
194 return (BOOLEAN
) (Char
>= L
'0' && Char
<= L
'9');
198 Helper function to find ShellEnvironment2 for constructor.
200 @param[in] ImageHandle A copy of the calling image's handle.
202 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
206 IN EFI_HANDLE ImageHandle
216 Status
= gBS
->OpenProtocol(ImageHandle
,
217 &gEfiShellEnvironment2Guid
,
218 (VOID
**)&mEfiShellEnvironment2
,
221 EFI_OPEN_PROTOCOL_GET_PROTOCOL
224 // look for the mEfiShellEnvironment2 protocol at a higher level
226 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
))){
228 // figure out how big of a buffer we need.
230 Status
= gBS
->LocateHandle (ByProtocol
,
231 &gEfiShellEnvironment2Guid
,
232 NULL
, // ignored for ByProtocol
237 // maybe it's not there???
239 if (Status
== EFI_BUFFER_TOO_SMALL
) {
240 Buffer
= (EFI_HANDLE
*)AllocateZeroPool(BufferSize
);
241 if (Buffer
== NULL
) {
242 return (EFI_OUT_OF_RESOURCES
);
244 Status
= gBS
->LocateHandle (ByProtocol
,
245 &gEfiShellEnvironment2Guid
,
246 NULL
, // ignored for ByProtocol
251 if (!EFI_ERROR (Status
) && Buffer
!= NULL
) {
253 // now parse the list of returned handles
255 Status
= EFI_NOT_FOUND
;
256 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
257 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
258 &gEfiShellEnvironment2Guid
,
259 (VOID
**)&mEfiShellEnvironment2
,
262 EFI_OPEN_PROTOCOL_GET_PROTOCOL
264 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
)) {
265 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
266 Status
= EFI_SUCCESS
;
272 if (Buffer
!= NULL
) {
279 Function to do most of the work of the constructor. Allows for calling
280 multiple times without complete re-initialization.
282 @param[in] ImageHandle A copy of the ImageHandle.
283 @param[in] SystemTable A pointer to the SystemTable for the application.
285 @retval EFI_SUCCESS The operationw as successful.
288 ShellLibConstructorWorker (
289 IN EFI_HANDLE ImageHandle
,
290 IN EFI_SYSTEM_TABLE
*SystemTable
295 if (gEfiShellProtocol
== NULL
) {
297 // UEFI 2.0 shell interfaces (used preferentially)
299 Status
= gBS
->OpenProtocol (
301 &gEfiShellProtocolGuid
,
302 (VOID
**)&gEfiShellProtocol
,
305 EFI_OPEN_PROTOCOL_GET_PROTOCOL
307 if (EFI_ERROR (Status
)) {
309 // Search for the shell protocol
311 Status
= gBS
->LocateProtocol (
312 &gEfiShellProtocolGuid
,
314 (VOID
**)&gEfiShellProtocol
316 if (EFI_ERROR (Status
)) {
317 gEfiShellProtocol
= NULL
;
322 if (gEfiShellParametersProtocol
== NULL
) {
323 Status
= gBS
->OpenProtocol (
325 &gEfiShellParametersProtocolGuid
,
326 (VOID
**)&gEfiShellParametersProtocol
,
329 EFI_OPEN_PROTOCOL_GET_PROTOCOL
331 if (EFI_ERROR (Status
)) {
332 gEfiShellParametersProtocol
= NULL
;
336 if (gEfiShellProtocol
== NULL
) {
338 // Moved to seperate function due to complexity
340 Status
= ShellFindSE2(ImageHandle
);
342 if (EFI_ERROR(Status
)) {
343 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
344 mEfiShellEnvironment2
= NULL
;
346 Status
= gBS
->OpenProtocol(ImageHandle
,
347 &gEfiShellInterfaceGuid
,
348 (VOID
**)&mEfiShellInterface
,
351 EFI_OPEN_PROTOCOL_GET_PROTOCOL
353 if (EFI_ERROR(Status
)) {
354 mEfiShellInterface
= NULL
;
359 // Getting either EDK Shell's ShellEnvironment2 and ShellInterface protocol
360 // or UEFI Shell's Shell protocol.
361 // When ShellLib is linked to a driver producing DynamicCommand protocol,
362 // ShellParameters protocol is set by DynamicCommand.Handler().
364 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
365 (gEfiShellProtocol
!= NULL
)
367 if (gEfiShellProtocol
!= NULL
) {
368 FileFunctionMap
.GetFileInfo
= gEfiShellProtocol
->GetFileInfo
;
369 FileFunctionMap
.SetFileInfo
= gEfiShellProtocol
->SetFileInfo
;
370 FileFunctionMap
.ReadFile
= gEfiShellProtocol
->ReadFile
;
371 FileFunctionMap
.WriteFile
= gEfiShellProtocol
->WriteFile
;
372 FileFunctionMap
.CloseFile
= gEfiShellProtocol
->CloseFile
;
373 FileFunctionMap
.DeleteFile
= gEfiShellProtocol
->DeleteFile
;
374 FileFunctionMap
.GetFilePosition
= gEfiShellProtocol
->GetFilePosition
;
375 FileFunctionMap
.SetFilePosition
= gEfiShellProtocol
->SetFilePosition
;
376 FileFunctionMap
.FlushFile
= gEfiShellProtocol
->FlushFile
;
377 FileFunctionMap
.GetFileSize
= gEfiShellProtocol
->GetFileSize
;
379 FileFunctionMap
.GetFileInfo
= (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
;
380 FileFunctionMap
.SetFileInfo
= (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
;
381 FileFunctionMap
.ReadFile
= (EFI_SHELL_READ_FILE
)FileHandleRead
;
382 FileFunctionMap
.WriteFile
= (EFI_SHELL_WRITE_FILE
)FileHandleWrite
;
383 FileFunctionMap
.CloseFile
= (EFI_SHELL_CLOSE_FILE
)FileHandleClose
;
384 FileFunctionMap
.DeleteFile
= (EFI_SHELL_DELETE_FILE
)FileHandleDelete
;
385 FileFunctionMap
.GetFilePosition
= (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
;
386 FileFunctionMap
.SetFilePosition
= (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
;
387 FileFunctionMap
.FlushFile
= (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
;
388 FileFunctionMap
.GetFileSize
= (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
;
390 return (EFI_SUCCESS
);
392 return (EFI_NOT_FOUND
);
395 Constructor for the Shell library.
397 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
399 @param ImageHandle the image handle of the process
400 @param SystemTable the EFI System Table pointer
402 @retval EFI_SUCCESS the initialization was complete sucessfully
403 @return others an error ocurred during initialization
407 ShellLibConstructor (
408 IN EFI_HANDLE ImageHandle
,
409 IN EFI_SYSTEM_TABLE
*SystemTable
412 mEfiShellEnvironment2
= NULL
;
413 gEfiShellProtocol
= NULL
;
414 gEfiShellParametersProtocol
= NULL
;
415 mEfiShellInterface
= NULL
;
416 mEfiShellEnvironment2Handle
= NULL
;
417 mUnicodeCollationProtocol
= NULL
;
420 // verify that auto initialize is not set false
422 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
423 return (EFI_SUCCESS
);
426 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
430 Destructor for the library. free any resources.
432 @param[in] ImageHandle A copy of the ImageHandle.
433 @param[in] SystemTable A pointer to the SystemTable for the application.
435 @retval EFI_SUCCESS The operation was successful.
436 @return An error from the CloseProtocol function.
441 IN EFI_HANDLE ImageHandle
,
442 IN EFI_SYSTEM_TABLE
*SystemTable
447 if (mEfiShellEnvironment2
!= NULL
) {
448 Status
= gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
449 &gEfiShellEnvironment2Guid
,
452 if (!EFI_ERROR (Status
)) {
453 mEfiShellEnvironment2
= NULL
;
454 mEfiShellEnvironment2Handle
= NULL
;
457 if (mEfiShellInterface
!= NULL
) {
458 Status
= gBS
->CloseProtocol(ImageHandle
,
459 &gEfiShellInterfaceGuid
,
462 if (!EFI_ERROR (Status
)) {
463 mEfiShellInterface
= NULL
;
466 if (gEfiShellProtocol
!= NULL
) {
467 Status
= gBS
->CloseProtocol(ImageHandle
,
468 &gEfiShellProtocolGuid
,
471 if (!EFI_ERROR (Status
)) {
472 gEfiShellProtocol
= NULL
;
475 if (gEfiShellParametersProtocol
!= NULL
) {
476 Status
= gBS
->CloseProtocol(ImageHandle
,
477 &gEfiShellParametersProtocolGuid
,
480 if (!EFI_ERROR (Status
)) {
481 gEfiShellParametersProtocol
= NULL
;
485 return (EFI_SUCCESS
);
489 This function causes the shell library to initialize itself. If the shell library
490 is already initialized it will de-initialize all the current protocol poitners and
491 re-populate them again.
493 When the library is used with PcdShellLibAutoInitialize set to true this function
494 will return EFI_SUCCESS and perform no actions.
496 This function is intended for internal access for shell commands only.
498 @retval EFI_SUCCESS the initialization was complete sucessfully
510 // if auto initialize is not false then skip
512 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
513 return (EFI_SUCCESS
);
517 // deinit the current stuff
519 Status
= ShellLibDestructor (gImageHandle
, gST
);
520 ASSERT_EFI_ERROR (Status
);
523 // init the new stuff
525 return (ShellLibConstructorWorker(gImageHandle
, gST
));
529 This function will retrieve the information about the file for the handle
530 specified and store it in allocated pool memory.
532 This function allocates a buffer to store the file's information. It is the
533 caller's responsibility to free the buffer
535 @param FileHandle The file handle of the file for which information is
538 @retval NULL information could not be retrieved.
540 @return the information about the file
545 IN SHELL_FILE_HANDLE FileHandle
548 return (FileFunctionMap
.GetFileInfo(FileHandle
));
552 This function sets the information about the file for the opened handle
555 @param[in] FileHandle The file handle of the file for which information
558 @param[in] FileInfo The information to set.
560 @retval EFI_SUCCESS The information was set.
561 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
562 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
563 @retval EFI_NO_MEDIA The device has no medium.
564 @retval EFI_DEVICE_ERROR The device reported an error.
565 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
566 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
567 @retval EFI_ACCESS_DENIED The file was opened read only.
568 @retval EFI_VOLUME_FULL The volume is full.
573 IN SHELL_FILE_HANDLE FileHandle
,
574 IN EFI_FILE_INFO
*FileInfo
577 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
581 This function will open a file or directory referenced by DevicePath.
583 This function opens a file with the open mode according to the file path. The
584 Attributes is valid only for EFI_FILE_MODE_CREATE.
586 @param FilePath on input the device path to the file. On output
587 the remaining device path.
588 @param FileHandle pointer to the file handle.
589 @param OpenMode the mode to open the file with.
590 @param Attributes the file's file attributes.
592 @retval EFI_SUCCESS The information was set.
593 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
594 @retval EFI_UNSUPPORTED Could not open the file path.
595 @retval EFI_NOT_FOUND The specified file could not be found on the
596 device or the file system could not be found on
598 @retval EFI_NO_MEDIA The device has no medium.
599 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
600 medium is no longer supported.
601 @retval EFI_DEVICE_ERROR The device reported an error.
602 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
603 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
604 @retval EFI_ACCESS_DENIED The file was opened read only.
605 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
607 @retval EFI_VOLUME_FULL The volume is full.
611 ShellOpenFileByDevicePath(
612 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
613 OUT SHELL_FILE_HANDLE
*FileHandle
,
620 EFI_FILE_PROTOCOL
*File
;
622 if (FilePath
== NULL
|| FileHandle
== NULL
) {
623 return (EFI_INVALID_PARAMETER
);
627 // which shell interface should we use
629 if (gEfiShellProtocol
!= NULL
) {
631 // use UEFI Shell 2.0 method.
633 FileName
= gEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
634 if (FileName
== NULL
) {
635 return (EFI_INVALID_PARAMETER
);
637 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
644 // use old shell method.
646 Status
= EfiOpenFileByDevicePath (FilePath
, &File
, OpenMode
, Attributes
);
647 if (EFI_ERROR (Status
)) {
652 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
654 *FileHandle
= (VOID
*)File
;
655 return (EFI_SUCCESS
);
659 This function will open a file or directory referenced by filename.
661 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
662 otherwise, the Filehandle is NULL. The Attributes is valid only for
663 EFI_FILE_MODE_CREATE.
665 if FileName is NULL then ASSERT()
667 @param FileName pointer to file name
668 @param FileHandle pointer to the file handle.
669 @param OpenMode the mode to open the file with.
670 @param Attributes the file's file attributes.
672 @retval EFI_SUCCESS The information was set.
673 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
674 @retval EFI_UNSUPPORTED Could not open the file path.
675 @retval EFI_NOT_FOUND The specified file could not be found on the
676 device or the file system could not be found
678 @retval EFI_NO_MEDIA The device has no medium.
679 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
680 medium is no longer supported.
681 @retval EFI_DEVICE_ERROR The device reported an error.
682 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
683 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
684 @retval EFI_ACCESS_DENIED The file was opened read only.
685 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
687 @retval EFI_VOLUME_FULL The volume is full.
692 IN CONST CHAR16
*FileName
,
693 OUT SHELL_FILE_HANDLE
*FileHandle
,
698 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
700 EFI_FILE_INFO
*FileInfo
;
701 CHAR16
*FileNameCopy
;
705 // ASSERT if FileName is NULL
707 ASSERT(FileName
!= NULL
);
709 if (FileName
== NULL
) {
710 return (EFI_INVALID_PARAMETER
);
713 if (gEfiShellProtocol
!= NULL
) {
714 if ((OpenMode
& EFI_FILE_MODE_CREATE
) == EFI_FILE_MODE_CREATE
) {
717 // Create only a directory
719 if ((Attributes
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
720 return ShellCreateDirectory(FileName
, FileHandle
);
724 // Create the directory to create the file in
726 FileNameCopy
= AllocateCopyPool (StrSize (FileName
), FileName
);
727 if (FileNameCopy
== NULL
) {
728 return (EFI_OUT_OF_RESOURCES
);
730 PathCleanUpDirectories (FileNameCopy
);
731 if (PathRemoveLastItem (FileNameCopy
)) {
732 if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy
, FileHandle
))) {
733 ShellCloseFile (FileHandle
);
736 SHELL_FREE_NON_NULL (FileNameCopy
);
740 // Use UEFI Shell 2.0 method to create the file
742 Status
= gEfiShellProtocol
->OpenFileByName(FileName
,
745 if (EFI_ERROR(Status
)) {
749 if (mUnicodeCollationProtocol
== NULL
) {
750 Status
= gBS
->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&mUnicodeCollationProtocol
);
751 if (EFI_ERROR (Status
)) {
752 gEfiShellProtocol
->CloseFile (*FileHandle
);
757 if ((mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NUL") != 0) &&
758 (mUnicodeCollationProtocol
->StriColl (mUnicodeCollationProtocol
, (CHAR16
*)FileName
, L
"NULL") != 0) &&
759 !EFI_ERROR(Status
) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)){
760 FileInfo
= FileFunctionMap
.GetFileInfo(*FileHandle
);
761 ASSERT(FileInfo
!= NULL
);
762 FileInfo
->Attribute
= Attributes
;
763 Status2
= FileFunctionMap
.SetFileInfo(*FileHandle
, FileInfo
);
765 if (EFI_ERROR (Status2
)) {
766 gEfiShellProtocol
->CloseFile(*FileHandle
);
773 // Using EFI Shell version
774 // this means convert name to path and call that function
775 // since this will use EFI method again that will open it.
777 ASSERT(mEfiShellEnvironment2
!= NULL
);
778 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
779 if (FilePath
!= NULL
) {
780 return (ShellOpenFileByDevicePath(&FilePath
,
785 return (EFI_DEVICE_ERROR
);
788 This function create a directory
790 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
791 otherwise, the Filehandle is NULL. If the directory already existed, this
792 function opens the existing directory.
794 @param DirectoryName pointer to directory name
795 @param FileHandle pointer to the file handle.
797 @retval EFI_SUCCESS The information was set.
798 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
799 @retval EFI_UNSUPPORTED Could not open the file path.
800 @retval EFI_NOT_FOUND The specified file could not be found on the
801 device or the file system could not be found
803 @retval EFI_NO_MEDIA The device has no medium.
804 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
805 medium is no longer supported.
806 @retval EFI_DEVICE_ERROR The device reported an error.
807 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
808 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
809 @retval EFI_ACCESS_DENIED The file was opened read only.
810 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
812 @retval EFI_VOLUME_FULL The volume is full.
813 @sa ShellOpenFileByName
817 ShellCreateDirectory(
818 IN CONST CHAR16
*DirectoryName
,
819 OUT SHELL_FILE_HANDLE
*FileHandle
822 if (gEfiShellProtocol
!= NULL
) {
824 // Use UEFI Shell 2.0 method
826 return (gEfiShellProtocol
->CreateFile(DirectoryName
,
831 return (ShellOpenFileByName(DirectoryName
,
833 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
840 This function reads information from an opened file.
842 If FileHandle is not a directory, the function reads the requested number of
843 bytes from the file at the file's current position and returns them in Buffer.
844 If the read goes beyond the end of the file, the read length is truncated to the
845 end of the file. The file's current position is increased by the number of bytes
846 returned. If FileHandle is a directory, the function reads the directory entry
847 at the file's current position and returns the entry in Buffer. If the Buffer
848 is not large enough to hold the current directory entry, then
849 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
850 BufferSize is set to be the size of the buffer needed to read the entry. On
851 success, the current position is updated to the next directory entry. If there
852 are no more directory entries, the read returns a zero-length buffer.
853 EFI_FILE_INFO is the structure returned as the directory entry.
855 @param FileHandle the opened file handle
856 @param BufferSize on input the size of buffer in bytes. on return
857 the number of bytes written.
858 @param Buffer the buffer to put read data into.
860 @retval EFI_SUCCESS Data was read.
861 @retval EFI_NO_MEDIA The device has no media.
862 @retval EFI_DEVICE_ERROR The device reported an error.
863 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
864 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
871 IN SHELL_FILE_HANDLE FileHandle
,
872 IN OUT UINTN
*BufferSize
,
876 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
881 Write data to a file.
883 This function writes the specified number of bytes to the file at the current
884 file position. The current file position is advanced the actual number of bytes
885 written, which is returned in BufferSize. Partial writes only occur when there
886 has been a data error during the write attempt (such as "volume space full").
887 The file is automatically grown to hold the data if required. Direct writes to
888 opened directories are not supported.
890 @param FileHandle The opened file for writing
891 @param BufferSize on input the number of bytes in Buffer. On output
892 the number of bytes written.
893 @param Buffer the buffer containing data to write is stored.
895 @retval EFI_SUCCESS Data was written.
896 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
897 @retval EFI_NO_MEDIA The device has no media.
898 @retval EFI_DEVICE_ERROR The device reported an error.
899 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
900 @retval EFI_WRITE_PROTECTED The device is write-protected.
901 @retval EFI_ACCESS_DENIED The file was open for read only.
902 @retval EFI_VOLUME_FULL The volume is full.
907 IN SHELL_FILE_HANDLE FileHandle
,
908 IN OUT UINTN
*BufferSize
,
912 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
916 Close an open file handle.
918 This function closes a specified file handle. All "dirty" cached file data is
919 flushed to the device, and the file is closed. In all cases the handle is
922 @param FileHandle the file handle to close.
924 @retval EFI_SUCCESS the file handle was closed sucessfully.
929 IN SHELL_FILE_HANDLE
*FileHandle
932 return (FileFunctionMap
.CloseFile(*FileHandle
));
936 Delete a file and close the handle
938 This function closes and deletes a file. In all cases the file handle is closed.
939 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
940 returned, but the handle is still closed.
942 @param FileHandle the file handle to delete
944 @retval EFI_SUCCESS the file was closed sucessfully
945 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
947 @retval INVALID_PARAMETER One of the parameters has an invalid value.
952 IN SHELL_FILE_HANDLE
*FileHandle
955 return (FileFunctionMap
.DeleteFile(*FileHandle
));
959 Set the current position in a file.
961 This function sets the current file position for the handle to the position
962 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
963 absolute positioning is supported, and seeking past the end of the file is
964 allowed (a subsequent write would grow the file). Seeking to position
965 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
966 If FileHandle is a directory, the only position that may be set is zero. This
967 has the effect of starting the read process of the directory entries over.
969 @param FileHandle The file handle on which the position is being set
970 @param Position Byte position from begining of file
972 @retval EFI_SUCCESS Operation completed sucessfully.
973 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
975 @retval INVALID_PARAMETER One of the parameters has an invalid value.
979 ShellSetFilePosition (
980 IN SHELL_FILE_HANDLE FileHandle
,
984 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
988 Gets a file's current position
990 This function retrieves the current file position for the file handle. For
991 directories, the current file position has no meaning outside of the file
992 system driver and as such the operation is not supported. An error is returned
993 if FileHandle is a directory.
995 @param FileHandle The open file handle on which to get the position.
996 @param Position Byte position from begining of file.
998 @retval EFI_SUCCESS the operation completed sucessfully.
999 @retval INVALID_PARAMETER One of the parameters has an invalid value.
1000 @retval EFI_UNSUPPORTED the request is not valid on directories.
1004 ShellGetFilePosition (
1005 IN SHELL_FILE_HANDLE FileHandle
,
1006 OUT UINT64
*Position
1009 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
1012 Flushes data on a file
1014 This function flushes all modified data associated with a file to a device.
1016 @param FileHandle The file handle on which to flush data
1018 @retval EFI_SUCCESS The data was flushed.
1019 @retval EFI_NO_MEDIA The device has no media.
1020 @retval EFI_DEVICE_ERROR The device reported an error.
1021 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1022 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
1023 @retval EFI_ACCESS_DENIED The file was opened for read only.
1028 IN SHELL_FILE_HANDLE FileHandle
1031 return (FileFunctionMap
.FlushFile(FileHandle
));
1034 /** Retrieve first entry from a directory.
1036 This function takes an open directory handle and gets information from the
1037 first entry in the directory. A buffer is allocated to contain
1038 the information and a pointer to the buffer is returned in *Buffer. The
1039 caller can use ShellFindNextFile() to get subsequent directory entries.
1041 The buffer will be freed by ShellFindNextFile() when the last directory
1042 entry is read. Otherwise, the caller must free the buffer, using FreePool,
1043 when finished with it.
1045 @param[in] DirHandle The file handle of the directory to search.
1046 @param[out] Buffer The pointer to the buffer for the file's information.
1048 @retval EFI_SUCCESS Found the first file.
1049 @retval EFI_NOT_FOUND Cannot find the directory.
1050 @retval EFI_NO_MEDIA The device has no media.
1051 @retval EFI_DEVICE_ERROR The device reported an error.
1052 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1053 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
1058 ShellFindFirstFile (
1059 IN SHELL_FILE_HANDLE DirHandle
,
1060 OUT EFI_FILE_INFO
**Buffer
1064 // pass to file handle lib
1066 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
1068 /** Retrieve next entries from a directory.
1070 To use this function, the caller must first call the ShellFindFirstFile()
1071 function to get the first directory entry. Subsequent directory entries are
1072 retrieved by using the ShellFindNextFile() function. This function can
1073 be called several times to get each entry from the directory. If the call of
1074 ShellFindNextFile() retrieved the last directory entry, the next call of
1075 this function will set *NoFile to TRUE and free the buffer.
1077 @param[in] DirHandle The file handle of the directory.
1078 @param[out] Buffer The pointer to buffer for file's information.
1079 @param[out] NoFile The pointer to boolean when last file is found.
1081 @retval EFI_SUCCESS Found the next file, or reached last file
1082 @retval EFI_NO_MEDIA The device has no media.
1083 @retval EFI_DEVICE_ERROR The device reported an error.
1084 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1089 IN SHELL_FILE_HANDLE DirHandle
,
1090 OUT EFI_FILE_INFO
*Buffer
,
1095 // pass to file handle lib
1097 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
1100 Retrieve the size of a file.
1102 if FileHandle is NULL then ASSERT()
1103 if Size is NULL then ASSERT()
1105 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
1108 @param FileHandle file handle from which size is retrieved
1109 @param Size pointer to size
1111 @retval EFI_SUCCESS operation was completed sucessfully
1112 @retval EFI_DEVICE_ERROR cannot access the file
1117 IN SHELL_FILE_HANDLE FileHandle
,
1121 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
1124 Retrieves the status of the break execution flag
1126 this function is useful to check whether the application is being asked to halt by the shell.
1128 @retval TRUE the execution break is enabled
1129 @retval FALSE the execution break is not enabled
1133 ShellGetExecutionBreakFlag(
1138 // Check for UEFI Shell 2.0 protocols
1140 if (gEfiShellProtocol
!= NULL
) {
1143 // We are using UEFI Shell 2.0; see if the event has been triggered
1145 if (gBS
->CheckEvent(gEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
1152 // using EFI Shell; call the function to check
1154 if (mEfiShellEnvironment2
!= NULL
) {
1155 return (mEfiShellEnvironment2
->GetExecutionBreak());
1161 return the value of an environment variable
1163 this function gets the value of the environment variable set by the
1164 ShellSetEnvironmentVariable function
1166 @param EnvKey The key name of the environment variable.
1168 @retval NULL the named environment variable does not exist.
1169 @return != NULL pointer to the value of the environment variable
1173 ShellGetEnvironmentVariable (
1174 IN CONST CHAR16
*EnvKey
1178 // Check for UEFI Shell 2.0 protocols
1180 if (gEfiShellProtocol
!= NULL
) {
1181 return (gEfiShellProtocol
->GetEnv(EnvKey
));
1185 // Check for EFI shell
1187 if (mEfiShellEnvironment2
!= NULL
) {
1188 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1194 set the value of an environment variable
1196 This function changes the current value of the specified environment variable. If the
1197 environment variable exists and the Value is an empty string, then the environment
1198 variable is deleted. If the environment variable exists and the Value is not an empty
1199 string, then the value of the environment variable is changed. If the environment
1200 variable does not exist and the Value is an empty string, there is no action. If the
1201 environment variable does not exist and the Value is a non-empty string, then the
1202 environment variable is created and assigned the specified value.
1204 This is not supported pre-UEFI Shell 2.0.
1206 @param EnvKey The key name of the environment variable.
1207 @param EnvVal The Value of the environment variable
1208 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1210 @retval EFI_SUCCESS the operation was completed sucessfully
1211 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1215 ShellSetEnvironmentVariable (
1216 IN CONST CHAR16
*EnvKey
,
1217 IN CONST CHAR16
*EnvVal
,
1222 // Check for UEFI Shell 2.0 protocols
1224 if (gEfiShellProtocol
!= NULL
) {
1225 return (gEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1229 // This feature does not exist under EFI shell
1231 return (EFI_UNSUPPORTED
);
1235 Cause the shell to parse and execute a command line.
1237 This function creates a nested instance of the shell and executes the specified
1238 command (CommandLine) with the specified environment (Environment). Upon return,
1239 the status code returned by the specified command is placed in StatusCode.
1240 If Environment is NULL, then the current environment is used and all changes made
1241 by the commands executed will be reflected in the current environment. If the
1242 Environment is non-NULL, then the changes made will be discarded.
1243 The CommandLine is executed from the current working directory on the current
1246 The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0
1247 environment. The values pointed to by the parameters will be unchanged by the
1248 ShellExecute() function. The Output parameter has no effect in a
1249 UEFI Shell 2.0 environment.
1251 @param[in] ParentHandle The parent image starting the operation.
1252 @param[in] CommandLine The pointer to a NULL terminated command line.
1253 @param[in] Output True to display debug output. False to hide it.
1254 @param[in] EnvironmentVariables Optional pointer to array of environment variables
1255 in the form "x=y". If NULL, the current set is used.
1256 @param[out] Status The status of the run command line.
1258 @retval EFI_SUCCESS The operation completed sucessfully. Status
1259 contains the status code returned.
1260 @retval EFI_INVALID_PARAMETER A parameter contains an invalid value.
1261 @retval EFI_OUT_OF_RESOURCES Out of resources.
1262 @retval EFI_UNSUPPORTED The operation is not allowed.
1267 IN EFI_HANDLE
*ParentHandle
,
1268 IN CHAR16
*CommandLine OPTIONAL
,
1269 IN BOOLEAN Output OPTIONAL
,
1270 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1271 OUT EFI_STATUS
*Status OPTIONAL
1274 EFI_STATUS CmdStatus
;
1276 // Check for UEFI Shell 2.0 protocols
1278 if (gEfiShellProtocol
!= NULL
) {
1280 // Call UEFI Shell 2.0 version (not using Output parameter)
1282 return (gEfiShellProtocol
->Execute(ParentHandle
,
1284 EnvironmentVariables
,
1289 // Check for EFI shell
1291 if (mEfiShellEnvironment2
!= NULL
) {
1293 // Call EFI Shell version.
1294 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1296 CmdStatus
= (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1300 // No Status output parameter so just use the returned status
1302 if (Status
!= NULL
) {
1303 *Status
= CmdStatus
;
1306 // If there was an error, we can't tell if it was from the command or from
1307 // the Execute() function, so we'll just assume the shell ran successfully
1308 // and the error came from the command.
1313 return (EFI_UNSUPPORTED
);
1317 Retreives the current directory path
1319 If the DeviceName is NULL, it returns the current device's current directory
1320 name. If the DeviceName is not NULL, it returns the current directory name
1323 Note that the current directory string should exclude the tailing backslash character.
1325 @param DeviceName the name of the drive to get directory on
1327 @retval NULL the directory does not exist
1328 @return != NULL the directory
1332 ShellGetCurrentDir (
1333 IN CHAR16
* CONST DeviceName OPTIONAL
1337 // Check for UEFI Shell 2.0 protocols
1339 if (gEfiShellProtocol
!= NULL
) {
1340 return (gEfiShellProtocol
->GetCurDir(DeviceName
));
1344 // Check for EFI shell
1346 if (mEfiShellEnvironment2
!= NULL
) {
1347 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1353 sets (enabled or disabled) the page break mode
1355 when page break mode is enabled the screen will stop scrolling
1356 and wait for operator input before scrolling a subsequent screen.
1358 @param CurrentState TRUE to enable and FALSE to disable
1362 ShellSetPageBreakMode (
1363 IN BOOLEAN CurrentState
1367 // check for enabling
1369 if (CurrentState
!= 0x00) {
1371 // check for UEFI Shell 2.0
1373 if (gEfiShellProtocol
!= NULL
) {
1375 // Enable with UEFI 2.0 Shell
1377 gEfiShellProtocol
->EnablePageBreak();
1381 // Check for EFI shell
1383 if (mEfiShellEnvironment2
!= NULL
) {
1385 // Enable with EFI Shell
1387 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1393 // check for UEFI Shell 2.0
1395 if (gEfiShellProtocol
!= NULL
) {
1397 // Disable with UEFI 2.0 Shell
1399 gEfiShellProtocol
->DisablePageBreak();
1403 // Check for EFI shell
1405 if (mEfiShellEnvironment2
!= NULL
) {
1407 // Disable with EFI Shell
1409 mEfiShellEnvironment2
->DisablePageBreak ();
1417 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1418 /// This allows for the struct to be populated.
1425 SHELL_FILE_HANDLE Handle
;
1426 EFI_FILE_INFO
*Info
;
1427 } EFI_SHELL_FILE_INFO_NO_CONST
;
1430 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1432 if OldStyleFileList is NULL then ASSERT()
1434 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1435 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1436 the ShellCloseFileMetaArg function.
1438 @param[in] FileList the EFI shell list type
1439 @param[in, out] ListHead the list to add to
1441 @retval the resultant head of the double linked new format list;
1444 InternalShellConvertFileListType (
1445 IN LIST_ENTRY
*FileList
,
1446 IN OUT LIST_ENTRY
*ListHead
1449 SHELL_FILE_ARG
*OldInfo
;
1451 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1456 ASSERT(FileList
!= NULL
);
1457 ASSERT(ListHead
!= NULL
);
1460 // enumerate through each member of the old list and copy
1462 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1463 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1464 ASSERT(OldInfo
!= NULL
);
1467 // Skip ones that failed to open...
1469 if (OldInfo
->Status
!= EFI_SUCCESS
) {
1474 // make sure the old list was valid
1476 ASSERT(OldInfo
->Info
!= NULL
);
1477 ASSERT(OldInfo
->FullName
!= NULL
);
1478 ASSERT(OldInfo
->FileName
!= NULL
);
1481 // allocate a new EFI_SHELL_FILE_INFO object
1483 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1484 if (NewInfo
== NULL
) {
1485 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1491 // copy the simple items
1493 NewInfo
->Handle
= OldInfo
->Handle
;
1494 NewInfo
->Status
= OldInfo
->Status
;
1496 // old shell checks for 0 not NULL
1497 OldInfo
->Handle
= 0;
1500 // allocate new space to copy strings and structure
1502 NewInfo
->FullName
= AllocateCopyPool(StrSize(OldInfo
->FullName
), OldInfo
->FullName
);
1503 NewInfo
->FileName
= AllocateCopyPool(StrSize(OldInfo
->FileName
), OldInfo
->FileName
);
1504 NewInfo
->Info
= AllocateCopyPool((UINTN
)OldInfo
->Info
->Size
, OldInfo
->Info
);
1507 // make sure all the memory allocations were sucessful
1509 if (NULL
== NewInfo
->FullName
|| NewInfo
->FileName
== NULL
|| NewInfo
->Info
== NULL
) {
1511 // Free the partially allocated new node
1513 SHELL_FREE_NON_NULL(NewInfo
->FullName
);
1514 SHELL_FREE_NON_NULL(NewInfo
->FileName
);
1515 SHELL_FREE_NON_NULL(NewInfo
->Info
);
1516 SHELL_FREE_NON_NULL(NewInfo
);
1519 // Free the previously converted stuff
1521 ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO
**)(&ListHead
));
1527 // add that to the list
1529 InsertTailList(ListHead
, &NewInfo
->Link
);
1534 Opens a group of files based on a path.
1536 This function uses the Arg to open all the matching files. Each matched
1537 file has a SHELL_FILE_INFO structure to record the file information. These
1538 structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO
1539 structures from ListHead to access each file. This function supports wildcards
1540 and will process '?' and '*' as such. the list must be freed with a call to
1541 ShellCloseFileMetaArg().
1543 If you are NOT appending to an existing list *ListHead must be NULL. If
1544 *ListHead is NULL then it must be callee freed.
1546 @param Arg pointer to path string
1547 @param OpenMode mode to open files with
1548 @param ListHead head of linked list of results
1550 @retval EFI_SUCCESS the operation was sucessful and the list head
1551 contains the list of opened files
1552 @return != EFI_SUCCESS the operation failed
1554 @sa InternalShellConvertFileListType
1558 ShellOpenFileMetaArg (
1561 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1565 LIST_ENTRY mOldStyleFileList
;
1566 CHAR16
*CleanFilePathStr
;
1569 // ASSERT that Arg and ListHead are not NULL
1571 ASSERT(Arg
!= NULL
);
1572 ASSERT(ListHead
!= NULL
);
1574 CleanFilePathStr
= NULL
;
1576 Status
= InternalShellStripQuotes (Arg
, &CleanFilePathStr
);
1577 if (EFI_ERROR (Status
)) {
1582 // Check for UEFI Shell 2.0 protocols
1584 if (gEfiShellProtocol
!= NULL
) {
1585 if (*ListHead
== NULL
) {
1586 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1587 if (*ListHead
== NULL
) {
1588 FreePool(CleanFilePathStr
);
1589 return (EFI_OUT_OF_RESOURCES
);
1591 InitializeListHead(&((*ListHead
)->Link
));
1593 Status
= gEfiShellProtocol
->OpenFileList(CleanFilePathStr
,
1596 if (EFI_ERROR(Status
)) {
1597 gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1599 Status
= gEfiShellProtocol
->RemoveDupInFileList(ListHead
);
1601 if (*ListHead
!= NULL
&& IsListEmpty(&(*ListHead
)->Link
)) {
1602 FreePool(*ListHead
);
1603 FreePool(CleanFilePathStr
);
1605 return (EFI_NOT_FOUND
);
1607 FreePool(CleanFilePathStr
);
1612 // Check for EFI shell
1614 if (mEfiShellEnvironment2
!= NULL
) {
1616 // make sure the list head is initialized
1618 InitializeListHead(&mOldStyleFileList
);
1621 // Get the EFI Shell list of files
1623 Status
= mEfiShellEnvironment2
->FileMetaArg(CleanFilePathStr
, &mOldStyleFileList
);
1624 if (EFI_ERROR(Status
)) {
1626 FreePool(CleanFilePathStr
);
1630 if (*ListHead
== NULL
) {
1631 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1632 if (*ListHead
== NULL
) {
1633 FreePool(CleanFilePathStr
);
1634 return (EFI_OUT_OF_RESOURCES
);
1636 InitializeListHead(&((*ListHead
)->Link
));
1640 // Convert that to equivalent of UEFI Shell 2.0 structure
1642 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1645 // Free the EFI Shell version that was converted.
1647 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1649 if ((*ListHead
)->Link
.ForwardLink
== (*ListHead
)->Link
.BackLink
&& (*ListHead
)->Link
.BackLink
== &((*ListHead
)->Link
)) {
1650 FreePool(*ListHead
);
1652 Status
= EFI_NOT_FOUND
;
1654 FreePool(CleanFilePathStr
);
1658 FreePool(CleanFilePathStr
);
1659 return (EFI_UNSUPPORTED
);
1662 Free the linked list returned from ShellOpenFileMetaArg.
1664 if ListHead is NULL then ASSERT().
1666 @param ListHead the pointer to free.
1668 @retval EFI_SUCCESS the operation was sucessful.
1672 ShellCloseFileMetaArg (
1673 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1679 // ASSERT that ListHead is not NULL
1681 ASSERT(ListHead
!= NULL
);
1684 // Check for UEFI Shell 2.0 protocols
1686 if (gEfiShellProtocol
!= NULL
) {
1687 return (gEfiShellProtocol
->FreeFileList(ListHead
));
1688 } else if (mEfiShellEnvironment2
!= NULL
) {
1690 // Since this is EFI Shell version we need to free our internally made copy
1693 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1694 ; *ListHead
!= NULL
&& !IsListEmpty(&(*ListHead
)->Link
)
1695 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1696 RemoveEntryList(Node
);
1697 ((EFI_FILE_PROTOCOL
*)((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
)->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1698 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1699 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1700 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1701 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1703 SHELL_FREE_NON_NULL(*ListHead
);
1707 return (EFI_UNSUPPORTED
);
1711 Find a file by searching the CWD and then the path.
1713 If FileName is NULL then ASSERT.
1715 If the return value is not NULL then the memory must be caller freed.
1717 @param FileName Filename string.
1719 @retval NULL the file was not found
1720 @return !NULL the full path to the file.
1725 IN CONST CHAR16
*FileName
1729 SHELL_FILE_HANDLE Handle
;
1733 CONST CHAR16
*Walker
;
1740 // First make sure its not an absolute path.
1742 Status
= ShellOpenFileByName(FileName
, &Handle
, EFI_FILE_MODE_READ
, 0);
1743 if (!EFI_ERROR(Status
)){
1744 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1745 ASSERT(RetVal
== NULL
);
1746 RetVal
= StrnCatGrow(&RetVal
, NULL
, FileName
, 0);
1747 ShellCloseFile(&Handle
);
1750 ShellCloseFile(&Handle
);
1754 Path
= ShellGetEnvironmentVariable(L
"cwd");
1756 Size
= StrSize(Path
) + sizeof(CHAR16
);
1757 Size
+= StrSize(FileName
);
1758 TestPath
= AllocateZeroPool(Size
);
1759 if (TestPath
== NULL
) {
1762 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), Path
);
1763 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1764 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1765 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1766 if (!EFI_ERROR(Status
)){
1767 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1768 ASSERT(RetVal
== NULL
);
1769 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1770 ShellCloseFile(&Handle
);
1774 ShellCloseFile(&Handle
);
1779 Path
= ShellGetEnvironmentVariable(L
"path");
1781 Size
= StrSize(Path
)+sizeof(CHAR16
);
1782 Size
+= StrSize(FileName
);
1783 TestPath
= AllocateZeroPool(Size
);
1784 if (TestPath
== NULL
) {
1787 Walker
= (CHAR16
*)Path
;
1789 CopyMem(TestPath
, Walker
, StrSize(Walker
));
1790 if (TestPath
!= NULL
) {
1791 TempChar
= StrStr(TestPath
, L
";");
1792 if (TempChar
!= NULL
) {
1793 *TempChar
= CHAR_NULL
;
1795 if (TestPath
[StrLen(TestPath
)-1] != L
'\\') {
1796 StrCatS(TestPath
, Size
/sizeof(CHAR16
), L
"\\");
1798 if (FileName
[0] == L
'\\') {
1801 StrCatS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1802 if (StrStr(Walker
, L
";") != NULL
) {
1803 Walker
= StrStr(Walker
, L
";") + 1;
1807 Status
= ShellOpenFileByName(TestPath
, &Handle
, EFI_FILE_MODE_READ
, 0);
1808 if (!EFI_ERROR(Status
)){
1809 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
1810 ASSERT(RetVal
== NULL
);
1811 RetVal
= StrnCatGrow(&RetVal
, NULL
, TestPath
, 0);
1812 ShellCloseFile(&Handle
);
1815 ShellCloseFile(&Handle
);
1819 } while (Walker
!= NULL
&& Walker
[0] != CHAR_NULL
);
1826 Find a file by searching the CWD and then the path with a variable set of file
1827 extensions. If the file is not found it will append each extension in the list
1828 in the order provided and return the first one that is successful.
1830 If FileName is NULL, then ASSERT.
1831 If FileExtension is NULL, then behavior is identical to ShellFindFilePath.
1833 If the return value is not NULL then the memory must be caller freed.
1835 @param[in] FileName Filename string.
1836 @param[in] FileExtension Semi-colon delimeted list of possible extensions.
1838 @retval NULL The file was not found.
1839 @retval !NULL The path to the file.
1843 ShellFindFilePathEx (
1844 IN CONST CHAR16
*FileName
,
1845 IN CONST CHAR16
*FileExtension
1850 CONST CHAR16
*ExtensionWalker
;
1855 ASSERT(FileName
!= NULL
);
1856 if (FileExtension
== NULL
) {
1857 return (ShellFindFilePath(FileName
));
1859 RetVal
= ShellFindFilePath(FileName
);
1860 if (RetVal
!= NULL
) {
1863 Size
= StrSize(FileName
);
1864 Size
+= StrSize(FileExtension
);
1865 TestPath
= AllocateZeroPool(Size
);
1866 if (TestPath
== NULL
) {
1869 for (ExtensionWalker
= FileExtension
, TempChar2
= (CHAR16
*)FileExtension
; TempChar2
!= NULL
; ExtensionWalker
= TempChar2
+ 1){
1870 StrCpyS(TestPath
, Size
/sizeof(CHAR16
), FileName
);
1871 if (ExtensionWalker
!= NULL
) {
1872 StrCatS(TestPath
, Size
/sizeof(CHAR16
), ExtensionWalker
);
1874 TempChar
= StrStr(TestPath
, L
";");
1875 if (TempChar
!= NULL
) {
1876 *TempChar
= CHAR_NULL
;
1878 RetVal
= ShellFindFilePath(TestPath
);
1879 if (RetVal
!= NULL
) {
1882 ASSERT(ExtensionWalker
!= NULL
);
1883 TempChar2
= StrStr(ExtensionWalker
, L
";");
1892 SHELL_PARAM_TYPE Type
;
1894 UINTN OriginalPosition
;
1895 } SHELL_PARAM_PACKAGE
;
1898 Checks the list of valid arguments and returns TRUE if the item was found. If the
1899 return value is TRUE then the type parameter is set also.
1901 if CheckList is NULL then ASSERT();
1902 if Name is NULL then ASSERT();
1903 if Type is NULL then ASSERT();
1905 @param Name pointer to Name of parameter found
1906 @param CheckList List to check against
1907 @param Type pointer to type of parameter if it was found
1909 @retval TRUE the Parameter was found. Type is valid.
1910 @retval FALSE the Parameter was not found. Type is not valid.
1913 InternalIsOnCheckList (
1914 IN CONST CHAR16
*Name
,
1915 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1916 OUT SHELL_PARAM_TYPE
*Type
1919 SHELL_PARAM_ITEM
*TempListItem
;
1923 // ASSERT that all 3 pointer parameters aren't NULL
1925 ASSERT(CheckList
!= NULL
);
1926 ASSERT(Type
!= NULL
);
1927 ASSERT(Name
!= NULL
);
1930 // question mark and page break mode are always supported
1932 if ((StrCmp(Name
, L
"-?") == 0) ||
1933 (StrCmp(Name
, L
"-b") == 0)
1940 // Enumerate through the list
1942 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1944 // If the Type is TypeStart only check the first characters of the passed in param
1945 // If it matches set the type and return TRUE
1947 if (TempListItem
->Type
== TypeStart
) {
1948 if (StrnCmp(Name
, TempListItem
->Name
, StrLen(TempListItem
->Name
)) == 0) {
1949 *Type
= TempListItem
->Type
;
1953 TempString
= StrnCatGrow(&TempString
, NULL
, Name
, StrLen(TempListItem
->Name
));
1954 if (TempString
!= NULL
) {
1955 if (StringNoCaseCompare(&TempString
, &TempListItem
->Name
) == 0) {
1956 *Type
= TempListItem
->Type
;
1957 FreePool(TempString
);
1960 FreePool(TempString
);
1962 } else if (StringNoCaseCompare(&Name
, &TempListItem
->Name
) == 0) {
1963 *Type
= TempListItem
->Type
;
1971 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1973 @param[in] Name pointer to Name of parameter found
1974 @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not.
1975 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
1977 @retval TRUE the Parameter is a flag.
1978 @retval FALSE the Parameter not a flag.
1982 IN CONST CHAR16
*Name
,
1983 IN CONST BOOLEAN AlwaysAllowNumbers
,
1984 IN CONST BOOLEAN TimeNumbers
1988 // ASSERT that Name isn't NULL
1990 ASSERT(Name
!= NULL
);
1993 // If we accept numbers then dont return TRUE. (they will be values)
1995 if (((Name
[0] == L
'-' || Name
[0] == L
'+') && InternalShellIsHexOrDecimalNumber(Name
+1, FALSE
, FALSE
, TimeNumbers
)) && AlwaysAllowNumbers
) {
2000 // If the Name has a /, +, or - as the first character return TRUE
2002 if ((Name
[0] == L
'/') ||
2003 (Name
[0] == L
'-') ||
2012 Checks the command line arguments passed against the list of valid ones.
2014 If no initialization is required, then return RETURN_SUCCESS.
2016 @param[in] CheckList pointer to list of parameters to check
2017 @param[out] CheckPackage pointer to pointer to list checked values
2018 @param[out] ProblemParam optional pointer to pointer to unicode string for
2019 the paramater that caused failure. If used then the
2020 caller is responsible for freeing the memory.
2021 @param[in] AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
2022 @param[in] Argv pointer to array of parameters
2023 @param[in] Argc Count of parameters in Argv
2024 @param[in] AlwaysAllowNumbers TRUE to allow numbers always, FALSE otherwise.
2026 @retval EFI_SUCCESS The operation completed sucessfully.
2027 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
2028 @retval EFI_INVALID_PARAMETER A parameter was invalid
2029 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
2030 duplicated. the duplicated command line argument
2031 was returned in ProblemParam if provided.
2032 @retval EFI_NOT_FOUND a argument required a value that was missing.
2033 the invalid command line argument was returned in
2034 ProblemParam if provided.
2037 InternalCommandLineParse (
2038 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2039 OUT LIST_ENTRY
**CheckPackage
,
2040 OUT CHAR16
**ProblemParam OPTIONAL
,
2041 IN BOOLEAN AutoPageBreak
,
2042 IN CONST CHAR16
**Argv
,
2044 IN BOOLEAN AlwaysAllowNumbers
2048 SHELL_PARAM_TYPE CurrentItemType
;
2049 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
2053 CONST CHAR16
*TempPointer
;
2054 UINTN CurrentValueSize
;
2057 CurrentItemPackage
= NULL
;
2063 // If there is only 1 item we dont need to do anything
2066 *CheckPackage
= NULL
;
2067 return (EFI_SUCCESS
);
2073 ASSERT(CheckList
!= NULL
);
2074 ASSERT(Argv
!= NULL
);
2077 // initialize the linked list
2079 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
2080 if (*CheckPackage
== NULL
) {
2081 return (EFI_OUT_OF_RESOURCES
);
2084 InitializeListHead(*CheckPackage
);
2087 // loop through each of the arguments
2089 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
2090 if (Argv
[LoopCounter
] == NULL
) {
2092 // do nothing for NULL argv
2094 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
)) {
2096 // We might have leftover if last parameter didnt have optional value
2098 if (GetItemValue
!= 0) {
2100 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2105 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2106 if (CurrentItemPackage
== NULL
) {
2107 ShellCommandLineFreeVarList(*CheckPackage
);
2108 *CheckPackage
= NULL
;
2109 return (EFI_OUT_OF_RESOURCES
);
2111 CurrentItemPackage
->Name
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2112 if (CurrentItemPackage
->Name
== NULL
) {
2113 ShellCommandLineFreeVarList(*CheckPackage
);
2114 *CheckPackage
= NULL
;
2115 return (EFI_OUT_OF_RESOURCES
);
2117 CurrentItemPackage
->Type
= CurrentItemType
;
2118 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
2119 CurrentItemPackage
->Value
= NULL
;
2122 // Does this flag require a value
2124 switch (CurrentItemPackage
->Type
) {
2126 // possibly trigger the next loop(s) to populate the value of this item
2133 case TypeDoubleValue
:
2138 GetItemValue
= (UINTN
)(-1);
2143 // this item has no value expected; we are done
2145 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2146 ASSERT(GetItemValue
== 0);
2149 } else if (GetItemValue
!= 0 && CurrentItemPackage
!= NULL
&& !InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, (BOOLEAN
)(CurrentItemPackage
->Type
== TypeTimeValue
))) {
2151 // get the item VALUE for a previous flag
2153 CurrentValueSize
= ValueSize
+ StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2154 NewValue
= ReallocatePool(ValueSize
, CurrentValueSize
, CurrentItemPackage
->Value
);
2155 if (NewValue
== NULL
) {
2156 SHELL_FREE_NON_NULL (CurrentItemPackage
->Value
);
2157 SHELL_FREE_NON_NULL (CurrentItemPackage
);
2158 ShellCommandLineFreeVarList (*CheckPackage
);
2159 *CheckPackage
= NULL
;
2160 return EFI_OUT_OF_RESOURCES
;
2162 CurrentItemPackage
->Value
= NewValue
;
2163 if (ValueSize
== 0) {
2164 StrCpyS( CurrentItemPackage
->Value
,
2165 CurrentValueSize
/sizeof(CHAR16
),
2169 StrCatS( CurrentItemPackage
->Value
,
2170 CurrentValueSize
/sizeof(CHAR16
),
2173 StrCatS( CurrentItemPackage
->Value
,
2174 CurrentValueSize
/sizeof(CHAR16
),
2178 ValueSize
+= StrSize(Argv
[LoopCounter
]) + sizeof(CHAR16
);
2181 if (GetItemValue
== 0) {
2182 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2184 } else if (!InternalIsFlag(Argv
[LoopCounter
], AlwaysAllowNumbers
, FALSE
)){
2186 // add this one as a non-flag
2189 TempPointer
= Argv
[LoopCounter
];
2190 if ((*TempPointer
== L
'^' && *(TempPointer
+1) == L
'-')
2191 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'/')
2192 || (*TempPointer
== L
'^' && *(TempPointer
+1) == L
'+')
2196 CurrentItemPackage
= AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE
));
2197 if (CurrentItemPackage
== NULL
) {
2198 ShellCommandLineFreeVarList(*CheckPackage
);
2199 *CheckPackage
= NULL
;
2200 return (EFI_OUT_OF_RESOURCES
);
2202 CurrentItemPackage
->Name
= NULL
;
2203 CurrentItemPackage
->Type
= TypePosition
;
2204 CurrentItemPackage
->Value
= AllocateCopyPool(StrSize(TempPointer
), TempPointer
);
2205 if (CurrentItemPackage
->Value
== NULL
) {
2206 ShellCommandLineFreeVarList(*CheckPackage
);
2207 *CheckPackage
= NULL
;
2208 return (EFI_OUT_OF_RESOURCES
);
2210 CurrentItemPackage
->OriginalPosition
= Count
++;
2211 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2214 // this was a non-recognised flag... error!
2216 if (ProblemParam
!= NULL
) {
2217 *ProblemParam
= AllocateCopyPool(StrSize(Argv
[LoopCounter
]), Argv
[LoopCounter
]);
2219 ShellCommandLineFreeVarList(*CheckPackage
);
2220 *CheckPackage
= NULL
;
2221 return (EFI_VOLUME_CORRUPTED
);
2224 if (GetItemValue
!= 0) {
2226 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
2229 // support for AutoPageBreak
2231 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
2232 ShellSetPageBreakMode(TRUE
);
2234 return (EFI_SUCCESS
);
2238 Checks the command line arguments passed against the list of valid ones.
2239 Optionally removes NULL values first.
2241 If no initialization is required, then return RETURN_SUCCESS.
2243 @param[in] CheckList The pointer to list of parameters to check.
2244 @param[out] CheckPackage The package of checked values.
2245 @param[out] ProblemParam Optional pointer to pointer to unicode string for
2246 the paramater that caused failure.
2247 @param[in] AutoPageBreak Will automatically set PageBreakEnabled.
2248 @param[in] AlwaysAllowNumbers Will never fail for number based flags.
2250 @retval EFI_SUCCESS The operation completed sucessfully.
2251 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2252 @retval EFI_INVALID_PARAMETER A parameter was invalid.
2253 @retval EFI_VOLUME_CORRUPTED The command line was corrupt.
2254 @retval EFI_DEVICE_ERROR The commands contained 2 opposing arguments. One
2255 of the command line arguments was returned in
2256 ProblemParam if provided.
2257 @retval EFI_NOT_FOUND A argument required a value that was missing.
2258 The invalid command line argument was returned in
2259 ProblemParam if provided.
2263 ShellCommandLineParseEx (
2264 IN CONST SHELL_PARAM_ITEM
*CheckList
,
2265 OUT LIST_ENTRY
**CheckPackage
,
2266 OUT CHAR16
**ProblemParam OPTIONAL
,
2267 IN BOOLEAN AutoPageBreak
,
2268 IN BOOLEAN AlwaysAllowNumbers
2272 // ASSERT that CheckList and CheckPackage aren't NULL
2274 ASSERT(CheckList
!= NULL
);
2275 ASSERT(CheckPackage
!= NULL
);
2278 // Check for UEFI Shell 2.0 protocols
2280 if (gEfiShellParametersProtocol
!= NULL
) {
2281 return (InternalCommandLineParse(CheckList
,
2285 (CONST CHAR16
**) gEfiShellParametersProtocol
->Argv
,
2286 gEfiShellParametersProtocol
->Argc
,
2287 AlwaysAllowNumbers
));
2291 // ASSERT That EFI Shell is not required
2293 ASSERT (mEfiShellInterface
!= NULL
);
2294 return (InternalCommandLineParse(CheckList
,
2298 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
2299 mEfiShellInterface
->Argc
,
2300 AlwaysAllowNumbers
));
2304 Frees shell variable list that was returned from ShellCommandLineParse.
2306 This function will free all the memory that was used for the CheckPackage
2307 list of postprocessed shell arguments.
2309 this function has no return value.
2311 if CheckPackage is NULL, then return
2313 @param CheckPackage the list to de-allocate
2317 ShellCommandLineFreeVarList (
2318 IN LIST_ENTRY
*CheckPackage
2324 // check for CheckPackage == NULL
2326 if (CheckPackage
== NULL
) {
2331 // for each node in the list
2333 for ( Node
= GetFirstNode(CheckPackage
)
2334 ; !IsListEmpty(CheckPackage
)
2335 ; Node
= GetFirstNode(CheckPackage
)
2338 // Remove it from the list
2340 RemoveEntryList(Node
);
2343 // if it has a name free the name
2345 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2346 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
2350 // if it has a value free the value
2352 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
2353 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2357 // free the node structure
2359 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
2362 // free the list head node
2364 FreePool(CheckPackage
);
2367 Checks for presence of a flag parameter
2369 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
2371 if CheckPackage is NULL then return FALSE.
2372 if KeyString is NULL then ASSERT()
2374 @param CheckPackage The package of parsed command line arguments
2375 @param KeyString the Key of the command line argument to check for
2377 @retval TRUE the flag is on the command line
2378 @retval FALSE the flag is not on the command line
2382 ShellCommandLineGetFlag (
2383 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2384 IN CONST CHAR16
* CONST KeyString
2391 // return FALSE for no package or KeyString is NULL
2393 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2398 // enumerate through the list of parametrs
2400 for ( Node
= GetFirstNode(CheckPackage
)
2401 ; !IsNull (CheckPackage
, Node
)
2402 ; Node
= GetNextNode(CheckPackage
, Node
)
2405 // If the Name matches, return TRUE (and there may be NULL name)
2407 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2409 // If Type is TypeStart then only compare the begining of the strings
2411 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2412 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2416 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2417 if (TempString
!= NULL
) {
2418 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2419 FreePool(TempString
);
2422 FreePool(TempString
);
2424 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2432 Returns value from command line argument.
2434 Value parameters are in the form of "-<Key> value" or "/<Key> value".
2436 If CheckPackage is NULL, then return NULL.
2438 @param[in] CheckPackage The package of parsed command line arguments.
2439 @param[in] KeyString The Key of the command line argument to check for.
2441 @retval NULL The flag is not on the command line.
2442 @retval !=NULL The pointer to unicode string of the value.
2446 ShellCommandLineGetValue (
2447 IN CONST LIST_ENTRY
*CheckPackage
,
2448 IN CHAR16
*KeyString
2455 // return NULL for no package or KeyString is NULL
2457 if (CheckPackage
== NULL
|| KeyString
== NULL
) {
2462 // enumerate through the list of parametrs
2464 for ( Node
= GetFirstNode(CheckPackage
)
2465 ; !IsNull (CheckPackage
, Node
)
2466 ; Node
= GetNextNode(CheckPackage
, Node
)
2469 // If the Name matches, return TRUE (and there may be NULL name)
2471 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
2473 // If Type is TypeStart then only compare the begining of the strings
2475 if (((SHELL_PARAM_PACKAGE
*)Node
)->Type
== TypeStart
) {
2476 if (StrnCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
, StrLen(KeyString
)) == 0) {
2477 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2480 TempString
= StrnCatGrow(&TempString
, NULL
, KeyString
, StrLen(((SHELL_PARAM_PACKAGE
*)Node
)->Name
));
2481 if (TempString
!= NULL
) {
2482 if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2483 FreePool(TempString
);
2484 return (((SHELL_PARAM_PACKAGE
*)Node
)->Name
+ StrLen(KeyString
));
2486 FreePool(TempString
);
2488 } else if (StringNoCaseCompare(&KeyString
, &((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
2489 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2497 Returns raw value from command line argument.
2499 Raw value parameters are in the form of "value" in a specific position in the list.
2501 If CheckPackage is NULL, then return NULL.
2503 @param[in] CheckPackage The package of parsed command line arguments.
2504 @param[in] Position The position of the value.
2506 @retval NULL The flag is not on the command line.
2507 @retval !=NULL The pointer to unicode string of the value.
2511 ShellCommandLineGetRawValue (
2512 IN CONST LIST_ENTRY
* CONST CheckPackage
,
2519 // check for CheckPackage == NULL
2521 if (CheckPackage
== NULL
) {
2526 // enumerate through the list of parametrs
2528 for ( Node
= GetFirstNode(CheckPackage
)
2529 ; !IsNull (CheckPackage
, Node
)
2530 ; Node
= GetNextNode(CheckPackage
, Node
)
2533 // If the position matches, return the value
2535 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
2536 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
2543 returns the number of command line value parameters that were parsed.
2545 this will not include flags.
2547 @param[in] CheckPackage The package of parsed command line arguments.
2549 @retval (UINTN)-1 No parsing has ocurred
2550 @return other The number of value parameters found
2554 ShellCommandLineGetCount(
2555 IN CONST LIST_ENTRY
*CheckPackage
2561 if (CheckPackage
== NULL
) {
2564 for ( Node1
= GetFirstNode(CheckPackage
), Count
= 0
2565 ; !IsNull (CheckPackage
, Node1
)
2566 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2568 if (((SHELL_PARAM_PACKAGE
*)Node1
)->Name
== NULL
) {
2576 Determines if a parameter is duplicated.
2578 If Param is not NULL then it will point to a callee allocated string buffer
2579 with the parameter value if a duplicate is found.
2581 If CheckPackage is NULL, then ASSERT.
2583 @param[in] CheckPackage The package of parsed command line arguments.
2584 @param[out] Param Upon finding one, a pointer to the duplicated parameter.
2586 @retval EFI_SUCCESS No parameters were duplicated.
2587 @retval EFI_DEVICE_ERROR A duplicate was found.
2591 ShellCommandLineCheckDuplicate (
2592 IN CONST LIST_ENTRY
*CheckPackage
,
2599 ASSERT(CheckPackage
!= NULL
);
2601 for ( Node1
= GetFirstNode(CheckPackage
)
2602 ; !IsNull (CheckPackage
, Node1
)
2603 ; Node1
= GetNextNode(CheckPackage
, Node1
)
2605 for ( Node2
= GetNextNode(CheckPackage
, Node1
)
2606 ; !IsNull (CheckPackage
, Node2
)
2607 ; Node2
= GetNextNode(CheckPackage
, Node2
)
2609 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) {
2610 if (Param
!= NULL
) {
2612 *Param
= StrnCatGrow(Param
, NULL
, ((SHELL_PARAM_PACKAGE
*)Node1
)->Name
, 0);
2614 return (EFI_DEVICE_ERROR
);
2618 return (EFI_SUCCESS
);
2622 This is a find and replace function. Upon successful return the NewString is a copy of
2623 SourceString with each instance of FindTarget replaced with ReplaceWith.
2625 If SourceString and NewString overlap the behavior is undefined.
2627 If the string would grow bigger than NewSize it will halt and return error.
2629 @param[in] SourceString The string with source buffer.
2630 @param[in, out] NewString The string with resultant buffer.
2631 @param[in] NewSize The size in bytes of NewString.
2632 @param[in] FindTarget The string to look for.
2633 @param[in] ReplaceWith The string to replace FindTarget with.
2634 @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^'
2635 immediately before it.
2636 @param[in] ParameterReplacing If TRUE will add "" around items with spaces.
2638 @retval EFI_INVALID_PARAMETER SourceString was NULL.
2639 @retval EFI_INVALID_PARAMETER NewString was NULL.
2640 @retval EFI_INVALID_PARAMETER FindTarget was NULL.
2641 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL.
2642 @retval EFI_INVALID_PARAMETER FindTarget had length < 1.
2643 @retval EFI_INVALID_PARAMETER SourceString had length < 1.
2644 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
2645 the new string (truncation occurred).
2646 @retval EFI_SUCCESS The string was successfully copied with replacement.
2650 ShellCopySearchAndReplace(
2651 IN CHAR16 CONST
*SourceString
,
2652 IN OUT CHAR16
*NewString
,
2654 IN CONST CHAR16
*FindTarget
,
2655 IN CONST CHAR16
*ReplaceWith
,
2656 IN CONST BOOLEAN SkipPreCarrot
,
2657 IN CONST BOOLEAN ParameterReplacing
2663 if ( (SourceString
== NULL
)
2664 || (NewString
== NULL
)
2665 || (FindTarget
== NULL
)
2666 || (ReplaceWith
== NULL
)
2667 || (StrLen(FindTarget
) < 1)
2668 || (StrLen(SourceString
) < 1)
2670 return (EFI_INVALID_PARAMETER
);
2673 if (StrStr(ReplaceWith
, L
" ") == NULL
|| !ParameterReplacing
) {
2674 Replace
= StrnCatGrow(&Replace
, NULL
, ReplaceWith
, 0);
2676 Replace
= AllocateZeroPool(StrSize(ReplaceWith
) + 2*sizeof(CHAR16
));
2677 if (Replace
!= NULL
) {
2678 UnicodeSPrint(Replace
, StrSize(ReplaceWith
) + 2*sizeof(CHAR16
), L
"\"%s\"", ReplaceWith
);
2681 if (Replace
== NULL
) {
2682 return (EFI_OUT_OF_RESOURCES
);
2684 NewString
= ZeroMem(NewString
, NewSize
);
2685 while (*SourceString
!= CHAR_NULL
) {
2687 // if we find the FindTarget and either Skip == FALSE or Skip and we
2688 // dont have a carrot do a replace...
2690 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0
2691 && ((SkipPreCarrot
&& *(SourceString
-1) != L
'^') || !SkipPreCarrot
)
2693 SourceString
+= StrLen(FindTarget
);
2694 Size
= StrSize(NewString
);
2695 if ((Size
+ (StrLen(Replace
)*sizeof(CHAR16
))) > NewSize
) {
2697 return (EFI_BUFFER_TOO_SMALL
);
2699 StrCatS(NewString
, NewSize
/sizeof(CHAR16
), Replace
);
2701 Size
= StrSize(NewString
);
2702 if (Size
+ sizeof(CHAR16
) > NewSize
) {
2704 return (EFI_BUFFER_TOO_SMALL
);
2706 StrnCatS(NewString
, NewSize
/sizeof(CHAR16
), SourceString
, 1);
2711 return (EFI_SUCCESS
);
2715 Internal worker function to output a string.
2717 This function will output a string to the correct StdOut.
2719 @param[in] String The string to print out.
2721 @retval EFI_SUCCESS The operation was sucessful.
2722 @retval !EFI_SUCCESS The operation failed.
2726 IN CONST CHAR16
*String
2730 Size
= StrSize(String
) - sizeof(CHAR16
);
2732 return (EFI_SUCCESS
);
2734 if (gEfiShellParametersProtocol
!= NULL
) {
2735 return (gEfiShellProtocol
->WriteFile(gEfiShellParametersProtocol
->StdOut
, &Size
, (VOID
*)String
));
2737 if (mEfiShellInterface
!= NULL
) {
2738 if (mEfiShellInterface
->RedirArgc
== 0) {
2740 // Divide in half for old shell. Must be string length not size.
2742 Size
/=2; // Divide in half only when no redirection.
2744 return (mEfiShellInterface
->StdOut
->Write(mEfiShellInterface
->StdOut
, &Size
, (VOID
*)String
));
2747 return (EFI_UNSUPPORTED
);
2751 Print at a specific location on the screen.
2753 This function will move the cursor to a given screen location and print the specified string
2755 If -1 is specified for either the Row or Col the current screen location for BOTH
2758 if either Row or Col is out of range for the current console, then ASSERT
2759 if Format is NULL, then ASSERT
2761 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2762 the following additional flags:
2763 %N - Set output attribute to normal
2764 %H - Set output attribute to highlight
2765 %E - Set output attribute to error
2766 %B - Set output attribute to blue color
2767 %V - Set output attribute to green color
2769 Note: The background color is controlled by the shell command cls.
2771 @param[in] Col the column to print at
2772 @param[in] Row the row to print at
2773 @param[in] Format the format string
2774 @param[in] Marker the marker for the variable argument list
2776 @return EFI_SUCCESS The operation was successful.
2777 @return EFI_DEVICE_ERROR The console device reported an error.
2780 InternalShellPrintWorker(
2781 IN INT32 Col OPTIONAL
,
2782 IN INT32 Row OPTIONAL
,
2783 IN CONST CHAR16
*Format
,
2788 CHAR16
*ResumeLocation
;
2789 CHAR16
*FormatWalker
;
2790 UINTN OriginalAttribute
;
2791 CHAR16
*mPostReplaceFormat
;
2792 CHAR16
*mPostReplaceFormat2
;
2794 mPostReplaceFormat
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2795 mPostReplaceFormat2
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
2797 if (mPostReplaceFormat
== NULL
|| mPostReplaceFormat2
== NULL
) {
2798 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2799 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2800 return (EFI_OUT_OF_RESOURCES
);
2803 Status
= EFI_SUCCESS
;
2804 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2807 // Back and forth each time fixing up 1 of our flags...
2809 Status
= ShellCopySearchAndReplace(Format
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%N", L
"%%N", FALSE
, FALSE
);
2810 ASSERT_EFI_ERROR(Status
);
2811 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%E", L
"%%E", FALSE
, FALSE
);
2812 ASSERT_EFI_ERROR(Status
);
2813 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%H", L
"%%H", FALSE
, FALSE
);
2814 ASSERT_EFI_ERROR(Status
);
2815 Status
= ShellCopySearchAndReplace(mPostReplaceFormat
, mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%B", L
"%%B", FALSE
, FALSE
);
2816 ASSERT_EFI_ERROR(Status
);
2817 Status
= ShellCopySearchAndReplace(mPostReplaceFormat2
, mPostReplaceFormat
, PcdGet16 (PcdShellPrintBufferSize
), L
"%V", L
"%%V", FALSE
, FALSE
);
2818 ASSERT_EFI_ERROR(Status
);
2821 // Use the last buffer from replacing to print from...
2823 UnicodeVSPrint (mPostReplaceFormat2
, PcdGet16 (PcdShellPrintBufferSize
), mPostReplaceFormat
, Marker
);
2825 if (Col
!= -1 && Row
!= -1) {
2826 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2829 FormatWalker
= mPostReplaceFormat2
;
2830 while (*FormatWalker
!= CHAR_NULL
) {
2832 // Find the next attribute change request
2834 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2835 if (ResumeLocation
!= NULL
) {
2836 *ResumeLocation
= CHAR_NULL
;
2839 // print the current FormatWalker string
2841 if (StrLen(FormatWalker
)>0) {
2842 Status
= InternalPrintTo(FormatWalker
);
2843 if (EFI_ERROR(Status
)) {
2849 // update the attribute
2851 if (ResumeLocation
!= NULL
) {
2852 if ((ResumeLocation
!= mPostReplaceFormat2
) && (*(ResumeLocation
-1) == L
'^')) {
2854 // Move cursor back 1 position to overwrite the ^
2856 gST
->ConOut
->SetCursorPosition(gST
->ConOut
, gST
->ConOut
->Mode
->CursorColumn
- 1, gST
->ConOut
->Mode
->CursorRow
);
2859 // Print a simple '%' symbol
2861 Status
= InternalPrintTo(L
"%");
2862 ResumeLocation
= ResumeLocation
- 1;
2864 switch (*(ResumeLocation
+1)) {
2866 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2869 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2872 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2875 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_LIGHTBLUE
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2878 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_LIGHTGREEN
, ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2882 // Print a simple '%' symbol
2884 Status
= InternalPrintTo(L
"%");
2885 if (EFI_ERROR(Status
)) {
2888 ResumeLocation
= ResumeLocation
- 1;
2894 // reset to normal now...
2900 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2902 FormatWalker
= ResumeLocation
+ 2;
2905 gST
->ConOut
->SetAttribute(gST
->ConOut
, OriginalAttribute
);
2907 SHELL_FREE_NON_NULL(mPostReplaceFormat
);
2908 SHELL_FREE_NON_NULL(mPostReplaceFormat2
);
2913 Print at a specific location on the screen.
2915 This function will move the cursor to a given screen location and print the specified string.
2917 If -1 is specified for either the Row or Col the current screen location for BOTH
2920 If either Row or Col is out of range for the current console, then ASSERT.
2921 If Format is NULL, then ASSERT.
2923 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2924 the following additional flags:
2925 %N - Set output attribute to normal
2926 %H - Set output attribute to highlight
2927 %E - Set output attribute to error
2928 %B - Set output attribute to blue color
2929 %V - Set output attribute to green color
2931 Note: The background color is controlled by the shell command cls.
2933 @param[in] Col the column to print at
2934 @param[in] Row the row to print at
2935 @param[in] Format the format string
2936 @param[in] ... The variable argument list.
2938 @return EFI_SUCCESS The printing was successful.
2939 @return EFI_DEVICE_ERROR The console device reported an error.
2944 IN INT32 Col OPTIONAL
,
2945 IN INT32 Row OPTIONAL
,
2946 IN CONST CHAR16
*Format
,
2952 if (Format
== NULL
) {
2953 return (EFI_INVALID_PARAMETER
);
2955 VA_START (Marker
, Format
);
2956 RetVal
= InternalShellPrintWorker(Col
, Row
, Format
, Marker
);
2962 Print at a specific location on the screen.
2964 This function will move the cursor to a given screen location and print the specified string.
2966 If -1 is specified for either the Row or Col the current screen location for BOTH
2969 If either Row or Col is out of range for the current console, then ASSERT.
2970 If Format is NULL, then ASSERT.
2972 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2973 the following additional flags:
2974 %N - Set output attribute to normal.
2975 %H - Set output attribute to highlight.
2976 %E - Set output attribute to error.
2977 %B - Set output attribute to blue color.
2978 %V - Set output attribute to green color.
2980 Note: The background color is controlled by the shell command cls.
2982 @param[in] Col The column to print at.
2983 @param[in] Row The row to print at.
2984 @param[in] Language The language of the string to retrieve. If this parameter
2985 is NULL, then the current platform language is used.
2986 @param[in] HiiFormatStringId The format string Id for getting from Hii.
2987 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
2988 @param[in] ... The variable argument list.
2990 @return EFI_SUCCESS The printing was successful.
2991 @return EFI_DEVICE_ERROR The console device reported an error.
2996 IN INT32 Col OPTIONAL
,
2997 IN INT32 Row OPTIONAL
,
2998 IN CONST CHAR8
*Language OPTIONAL
,
2999 IN CONST EFI_STRING_ID HiiFormatStringId
,
3000 IN CONST EFI_HII_HANDLE HiiFormatHandle
,
3005 CHAR16
*HiiFormatString
;
3008 RetVal
= EFI_DEVICE_ERROR
;
3010 VA_START (Marker
, HiiFormatHandle
);
3011 HiiFormatString
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, Language
);
3012 if (HiiFormatString
!= NULL
) {
3013 RetVal
= InternalShellPrintWorker (Col
, Row
, HiiFormatString
, Marker
);
3014 SHELL_FREE_NON_NULL (HiiFormatString
);
3022 Function to determine if a given filename represents a file or a directory.
3024 @param[in] DirName Path to directory to test.
3026 @retval EFI_SUCCESS The Path represents a directory
3027 @retval EFI_NOT_FOUND The Path does not represent a directory
3028 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3029 @return The path failed to open
3034 IN CONST CHAR16
*DirName
3038 SHELL_FILE_HANDLE Handle
;
3039 CHAR16
*TempLocation
;
3040 CHAR16
*TempLocation2
;
3042 ASSERT(DirName
!= NULL
);
3045 TempLocation
= NULL
;
3047 Status
= ShellOpenFileByName(DirName
, &Handle
, EFI_FILE_MODE_READ
, 0);
3048 if (EFI_ERROR(Status
)) {
3050 // try good logic first.
3052 if (gEfiShellProtocol
!= NULL
) {
3053 TempLocation
= StrnCatGrow(&TempLocation
, NULL
, DirName
, 0);
3054 if (TempLocation
== NULL
) {
3055 ShellCloseFile(&Handle
);
3056 return (EFI_OUT_OF_RESOURCES
);
3058 TempLocation2
= StrStr(TempLocation
, L
":");
3059 if (TempLocation2
!= NULL
&& StrLen(StrStr(TempLocation
, L
":")) == 2) {
3060 *(TempLocation2
+1) = CHAR_NULL
;
3062 if (gEfiShellProtocol
->GetDevicePathFromMap(TempLocation
) != NULL
) {
3063 FreePool(TempLocation
);
3064 return (EFI_SUCCESS
);
3066 FreePool(TempLocation
);
3069 // probably a map name?!?!!?
3071 TempLocation
= StrStr(DirName
, L
"\\");
3072 if (TempLocation
!= NULL
&& *(TempLocation
+1) == CHAR_NULL
) {
3073 return (EFI_SUCCESS
);
3079 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
) {
3080 ShellCloseFile(&Handle
);
3081 return (EFI_SUCCESS
);
3083 ShellCloseFile(&Handle
);
3084 return (EFI_NOT_FOUND
);
3088 Function to determine if a given filename represents a file.
3090 @param[in] Name Path to file to test.
3092 @retval EFI_SUCCESS The Path represents a file.
3093 @retval EFI_NOT_FOUND The Path does not represent a file.
3094 @retval other The path failed to open.
3099 IN CONST CHAR16
*Name
3103 SHELL_FILE_HANDLE Handle
;
3105 ASSERT(Name
!= NULL
);
3109 Status
= ShellOpenFileByName(Name
, &Handle
, EFI_FILE_MODE_READ
, 0);
3110 if (EFI_ERROR(Status
)) {
3114 if (FileHandleIsDirectory(Handle
) != EFI_SUCCESS
) {
3115 ShellCloseFile(&Handle
);
3116 return (EFI_SUCCESS
);
3118 ShellCloseFile(&Handle
);
3119 return (EFI_NOT_FOUND
);
3123 Function to determine if a given filename represents a file.
3125 This will search the CWD and then the Path.
3127 If Name is NULL, then ASSERT.
3129 @param[in] Name Path to file to test.
3131 @retval EFI_SUCCESS The Path represents a file.
3132 @retval EFI_NOT_FOUND The Path does not represent a file.
3133 @retval other The path failed to open.
3138 IN CONST CHAR16
*Name
3144 if (!EFI_ERROR(ShellIsFile(Name
))) {
3145 return (EFI_SUCCESS
);
3148 NewName
= ShellFindFilePath(Name
);
3149 if (NewName
== NULL
) {
3150 return (EFI_NOT_FOUND
);
3152 Status
= ShellIsFile(NewName
);
3158 Function return the number converted from a hex representation of a number.
3160 Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid
3161 result. Use ShellConvertStringToUint64 instead.
3163 @param[in] String String representation of a number.
3165 @return The unsigned integer result of the conversion.
3166 @retval (UINTN)(-1) An error occured.
3171 IN CONST CHAR16
*String
3176 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, TRUE
, TRUE
))) {
3177 return ((UINTN
)RetVal
);
3180 return ((UINTN
)(-1));
3184 Function to determine whether a string is decimal or hex representation of a number
3185 and return the number converted from the string. Spaces are always skipped.
3187 @param[in] String String representation of a number
3190 @retval (UINTN)(-1) An error ocurred.
3195 IN CONST CHAR16
*String
3203 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, TRUE
, FALSE
)) {
3207 if (!EFI_ERROR(ShellConvertStringToUint64(String
, &RetVal
, Hex
, TRUE
))) {
3208 return ((UINTN
)RetVal
);
3210 return ((UINTN
)(-1));
3214 Safely append with automatic string resizing given length of Destination and
3215 desired length of copy from Source.
3217 append the first D characters of Source to the end of Destination, where D is
3218 the lesser of Count and the StrLen() of Source. If appending those D characters
3219 will fit within Destination (whose Size is given as CurrentSize) and
3220 still leave room for a NULL terminator, then those characters are appended,
3221 starting at the original terminating NULL of Destination, and a new terminating
3224 If appending D characters onto Destination will result in a overflow of the size
3225 given in CurrentSize the string will be grown such that the copy can be performed
3226 and CurrentSize will be updated to the new size.
3228 If Source is NULL, there is nothing to append, just return the current buffer in
3231 if Destination is NULL, then ASSERT()
3232 if Destination's current length (including NULL terminator) is already more then
3233 CurrentSize, then ASSERT()
3235 @param[in, out] Destination The String to append onto
3236 @param[in, out] CurrentSize on call the number of bytes in Destination. On
3237 return possibly the new size (still in bytes). if NULL
3238 then allocate whatever is needed.
3239 @param[in] Source The String to append from
3240 @param[in] Count Maximum number of characters to append. if 0 then
3243 @return Destination return the resultant string.
3248 IN OUT CHAR16
**Destination
,
3249 IN OUT UINTN
*CurrentSize
,
3250 IN CONST CHAR16
*Source
,
3254 UINTN DestinationStartSize
;
3260 ASSERT(Destination
!= NULL
);
3263 // If there's nothing to do then just return Destination
3265 if (Source
== NULL
) {
3266 return (*Destination
);
3270 // allow for un-initialized pointers, based on size being 0
3272 if (CurrentSize
!= NULL
&& *CurrentSize
== 0) {
3273 *Destination
= NULL
;
3277 // allow for NULL pointers address as Destination
3279 if (*Destination
!= NULL
) {
3280 ASSERT(CurrentSize
!= 0);
3281 DestinationStartSize
= StrSize(*Destination
);
3282 ASSERT(DestinationStartSize
<= *CurrentSize
);
3284 DestinationStartSize
= 0;
3285 // ASSERT(*CurrentSize == 0);
3289 // Append all of Source?
3292 Count
= StrLen(Source
);
3296 // Test and grow if required
3298 if (CurrentSize
!= NULL
) {
3299 NewSize
= *CurrentSize
;
3300 if (NewSize
< DestinationStartSize
+ (Count
* sizeof(CHAR16
))) {
3301 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
3302 NewSize
+= 2 * Count
* sizeof(CHAR16
);
3304 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
3305 *CurrentSize
= NewSize
;
3308 NewSize
= (Count
+1)*sizeof(CHAR16
);
3309 *Destination
= AllocateZeroPool(NewSize
);
3313 // Now use standard StrnCat on a big enough buffer
3315 if (*Destination
== NULL
) {
3319 StrnCatS(*Destination
, NewSize
/sizeof(CHAR16
), Source
, Count
);
3320 return *Destination
;
3324 Prompt the user and return the resultant answer to the requestor.
3326 This function will display the requested question on the shell prompt and then
3327 wait for an appropriate answer to be input from the console.
3329 if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue
3330 or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.
3332 if the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type
3335 In either case *Response must be callee freed if Response was not NULL;
3337 @param Type What type of question is asked. This is used to filter the input
3338 to prevent invalid answers to question.
3339 @param Prompt Pointer to string prompt to use to request input.
3340 @param Response Pointer to Response which will be populated upon return.
3342 @retval EFI_SUCCESS The operation was sucessful.
3343 @retval EFI_UNSUPPORTED The operation is not supported as requested.
3344 @retval EFI_INVALID_PARAMETER A parameter was invalid.
3345 @return other The operation failed.
3349 ShellPromptForResponse (
3350 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3351 IN CHAR16
*Prompt OPTIONAL
,
3352 IN OUT VOID
**Response OPTIONAL
3358 SHELL_PROMPT_RESPONSE
*Resp
;
3362 Status
= EFI_UNSUPPORTED
;
3366 if (Type
!= ShellPromptResponseTypeFreeform
) {
3367 Resp
= (SHELL_PROMPT_RESPONSE
*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE
));
3369 if (Response
!= NULL
) {
3372 return (EFI_OUT_OF_RESOURCES
);
3377 case ShellPromptResponseTypeQuitContinue
:
3378 if (Prompt
!= NULL
) {
3379 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3382 // wait for valid response
3384 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3385 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3386 if (EFI_ERROR(Status
)) {
3389 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3390 if (Key
.UnicodeChar
== L
'Q' || Key
.UnicodeChar
==L
'q') {
3391 *Resp
= ShellPromptResponseQuit
;
3393 *Resp
= ShellPromptResponseContinue
;
3396 case ShellPromptResponseTypeYesNoCancel
:
3397 if (Prompt
!= NULL
) {
3398 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3401 // wait for valid response
3403 *Resp
= ShellPromptResponseMax
;
3404 while (*Resp
== ShellPromptResponseMax
) {
3405 if (ShellGetExecutionBreakFlag()) {
3406 Status
= EFI_ABORTED
;
3409 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3410 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3411 if (EFI_ERROR(Status
)) {
3414 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3415 switch (Key
.UnicodeChar
) {
3418 *Resp
= ShellPromptResponseYes
;
3422 *Resp
= ShellPromptResponseNo
;
3426 *Resp
= ShellPromptResponseCancel
;
3431 case ShellPromptResponseTypeYesNoAllCancel
:
3432 if (Prompt
!= NULL
) {
3433 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3436 // wait for valid response
3438 *Resp
= ShellPromptResponseMax
;
3439 while (*Resp
== ShellPromptResponseMax
) {
3440 if (ShellGetExecutionBreakFlag()) {
3441 Status
= EFI_ABORTED
;
3444 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3445 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3446 if (EFI_ERROR(Status
)) {
3450 if (Key
.UnicodeChar
<= 127 && Key
.UnicodeChar
>= 32) {
3451 ShellPrintEx (-1, -1, L
"%c", Key
.UnicodeChar
);
3454 switch (Key
.UnicodeChar
) {
3457 *Resp
= ShellPromptResponseYes
;
3461 *Resp
= ShellPromptResponseNo
;
3465 *Resp
= ShellPromptResponseAll
;
3469 *Resp
= ShellPromptResponseCancel
;
3474 case ShellPromptResponseTypeEnterContinue
:
3475 case ShellPromptResponseTypeAnyKeyContinue
:
3476 if (Prompt
!= NULL
) {
3477 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3480 // wait for valid response
3482 *Resp
= ShellPromptResponseMax
;
3483 while (*Resp
== ShellPromptResponseMax
) {
3484 if (ShellGetExecutionBreakFlag()) {
3485 Status
= EFI_ABORTED
;
3488 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3489 if (Type
== ShellPromptResponseTypeEnterContinue
) {
3490 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3491 if (EFI_ERROR(Status
)) {
3494 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3495 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3496 *Resp
= ShellPromptResponseContinue
;
3500 if (Type
== ShellPromptResponseTypeAnyKeyContinue
) {
3501 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3502 ASSERT_EFI_ERROR(Status
);
3503 *Resp
= ShellPromptResponseContinue
;
3508 case ShellPromptResponseTypeYesNo
:
3509 if (Prompt
!= NULL
) {
3510 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3513 // wait for valid response
3515 *Resp
= ShellPromptResponseMax
;
3516 while (*Resp
== ShellPromptResponseMax
) {
3517 if (ShellGetExecutionBreakFlag()) {
3518 Status
= EFI_ABORTED
;
3521 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3522 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3523 if (EFI_ERROR(Status
)) {
3526 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3527 switch (Key
.UnicodeChar
) {
3530 *Resp
= ShellPromptResponseYes
;
3534 *Resp
= ShellPromptResponseNo
;
3539 case ShellPromptResponseTypeFreeform
:
3540 if (Prompt
!= NULL
) {
3541 ShellPrintEx(-1, -1, L
"%s", Prompt
);
3544 if (ShellGetExecutionBreakFlag()) {
3545 Status
= EFI_ABORTED
;
3548 gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &EventIndex
);
3549 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
3550 if (EFI_ERROR(Status
)) {
3553 ShellPrintEx(-1, -1, L
"%c", Key
.UnicodeChar
);
3554 if (Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
) {
3557 ASSERT((Buffer
== NULL
&& Size
== 0) || (Buffer
!= NULL
));
3558 StrnCatGrow(&Buffer
, &Size
, &Key
.UnicodeChar
, 1);
3562 // This is the location to add new prompt types.
3563 // If your new type loops remember to add ExecutionBreak support.
3569 if (Response
!= NULL
) {
3572 } else if (Buffer
!= NULL
) {
3581 if (Buffer
!= NULL
) {
3586 ShellPrintEx(-1, -1, L
"\r\n");
3591 Prompt the user and return the resultant answer to the requestor.
3593 This function is the same as ShellPromptForResponse, except that the prompt is
3594 automatically pulled from HII.
3596 @param Type What type of question is asked. This is used to filter the input
3597 to prevent invalid answers to question.
3598 @param[in] HiiFormatStringId The format string Id for getting from Hii.
3599 @param[in] HiiFormatHandle The format string Handle for getting from Hii.
3600 @param Response Pointer to Response which will be populated upon return.
3602 @retval EFI_SUCCESS the operation was sucessful.
3603 @return other the operation failed.
3605 @sa ShellPromptForResponse
3609 ShellPromptForResponseHii (
3610 IN SHELL_PROMPT_REQUEST_TYPE Type
,
3611 IN CONST EFI_STRING_ID HiiFormatStringId
,
3612 IN CONST EFI_HII_HANDLE HiiFormatHandle
,
3613 IN OUT VOID
**Response
3619 Prompt
= HiiGetString(HiiFormatHandle
, HiiFormatStringId
, NULL
);
3620 Status
= ShellPromptForResponse(Type
, Prompt
, Response
);
3626 Function to determin if an entire string is a valid number.
3628 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
3630 @param[in] String The string to evaluate.
3631 @param[in] ForceHex TRUE - always assume hex.
3632 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
3633 @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise.
3635 @retval TRUE It is all numeric (dec/hex) characters.
3636 @retval FALSE There is a non-numeric character.
3639 InternalShellIsHexOrDecimalNumber (
3640 IN CONST CHAR16
*String
,
3641 IN CONST BOOLEAN ForceHex
,
3642 IN CONST BOOLEAN StopAtSpace
,
3643 IN CONST BOOLEAN TimeNumbers
3647 BOOLEAN LeadingZero
;
3649 if (String
== NULL
) {
3654 // chop off a single negative sign
3656 if (*String
== L
'-') {
3660 if (*String
== CHAR_NULL
) {
3665 // chop leading zeroes
3667 LeadingZero
= FALSE
;
3668 while(*String
== L
'0'){
3673 // allow '0x' or '0X', but not 'x' or 'X'
3675 if (*String
== L
'x' || *String
== L
'X') {
3678 // we got an x without a preceeding 0
3684 } else if (ForceHex
) {
3691 // loop through the remaining characters and use the lib function
3693 for ( ; *String
!= CHAR_NULL
&& !(StopAtSpace
&& *String
== L
' ') ; String
++){
3694 if (TimeNumbers
&& (String
[0] == L
':')) {
3698 if (!ShellIsHexaDecimalDigitCharacter(*String
)) {
3702 if (!ShellIsDecimalDigitCharacter(*String
)) {
3712 Function to determine if a given filename exists.
3714 @param[in] Name Path to test.
3716 @retval EFI_SUCCESS The Path represents a file.
3717 @retval EFI_NOT_FOUND The Path does not represent a file.
3718 @retval other The path failed to open.
3723 IN CONST CHAR16
*Name
3727 EFI_SHELL_FILE_INFO
*List
;
3729 ASSERT(Name
!= NULL
);
3732 Status
= ShellOpenFileMetaArg((CHAR16
*)Name
, EFI_FILE_MODE_READ
, &List
);
3733 if (EFI_ERROR(Status
)) {
3737 ShellCloseFileMetaArg(&List
);
3739 return (EFI_SUCCESS
);
3743 Convert a Unicode character to numerical value.
3745 This internal function only deal with Unicode character
3746 which maps to a valid hexadecimal ASII character, i.e.
3747 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
3748 Unicode character, the value returned does not make sense.
3750 @param Char The character to convert.
3752 @return The numerical value converted.
3756 InternalShellHexCharToUintn (
3760 if (ShellIsDecimalDigitCharacter (Char
)) {
3764 return (10 + CharToUpper (Char
) - L
'A');
3768 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
3770 This function returns a value of type UINT64 by interpreting the contents
3771 of the Unicode string specified by String as a hexadecimal number.
3772 The format of the input Unicode string String is:
3774 [spaces][zeros][x][hexadecimal digits].
3776 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
3777 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
3778 If "x" appears in the input string, it must be prefixed with at least one 0.
3779 The function will ignore the pad space, which includes spaces or tab characters,
3780 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
3781 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
3782 first valid hexadecimal digit. Then, the function stops at the first character that is
3783 a not a valid hexadecimal character or NULL, whichever one comes first.
3785 If String has only pad spaces, then zero is returned.
3786 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
3787 then zero is returned.
3789 @param[in] String A pointer to a Null-terminated Unicode string.
3790 @param[out] Value Upon a successful return the value of the conversion.
3791 @param[in] StopAtSpace FALSE to skip spaces.
3793 @retval EFI_SUCCESS The conversion was successful.
3794 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3795 @retval EFI_DEVICE_ERROR An overflow occured.
3798 InternalShellStrHexToUint64 (
3799 IN CONST CHAR16
*String
,
3801 IN CONST BOOLEAN StopAtSpace
3806 if (String
== NULL
|| StrSize(String
) == 0 || Value
== NULL
) {
3807 return (EFI_INVALID_PARAMETER
);
3811 // Ignore the pad spaces (space or tab)
3813 while ((*String
== L
' ') || (*String
== L
'\t')) {
3818 // Ignore leading Zeros after the spaces
3820 while (*String
== L
'0') {
3824 if (CharToUpper (*String
) == L
'X') {
3825 if (*(String
- 1) != L
'0') {
3837 // there is a space where there should't be
3839 if (*String
== L
' ') {
3840 return (EFI_INVALID_PARAMETER
);
3843 while (ShellIsHexaDecimalDigitCharacter (*String
)) {
3845 // If the Hex Number represented by String overflows according
3846 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3848 if (!(Result
<= (RShiftU64((((UINT64
) ~0) - InternalShellHexCharToUintn (*String
)), 4)))) {
3849 // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {
3850 return (EFI_DEVICE_ERROR
);
3853 Result
= (LShiftU64(Result
, 4));
3854 Result
+= InternalShellHexCharToUintn (*String
);
3858 // stop at spaces if requested
3860 if (StopAtSpace
&& *String
== L
' ') {
3866 return (EFI_SUCCESS
);
3870 Convert a Null-terminated Unicode decimal string to a value of
3873 This function returns a value of type UINT64 by interpreting the contents
3874 of the Unicode string specified by String as a decimal number. The format
3875 of the input Unicode string String is:
3877 [spaces] [decimal digits].
3879 The valid decimal digit character is in the range [0-9]. The
3880 function will ignore the pad space, which includes spaces or
3881 tab characters, before [decimal digits]. The running zero in the
3882 beginning of [decimal digits] will be ignored. Then, the function
3883 stops at the first character that is a not a valid decimal character
3884 or a Null-terminator, whichever one comes first.
3886 If String has only pad spaces, then 0 is returned.
3887 If String has no pad spaces or valid decimal digits,
3890 @param[in] String A pointer to a Null-terminated Unicode string.
3891 @param[out] Value Upon a successful return the value of the conversion.
3892 @param[in] StopAtSpace FALSE to skip spaces.
3894 @retval EFI_SUCCESS The conversion was successful.
3895 @retval EFI_INVALID_PARAMETER A parameter was NULL or invalid.
3896 @retval EFI_DEVICE_ERROR An overflow occured.
3899 InternalShellStrDecimalToUint64 (
3900 IN CONST CHAR16
*String
,
3902 IN CONST BOOLEAN StopAtSpace
3907 if (String
== NULL
|| StrSize (String
) == 0 || Value
== NULL
) {
3908 return (EFI_INVALID_PARAMETER
);
3912 // Ignore the pad spaces (space or tab)
3914 while ((*String
== L
' ') || (*String
== L
'\t')) {
3919 // Ignore leading Zeros after the spaces
3921 while (*String
== L
'0') {
3928 // Stop upon space if requested
3929 // (if the whole value was 0)
3931 if (StopAtSpace
&& *String
== L
' ') {
3933 return (EFI_SUCCESS
);
3936 while (ShellIsDecimalDigitCharacter (*String
)) {
3938 // If the number represented by String overflows according
3939 // to the range defined by UINT64, then return EFI_DEVICE_ERROR.
3942 if (!(Result
<= (DivU64x32((((UINT64
) ~0) - (*String
- L
'0')),10)))) {
3943 return (EFI_DEVICE_ERROR
);
3946 Result
= MultU64x32(Result
, 10) + (*String
- L
'0');
3950 // Stop at spaces if requested
3952 if (StopAtSpace
&& *String
== L
' ') {
3959 return (EFI_SUCCESS
);
3963 Function to verify and convert a string to its numerical value.
3965 If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.
3967 @param[in] String The string to evaluate.
3968 @param[out] Value Upon a successful return the value of the conversion.
3969 @param[in] ForceHex TRUE - always assume hex.
3970 @param[in] StopAtSpace FALSE to skip spaces.
3972 @retval EFI_SUCCESS The conversion was successful.
3973 @retval EFI_INVALID_PARAMETER String contained an invalid character.
3974 @retval EFI_NOT_FOUND String was a number, but Value was NULL.
3978 ShellConvertStringToUint64(
3979 IN CONST CHAR16
*String
,
3981 IN CONST BOOLEAN ForceHex
,
3982 IN CONST BOOLEAN StopAtSpace
3986 CONST CHAR16
*Walker
;
3992 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3995 if (!InternalShellIsHexOrDecimalNumber(String
, Hex
, StopAtSpace
, FALSE
)) {
3996 return (EFI_INVALID_PARAMETER
);
3999 return (EFI_INVALID_PARAMETER
);
4004 // Chop off leading spaces
4006 for (Walker
= String
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
&& *Walker
== L
' '; Walker
++);
4009 // make sure we have something left that is numeric.
4011 if (Walker
== NULL
|| *Walker
== CHAR_NULL
|| !InternalShellIsHexOrDecimalNumber(Walker
, Hex
, StopAtSpace
, FALSE
)) {
4012 return (EFI_INVALID_PARAMETER
);
4016 // do the conversion.
4018 if (Hex
|| StrnCmp(Walker
, L
"0x", 2) == 0 || StrnCmp(Walker
, L
"0X", 2) == 0){
4019 Status
= InternalShellStrHexToUint64(Walker
, &RetVal
, StopAtSpace
);
4021 Status
= InternalShellStrDecimalToUint64(Walker
, &RetVal
, StopAtSpace
);
4024 if (Value
== NULL
&& !EFI_ERROR(Status
)) {
4025 return (EFI_NOT_FOUND
);
4028 if (Value
!= NULL
) {
4036 Function to determin if an entire string is a valid number.
4038 If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.
4040 @param[in] String The string to evaluate.
4041 @param[in] ForceHex TRUE - always assume hex.
4042 @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going.
4044 @retval TRUE It is all numeric (dec/hex) characters.
4045 @retval FALSE There is a non-numeric character.
4049 ShellIsHexOrDecimalNumber (
4050 IN CONST CHAR16
*String
,
4051 IN CONST BOOLEAN ForceHex
,
4052 IN CONST BOOLEAN StopAtSpace
4055 if (ShellConvertStringToUint64(String
, NULL
, ForceHex
, StopAtSpace
) == EFI_NOT_FOUND
) {
4062 Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned
4063 buffer. The returned buffer must be callee freed.
4065 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4066 maintained and not changed for all operations with the same file.
4068 @param[in] Handle SHELL_FILE_HANDLE to read from.
4069 @param[in, out] Ascii Boolean value for indicating whether the file is
4070 Ascii (TRUE) or UCS2 (FALSE).
4072 @return The line of text from the file.
4073 @retval NULL There was not enough memory available.
4075 @sa ShellFileHandleReadLine
4079 ShellFileHandleReturnLine(
4080 IN SHELL_FILE_HANDLE Handle
,
4081 IN OUT BOOLEAN
*Ascii
4091 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4092 if (Status
== EFI_BUFFER_TOO_SMALL
) {
4093 RetVal
= AllocateZeroPool(Size
);
4094 if (RetVal
== NULL
) {
4097 Status
= ShellFileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
4100 if (Status
== EFI_END_OF_FILE
&& RetVal
!= NULL
&& *RetVal
!= CHAR_NULL
) {
4101 Status
= EFI_SUCCESS
;
4103 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
4111 Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.
4113 If the position upon start is 0, then the Ascii Boolean will be set. This should be
4114 maintained and not changed for all operations with the same file.
4116 NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ
4119 @param[in] Handle SHELL_FILE_HANDLE to read from.
4120 @param[in, out] Buffer The pointer to buffer to read into. If this function
4121 returns EFI_SUCCESS, then on output Buffer will
4122 contain a UCS2 string, even if the file being
4124 @param[in, out] Size On input, pointer to number of bytes in Buffer.
4125 On output, unchanged unless Buffer is too small
4126 to contain the next line of the file. In that
4127 case Size is set to the number of bytes needed
4128 to hold the next line of the file (as a UCS2
4129 string, even if it is an ASCII file).
4130 @param[in] Truncate If the buffer is large enough, this has no effect.
4131 If the buffer is is too small and Truncate is TRUE,
4132 the line will be truncated.
4133 If the buffer is is too small and Truncate is FALSE,
4134 then no read will occur.
4136 @param[in, out] Ascii Boolean value for indicating whether the file is
4137 Ascii (TRUE) or UCS2 (FALSE).
4139 @retval EFI_SUCCESS The operation was successful. The line is stored in
4141 @retval EFI_END_OF_FILE There are no more lines in the file.
4142 @retval EFI_INVALID_PARAMETER Handle was NULL.
4143 @retval EFI_INVALID_PARAMETER Size was NULL.
4144 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
4145 Size was updated to the minimum space required.
4149 ShellFileHandleReadLine(
4150 IN SHELL_FILE_HANDLE Handle
,
4151 IN OUT CHAR16
*Buffer
,
4153 IN BOOLEAN Truncate
,
4154 IN OUT BOOLEAN
*Ascii
4161 UINT64 OriginalFilePosition
;
4167 return (EFI_INVALID_PARAMETER
);
4169 if (Buffer
== NULL
) {
4172 *Buffer
= CHAR_NULL
;
4174 gEfiShellProtocol
->GetFilePosition(Handle
, &OriginalFilePosition
);
4175 if (OriginalFilePosition
== 0) {
4176 CharSize
= sizeof(CHAR16
);
4177 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4178 ASSERT_EFI_ERROR(Status
);
4179 if (CharBuffer
== gUnicodeFileTag
) {
4183 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4188 CharSize
= sizeof(CHAR8
);
4190 CharSize
= sizeof(CHAR16
);
4192 for (CountSoFar
= 0;;CountSoFar
++){
4194 Status
= gEfiShellProtocol
->ReadFile(Handle
, &CharSize
, &CharBuffer
);
4195 if ( EFI_ERROR(Status
)
4197 || (CharBuffer
== L
'\n' && !(*Ascii
))
4198 || (CharBuffer
== '\n' && *Ascii
)
4200 if (CharSize
== 0) {
4201 Status
= EFI_END_OF_FILE
;
4206 // if we have space save it...
4208 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
4209 ASSERT(Buffer
!= NULL
);
4210 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
4211 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
4216 // if we ran out of space tell when...
4218 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
4219 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
4221 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
4223 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
4225 return (EFI_BUFFER_TOO_SMALL
);
4227 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
4228 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
4235 Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function.
4237 @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed.
4238 @param[in] SectionToGetHelpOn Pointer to the section specifier(s).
4239 @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints
4240 the help content only.
4241 @retval EFI_DEVICE_ERROR The help data format was incorrect.
4242 @retval EFI_NOT_FOUND The help data could not be found.
4243 @retval EFI_SUCCESS The operation was successful.
4248 IN CONST CHAR16
*CommandToGetHelpOn
,
4249 IN CONST CHAR16
*SectionToGetHelpOn
,
4250 IN BOOLEAN PrintCommandText
4259 // Get the string to print based
4261 Status
= gEfiShellProtocol
->GetHelpText (CommandToGetHelpOn
, SectionToGetHelpOn
, &OutText
);
4264 // make sure we got a valid string
4266 if (EFI_ERROR(Status
)){
4269 if (OutText
== NULL
|| StrLen(OutText
) == 0) {
4270 return EFI_NOT_FOUND
;
4274 // Chop off trailing stuff we dont need
4276 while (OutText
[StrLen(OutText
)-1] == L
'\r' || OutText
[StrLen(OutText
)-1] == L
'\n' || OutText
[StrLen(OutText
)-1] == L
' ') {
4277 OutText
[StrLen(OutText
)-1] = CHAR_NULL
;
4281 // Print this out to the console
4283 if (PrintCommandText
) {
4284 ShellPrintEx(-1, -1, L
"%H%-14s%N- %s\r\n", CommandToGetHelpOn
, OutText
);
4286 ShellPrintEx(-1, -1, L
"%N%s\r\n", OutText
);
4289 SHELL_FREE_NON_NULL(OutText
);
4295 Function to delete a file by name
4297 @param[in] FileName Pointer to file name to delete.
4299 @retval EFI_SUCCESS the file was deleted sucessfully
4300 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
4302 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
4303 @retval EFI_NOT_FOUND The specified file could not be found on the
4304 device or the file system could not be found
4306 @retval EFI_NO_MEDIA The device has no medium.
4307 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
4308 medium is no longer supported.
4309 @retval EFI_DEVICE_ERROR The device reported an error.
4310 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
4311 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
4312 @retval EFI_ACCESS_DENIED The file was opened read only.
4313 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
4315 @retval other The file failed to open
4319 ShellDeleteFileByName(
4320 IN CONST CHAR16
*FileName
4324 SHELL_FILE_HANDLE FileHandle
;
4326 Status
= ShellFileExists(FileName
);
4328 if (Status
== EFI_SUCCESS
){
4329 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0x0);
4330 if (Status
== EFI_SUCCESS
){
4331 Status
= ShellDeleteFile(&FileHandle
);
4340 Cleans off all the quotes in the string.
4342 @param[in] OriginalString pointer to the string to be cleaned.
4343 @param[out] CleanString The new string with all quotes removed.
4344 Memory allocated in the function and free
4347 @retval EFI_SUCCESS The operation was successful.
4350 InternalShellStripQuotes (
4351 IN CONST CHAR16
*OriginalString
,
4352 OUT CHAR16
**CleanString
4357 if (OriginalString
== NULL
|| CleanString
== NULL
) {
4358 return EFI_INVALID_PARAMETER
;
4361 *CleanString
= AllocateCopyPool (StrSize (OriginalString
), OriginalString
);
4362 if (*CleanString
== NULL
) {
4363 return EFI_OUT_OF_RESOURCES
;
4366 for (Walker
= *CleanString
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
4367 if (*Walker
== L
'\"') {
4368 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));