2 Provides interface to shell functionality for shell commands and applications.
4 Copyright (c) 2006 - 2009, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/ShellLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/DevicePathLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/FileHandleLib.h>
25 #include <Library/PrintLib.h>
26 #include <Library/UefiLib.h>
28 #include <Protocol/EfiShellEnvironment2.h>
29 #include <Protocol/EfiShellInterface.h>
30 #include <Protocol/EfiShell.h>
31 #include <Protocol/EfiShellParameters.h>
32 #include <Protocol/SimpleFileSystem.h>
34 #include "UefiShellLib.h"
36 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
37 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
40 // This is not static since it's extern in the .h file
42 SHELL_PARAM_ITEM EmptyParamList
[] = {
47 // Static file globals for the shell library
49 STATIC EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
50 STATIC EFI_SHELL_INTERFACE
*mEfiShellInterface
;
51 STATIC EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
52 STATIC EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
53 STATIC EFI_HANDLE mEfiShellEnvironment2Handle
;
54 STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
57 helper function to find ShellEnvironment2 for constructor
62 IN EFI_HANDLE ImageHandle
72 Status
= gBS
->OpenProtocol(ImageHandle
,
73 &gEfiShellEnvironment2Guid
,
74 (VOID
**)&mEfiShellEnvironment2
,
77 EFI_OPEN_PROTOCOL_GET_PROTOCOL
80 // look for the mEfiShellEnvironment2 protocol at a higher level
82 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
&&
83 (mEfiShellEnvironment2
->MajorVersion
> EFI_SHELL_MAJOR_VER
||
84 (mEfiShellEnvironment2
->MajorVersion
== EFI_SHELL_MAJOR_VER
&& mEfiShellEnvironment2
->MinorVersion
>= EFI_SHELL_MINOR_VER
)))) {
86 // figure out how big of a buffer we need.
88 Status
= gBS
->LocateHandle (ByProtocol
,
89 &gEfiShellEnvironment2Guid
,
90 NULL
, // ignored for ByProtocol
94 ASSERT(Status
== EFI_BUFFER_TOO_SMALL
);
95 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
96 ASSERT(Buffer
!= NULL
);
97 Status
= gBS
->LocateHandle (ByProtocol
,
98 &gEfiShellEnvironment2Guid
,
99 NULL
, // ignored for ByProtocol
103 if (!EFI_ERROR (Status
)) {
105 // now parse the list of returned handles
107 Status
= EFI_NOT_FOUND
;
108 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
109 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
110 &gEfiShellEnvironment2Guid
,
111 (VOID
**)&mEfiShellEnvironment2
,
114 EFI_OPEN_PROTOCOL_GET_PROTOCOL
116 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
&&
117 (mEfiShellEnvironment2
->MajorVersion
> EFI_SHELL_MAJOR_VER
||
118 (mEfiShellEnvironment2
->MajorVersion
== EFI_SHELL_MAJOR_VER
&& mEfiShellEnvironment2
->MinorVersion
>= EFI_SHELL_MINOR_VER
))) {
119 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
120 Status
= EFI_SUCCESS
;
126 if (Buffer
!= NULL
) {
134 ShellLibConstructorWorker (
135 IN EFI_HANDLE ImageHandle
,
136 IN EFI_SYSTEM_TABLE
*SystemTable
141 // UEFI 2.0 shell interfaces (used preferentially)
143 Status
= gBS
->OpenProtocol(ImageHandle
,
144 &gEfiShellProtocolGuid
,
145 (VOID
**)&mEfiShellProtocol
,
148 EFI_OPEN_PROTOCOL_GET_PROTOCOL
150 if (EFI_ERROR(Status
)) {
151 mEfiShellProtocol
= NULL
;
153 Status
= gBS
->OpenProtocol(ImageHandle
,
154 &gEfiShellParametersProtocolGuid
,
155 (VOID
**)&mEfiShellParametersProtocol
,
158 EFI_OPEN_PROTOCOL_GET_PROTOCOL
160 if (EFI_ERROR(Status
)) {
161 mEfiShellParametersProtocol
= NULL
;
164 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
166 // Moved to seperate function due to complexity
168 Status
= ShellFindSE2(ImageHandle
);
170 if (EFI_ERROR(Status
)) {
171 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
172 mEfiShellEnvironment2
= NULL
;
174 Status
= gBS
->OpenProtocol(ImageHandle
,
175 &gEfiShellInterfaceGuid
,
176 (VOID
**)&mEfiShellInterface
,
179 EFI_OPEN_PROTOCOL_GET_PROTOCOL
181 if (EFI_ERROR(Status
)) {
182 mEfiShellInterface
= NULL
;
186 // only success getting 2 of either the old or new, but no 1/2 and 1/2
188 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
189 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
190 if (mEfiShellProtocol
!= NULL
) {
191 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
192 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
193 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
194 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
195 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
196 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
197 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
198 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
199 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
200 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
202 FileFunctionMap
.GetFileInfo
= FileHandleGetInfo
;
203 FileFunctionMap
.SetFileInfo
= FileHandleSetInfo
;
204 FileFunctionMap
.ReadFile
= FileHandleRead
;
205 FileFunctionMap
.WriteFile
= FileHandleWrite
;
206 FileFunctionMap
.CloseFile
= FileHandleClose
;
207 FileFunctionMap
.DeleteFile
= FileHandleDelete
;
208 FileFunctionMap
.GetFilePosition
= FileHandleGetPosition
;
209 FileFunctionMap
.SetFilePosition
= FileHandleSetPosition
;
210 FileFunctionMap
.FlushFile
= FileHandleFlush
;
211 FileFunctionMap
.GetFileSize
= FileHandleGetSize
;
213 return (EFI_SUCCESS
);
215 return (EFI_NOT_FOUND
);
218 Constructor for the Shell library.
220 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
222 @param ImageHandle the image handle of the process
223 @param SystemTable the EFI System Table pointer
225 @retval EFI_SUCCESS the initialization was complete sucessfully
226 @return others an error ocurred during initialization
230 ShellLibConstructor (
231 IN EFI_HANDLE ImageHandle
,
232 IN EFI_SYSTEM_TABLE
*SystemTable
237 mEfiShellEnvironment2
= NULL
;
238 mEfiShellProtocol
= NULL
;
239 mEfiShellParametersProtocol
= NULL
;
240 mEfiShellInterface
= NULL
;
241 mEfiShellEnvironment2Handle
= NULL
;
244 // verify that auto initialize is not set false
246 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
247 return (EFI_SUCCESS
);
250 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
254 Destructory for the library. free any resources.
259 IN EFI_HANDLE ImageHandle
,
260 IN EFI_SYSTEM_TABLE
*SystemTable
263 if (mEfiShellEnvironment2
!= NULL
) {
264 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
265 &gEfiShellEnvironment2Guid
,
268 mEfiShellEnvironment2
= NULL
;
270 if (mEfiShellInterface
!= NULL
) {
271 gBS
->CloseProtocol(ImageHandle
,
272 &gEfiShellInterfaceGuid
,
275 mEfiShellInterface
= NULL
;
277 if (mEfiShellProtocol
!= NULL
) {
278 gBS
->CloseProtocol(ImageHandle
,
279 &gEfiShellProtocolGuid
,
282 mEfiShellProtocol
= NULL
;
284 if (mEfiShellParametersProtocol
!= NULL
) {
285 gBS
->CloseProtocol(ImageHandle
,
286 &gEfiShellParametersProtocolGuid
,
289 mEfiShellParametersProtocol
= NULL
;
291 mEfiShellEnvironment2Handle
= NULL
;
292 return (EFI_SUCCESS
);
296 This function causes the shell library to initialize itself. If the shell library
297 is already initialized it will de-initialize all the current protocol poitners and
298 re-populate them again.
300 When the library is used with PcdShellLibAutoInitialize set to true this function
301 will return EFI_SUCCESS and perform no actions.
303 This function is intended for internal access for shell commands only.
305 @retval EFI_SUCCESS the initialization was complete sucessfully
313 // if auto initialize is not false then skip
315 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
316 return (EFI_SUCCESS
);
320 // deinit the current stuff
322 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
325 // init the new stuff
327 return (ShellLibConstructorWorker(gImageHandle
, gST
));
331 This function will retrieve the information about the file for the handle
332 specified and store it in allocated pool memory.
334 This function allocates a buffer to store the file's information. It is the
335 caller's responsibility to free the buffer
337 @param FileHandle The file handle of the file for which information is
340 @retval NULL information could not be retrieved.
342 @return the information about the file
347 IN EFI_FILE_HANDLE FileHandle
350 return (FileFunctionMap
.GetFileInfo(FileHandle
));
354 This function will set the information about the file for the opened handle
357 @param FileHandle The file handle of the file for which information
360 @param FileInfo The infotmation to set.
362 @retval EFI_SUCCESS The information was set.
363 @retval EFI_UNSUPPORTED The InformationType is not known.
364 @retval EFI_NO_MEDIA The device has no medium.
365 @retval EFI_DEVICE_ERROR The device reported an error.
366 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
367 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
368 @retval EFI_ACCESS_DENIED The file was opened read only.
369 @retval EFI_VOLUME_FULL The volume is full.
374 IN EFI_FILE_HANDLE FileHandle
,
375 IN EFI_FILE_INFO
*FileInfo
378 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
382 This function will open a file or directory referenced by DevicePath.
384 This function opens a file with the open mode according to the file path. The
385 Attributes is valid only for EFI_FILE_MODE_CREATE.
387 @param FilePath on input the device path to the file. On output
388 the remaining device path.
389 @param DeviceHandle pointer to the system device handle.
390 @param FileHandle pointer to the file handle.
391 @param OpenMode the mode to open the file with.
392 @param Attributes the file's file attributes.
394 @retval EFI_SUCCESS The information was set.
395 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
396 @retval EFI_UNSUPPORTED Could not open the file path.
397 @retval EFI_NOT_FOUND The specified file could not be found on the
398 device or the file system could not be found on
400 @retval EFI_NO_MEDIA The device has no medium.
401 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
402 medium is no longer supported.
403 @retval EFI_DEVICE_ERROR The device reported an error.
404 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
405 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
406 @retval EFI_ACCESS_DENIED The file was opened read only.
407 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
409 @retval EFI_VOLUME_FULL The volume is full.
413 ShellOpenFileByDevicePath(
414 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
415 OUT EFI_HANDLE
*DeviceHandle
,
416 OUT EFI_FILE_HANDLE
*FileHandle
,
423 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
424 EFI_FILE_HANDLE LastHandle
;
427 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
429 ASSERT(FilePath
!= NULL
);
430 ASSERT(FileHandle
!= NULL
);
431 ASSERT(DeviceHandle
!= NULL
);
433 // which shell interface should we use
435 if (mEfiShellProtocol
!= NULL
) {
437 // use UEFI Shell 2.0 method.
439 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
440 if (FileName
== NULL
) {
441 return (EFI_INVALID_PARAMETER
);
443 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
450 // use old shell method.
452 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
455 if (EFI_ERROR (Status
)) {
458 Status
= gBS
->OpenProtocol(*DeviceHandle
,
459 &gEfiSimpleFileSystemProtocolGuid
,
460 (VOID
**)&EfiSimpleFileSystemProtocol
,
463 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
464 if (EFI_ERROR (Status
)) {
467 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
468 if (EFI_ERROR (Status
)) {
474 // go down directories one node at a time.
476 while (!IsDevicePathEnd (*FilePath
)) {
478 // For file system access each node should be a file path component
480 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
481 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
484 return (EFI_INVALID_PARAMETER
);
487 // Open this file path node
489 LastHandle
= *FileHandle
;
493 // Try to test opening an existing file
495 Status
= LastHandle
->Open (
498 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
499 OpenMode
&~EFI_FILE_MODE_CREATE
,
504 // see if the error was that it needs to be created
506 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
507 Status
= LastHandle
->Open (
510 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
516 // Close the last node
518 LastHandle
->Close (LastHandle
);
520 if (EFI_ERROR(Status
)) {
527 *FilePath
= NextDevicePathNode (*FilePath
);
529 return (EFI_SUCCESS
);
533 This function will open a file or directory referenced by filename.
535 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
536 otherwise, the Filehandle is NULL. The Attributes is valid only for
537 EFI_FILE_MODE_CREATE.
539 if FileNAme is NULL then ASSERT()
541 @param FileName pointer to file name
542 @param FileHandle pointer to the file handle.
543 @param OpenMode the mode to open the file with.
544 @param Attributes the file's file attributes.
546 @retval EFI_SUCCESS The information was set.
547 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
548 @retval EFI_UNSUPPORTED Could not open the file path.
549 @retval EFI_NOT_FOUND The specified file could not be found on the
550 device or the file system could not be found
552 @retval EFI_NO_MEDIA The device has no medium.
553 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
554 medium is no longer supported.
555 @retval EFI_DEVICE_ERROR The device reported an error.
556 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
557 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
558 @retval EFI_ACCESS_DENIED The file was opened read only.
559 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
561 @retval EFI_VOLUME_FULL The volume is full.
567 OUT EFI_FILE_HANDLE
*FileHandle
,
572 EFI_HANDLE DeviceHandle
;
573 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
575 EFI_FILE_INFO
*FileInfo
;
578 // ASSERT if FileName is NULL
580 ASSERT(FileName
!= NULL
);
582 if (mEfiShellProtocol
!= NULL
) {
584 // Use UEFI Shell 2.0 method
586 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
589 if (!EFI_ERROR(Status
)){
590 FileInfo
= FileHandleGetInfo(*FileHandle
);
591 ASSERT(FileInfo
!= NULL
);
592 FileInfo
->Attribute
= Attributes
;
593 Status
= FileHandleSetInfo(*FileHandle
, FileInfo
);
598 // Using EFI Shell version
599 // this means convert name to path and call that function
600 // since this will use EFI method again that will open it.
602 ASSERT(mEfiShellEnvironment2
!= NULL
);
603 FilePath
= mEfiShellEnvironment2
->NameToPath (FileName
);
604 if (FileDevicePath
!= NULL
) {
605 return (ShellOpenFileByDevicePath(&FilePath
,
611 return (EFI_DEVICE_ERROR
);
614 This function create a directory
616 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
617 otherwise, the Filehandle is NULL. If the directory already existed, this
618 function opens the existing directory.
620 @param DirectoryName pointer to directory name
621 @param FileHandle pointer to the file handle.
623 @retval EFI_SUCCESS The information was set.
624 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
625 @retval EFI_UNSUPPORTED Could not open the file path.
626 @retval EFI_NOT_FOUND The specified file could not be found on the
627 device or the file system could not be found
629 @retval EFI_NO_MEDIA The device has no medium.
630 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
631 medium is no longer supported.
632 @retval EFI_DEVICE_ERROR The device reported an error.
633 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
634 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
635 @retval EFI_ACCESS_DENIED The file was opened read only.
636 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
638 @retval EFI_VOLUME_FULL The volume is full.
639 @sa ShellOpenFileByName
643 ShellCreateDirectory(
644 IN CHAR16
*DirectoryName
,
645 OUT EFI_FILE_HANDLE
*FileHandle
649 // this is a pass thru to the open file function with sepcific open mode and attributes
651 return (ShellOpenFileByName(DirectoryName
,
653 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
659 This function reads information from an opened file.
661 If FileHandle is not a directory, the function reads the requested number of
662 bytes from the file at the file's current position and returns them in Buffer.
663 If the read goes beyond the end of the file, the read length is truncated to the
664 end of the file. The file's current position is increased by the number of bytes
665 returned. If FileHandle is a directory, the function reads the directory entry
666 at the file's current position and returns the entry in Buffer. If the Buffer
667 is not large enough to hold the current directory entry, then
668 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
669 BufferSize is set to be the size of the buffer needed to read the entry. On
670 success, the current position is updated to the next directory entry. If there
671 are no more directory entries, the read returns a zero-length buffer.
672 EFI_FILE_INFO is the structure returned as the directory entry.
674 @param FileHandle the opened file handle
675 @param BufferSize on input the size of buffer in bytes. on return
676 the number of bytes written.
677 @param Buffer the buffer to put read data into.
679 @retval EFI_SUCCESS Data was read.
680 @retval EFI_NO_MEDIA The device has no media.
681 @retval EFI_DEVICE_ERROR The device reported an error.
682 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
683 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
690 IN EFI_FILE_HANDLE FileHandle
,
691 IN OUT UINTN
*BufferSize
,
695 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
700 Write data to a file.
702 This function writes the specified number of bytes to the file at the current
703 file position. The current file position is advanced the actual number of bytes
704 written, which is returned in BufferSize. Partial writes only occur when there
705 has been a data error during the write attempt (such as "volume space full").
706 The file is automatically grown to hold the data if required. Direct writes to
707 opened directories are not supported.
709 @param FileHandle The opened file for writing
710 @param BufferSize on input the number of bytes in Buffer. On output
711 the number of bytes written.
712 @param Buffer the buffer containing data to write is stored.
714 @retval EFI_SUCCESS Data was written.
715 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
716 @retval EFI_NO_MEDIA The device has no media.
717 @retval EFI_DEVICE_ERROR The device reported an error.
718 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
719 @retval EFI_WRITE_PROTECTED The device is write-protected.
720 @retval EFI_ACCESS_DENIED The file was open for read only.
721 @retval EFI_VOLUME_FULL The volume is full.
726 IN EFI_FILE_HANDLE FileHandle
,
727 IN OUT UINTN
*BufferSize
,
731 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
735 Close an open file handle.
737 This function closes a specified file handle. All "dirty" cached file data is
738 flushed to the device, and the file is closed. In all cases the handle is
741 @param FileHandle the file handle to close.
743 @retval EFI_SUCCESS the file handle was closed sucessfully.
748 IN EFI_FILE_HANDLE
*FileHandle
751 return (FileFunctionMap
.CloseFile(*FileHandle
));
755 Delete a file and close the handle
757 This function closes and deletes a file. In all cases the file handle is closed.
758 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
759 returned, but the handle is still closed.
761 @param FileHandle the file handle to delete
763 @retval EFI_SUCCESS the file was closed sucessfully
764 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
766 @retval INVALID_PARAMETER One of the parameters has an invalid value.
771 IN EFI_FILE_HANDLE
*FileHandle
774 return (FileFunctionMap
.DeleteFile(*FileHandle
));
778 Set the current position in a file.
780 This function sets the current file position for the handle to the position
781 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
782 absolute positioning is supported, and seeking past the end of the file is
783 allowed (a subsequent write would grow the file). Seeking to position
784 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
785 If FileHandle is a directory, the only position that may be set is zero. This
786 has the effect of starting the read process of the directory entries over.
788 @param FileHandle The file handle on which the position is being set
789 @param Position Byte position from begining of file
791 @retval EFI_SUCCESS Operation completed sucessfully.
792 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
794 @retval INVALID_PARAMETER One of the parameters has an invalid value.
798 ShellSetFilePosition (
799 IN EFI_FILE_HANDLE FileHandle
,
803 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
807 Gets a file's current position
809 This function retrieves the current file position for the file handle. For
810 directories, the current file position has no meaning outside of the file
811 system driver and as such the operation is not supported. An error is returned
812 if FileHandle is a directory.
814 @param FileHandle The open file handle on which to get the position.
815 @param Position Byte position from begining of file.
817 @retval EFI_SUCCESS the operation completed sucessfully.
818 @retval INVALID_PARAMETER One of the parameters has an invalid value.
819 @retval EFI_UNSUPPORTED the request is not valid on directories.
823 ShellGetFilePosition (
824 IN EFI_FILE_HANDLE FileHandle
,
828 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
831 Flushes data on a file
833 This function flushes all modified data associated with a file to a device.
835 @param FileHandle The file handle on which to flush data
837 @retval EFI_SUCCESS The data was flushed.
838 @retval EFI_NO_MEDIA The device has no media.
839 @retval EFI_DEVICE_ERROR The device reported an error.
840 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
841 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
842 @retval EFI_ACCESS_DENIED The file was opened for read only.
847 IN EFI_FILE_HANDLE FileHandle
850 return (FileFunctionMap
.FlushFile(FileHandle
));
854 Retrieves the first file from a directory
856 This function opens a directory and gets the first file's info in the
857 directory. Caller can use ShellFindNextFile() to get other files. When
858 complete the caller is responsible for calling FreePool() on Buffer.
860 @param DirHandle The file handle of the directory to search
861 @param Buffer Pointer to buffer for file's information
863 @retval EFI_SUCCESS Found the first file.
864 @retval EFI_NOT_FOUND Cannot find the directory.
865 @retval EFI_NO_MEDIA The device has no media.
866 @retval EFI_DEVICE_ERROR The device reported an error.
867 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
868 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
874 IN EFI_FILE_HANDLE DirHandle
,
875 OUT EFI_FILE_INFO
**Buffer
879 // pass to file handle lib
881 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
884 Retrieves the next file in a directory.
886 To use this function, caller must call the LibFindFirstFile() to get the
887 first file, and then use this function get other files. This function can be
888 called for several times to get each file's information in the directory. If
889 the call of ShellFindNextFile() got the last file in the directory, the next
890 call of this function has no file to get. *NoFile will be set to TRUE and the
891 Buffer memory will be automatically freed.
893 @param DirHandle the file handle of the directory
894 @param Buffer pointer to buffer for file's information
895 @param NoFile pointer to boolean when last file is found
897 @retval EFI_SUCCESS Found the next file, or reached last file
898 @retval EFI_NO_MEDIA The device has no media.
899 @retval EFI_DEVICE_ERROR The device reported an error.
900 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
905 IN EFI_FILE_HANDLE DirHandle
,
906 OUT EFI_FILE_INFO
*Buffer
,
911 // pass to file handle lib
913 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
916 Retrieve the size of a file.
918 if FileHandle is NULL then ASSERT()
919 if Size is NULL then ASSERT()
921 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
924 @param FileHandle file handle from which size is retrieved
925 @param Size pointer to size
927 @retval EFI_SUCCESS operation was completed sucessfully
928 @retval EFI_DEVICE_ERROR cannot access the file
933 IN EFI_FILE_HANDLE FileHandle
,
937 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
940 Retrieves the status of the break execution flag
942 this function is useful to check whether the application is being asked to halt by the shell.
944 @retval TRUE the execution break is enabled
945 @retval FALSE the execution break is not enabled
949 ShellGetExecutionBreakFlag(
954 // Check for UEFI Shell 2.0 protocols
956 if (mEfiShellProtocol
!= NULL
) {
959 // We are using UEFI Shell 2.0; see if the event has been triggered
961 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
968 // using EFI Shell; call the function to check
970 ASSERT(mEfiShellEnvironment2
!= NULL
);
971 return (mEfiShellEnvironment2
->GetExecutionBreak());
974 return the value of an environment variable
976 this function gets the value of the environment variable set by the
977 ShellSetEnvironmentVariable function
979 @param EnvKey The key name of the environment variable.
981 @retval NULL the named environment variable does not exist.
982 @return != NULL pointer to the value of the environment variable
986 ShellGetEnvironmentVariable (
991 // Check for UEFI Shell 2.0 protocols
993 if (mEfiShellProtocol
!= NULL
) {
994 return (mEfiShellProtocol
->GetEnv(EnvKey
));
998 // ASSERT that we must have EFI shell
1000 ASSERT(mEfiShellEnvironment2
!= NULL
);
1005 return (mEfiShellEnvironment2
->GetEnv(EnvKey
));
1008 set the value of an environment variable
1010 This function changes the current value of the specified environment variable. If the
1011 environment variable exists and the Value is an empty string, then the environment
1012 variable is deleted. If the environment variable exists and the Value is not an empty
1013 string, then the value of the environment variable is changed. If the environment
1014 variable does not exist and the Value is an empty string, there is no action. If the
1015 environment variable does not exist and the Value is a non-empty string, then the
1016 environment variable is created and assigned the specified value.
1018 This is not supported pre-UEFI Shell 2.0.
1020 @param EnvKey The key name of the environment variable.
1021 @param EnvVal The Value of the environment variable
1022 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1024 @retval EFI_SUCCESS the operation was completed sucessfully
1025 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1029 ShellSetEnvironmentVariable (
1030 IN CONST CHAR16
*EnvKey
,
1031 IN CONST CHAR16
*EnvVal
,
1036 // Check for UEFI Shell 2.0 protocols
1038 if (mEfiShellProtocol
!= NULL
) {
1039 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1043 // This feature does not exist under EFI shell
1045 return (EFI_UNSUPPORTED
);
1048 cause the shell to parse and execute a command line.
1050 This function creates a nested instance of the shell and executes the specified
1051 command (CommandLine) with the specified environment (Environment). Upon return,
1052 the status code returned by the specified command is placed in StatusCode.
1053 If Environment is NULL, then the current environment is used and all changes made
1054 by the commands executed will be reflected in the current environment. If the
1055 Environment is non-NULL, then the changes made will be discarded.
1056 The CommandLine is executed from the current working directory on the current
1059 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1060 Output is only supported for pre-UEFI Shell 2.0
1062 @param ImageHandle Parent image that is starting the operation
1063 @param CommandLine pointer to null terminated command line.
1064 @param Output true to display debug output. false to hide it.
1065 @param EnvironmentVariables optional pointer to array of environment variables
1066 in the form "x=y". if NULL current set is used.
1067 @param Status the status of the run command line.
1069 @retval EFI_SUCCESS the operation completed sucessfully. Status
1070 contains the status code returned.
1071 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1072 @retval EFI_OUT_OF_RESOURCES out of resources
1073 @retval EFI_UNSUPPORTED the operation is not allowed.
1078 IN EFI_HANDLE
*ParentHandle
,
1079 IN CHAR16
*CommandLine OPTIONAL
,
1080 IN BOOLEAN Output OPTIONAL
,
1081 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1082 OUT EFI_STATUS
*Status OPTIONAL
1086 // Check for UEFI Shell 2.0 protocols
1088 if (mEfiShellProtocol
!= NULL
) {
1090 // Call UEFI Shell 2.0 version (not using Output parameter)
1092 return (mEfiShellProtocol
->Execute(ParentHandle
,
1094 EnvironmentVariables
,
1098 // ASSERT that we must have EFI shell
1100 ASSERT(mEfiShellEnvironment2
!= NULL
);
1102 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1103 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1105 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1110 Retreives the current directory path
1112 If the DeviceName is NULL, it returns the current device's current directory
1113 name. If the DeviceName is not NULL, it returns the current directory name
1116 @param DeviceName the name of the drive to get directory on
1118 @retval NULL the directory does not exist
1119 @return != NULL the directory
1123 ShellGetCurrentDir (
1124 IN CHAR16
*DeviceName OPTIONAL
1128 // Check for UEFI Shell 2.0 protocols
1130 if (mEfiShellProtocol
!= NULL
) {
1131 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1134 // ASSERT that we must have EFI shell
1136 ASSERT(mEfiShellEnvironment2
!= NULL
);
1137 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1140 sets (enabled or disabled) the page break mode
1142 when page break mode is enabled the screen will stop scrolling
1143 and wait for operator input before scrolling a subsequent screen.
1145 @param CurrentState TRUE to enable and FALSE to disable
1149 ShellSetPageBreakMode (
1150 IN BOOLEAN CurrentState
1154 // check for enabling
1156 if (CurrentState
!= 0x00) {
1158 // check for UEFI Shell 2.0
1160 if (mEfiShellProtocol
!= NULL
) {
1162 // Enable with UEFI 2.0 Shell
1164 mEfiShellProtocol
->EnablePageBreak();
1168 // ASSERT that must have EFI Shell
1170 ASSERT(mEfiShellEnvironment2
!= NULL
);
1172 // Enable with EFI Shell
1174 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1179 // check for UEFI Shell 2.0
1181 if (mEfiShellProtocol
!= NULL
) {
1183 // Disable with UEFI 2.0 Shell
1185 mEfiShellProtocol
->DisablePageBreak();
1189 // ASSERT that must have EFI Shell
1191 ASSERT(mEfiShellEnvironment2
!= NULL
);
1193 // Disable with EFI Shell
1195 mEfiShellEnvironment2
->DisablePageBreak ();
1202 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1203 /// This allows for the struct to be populated.
1210 EFI_FILE_HANDLE Handle
;
1211 EFI_FILE_INFO
*Info
;
1212 } EFI_SHELL_FILE_INFO_NO_CONST
;
1215 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1217 if OldStyleFileList is NULL then ASSERT()
1219 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1220 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1221 the ShellCloseFileMetaArg function.
1223 @param FileList the EFI shell list type
1225 @retval the resultant head of the double linked new format list;
1229 InternalShellConvertFileListType (
1230 LIST_ENTRY
*FileList
1233 LIST_ENTRY
*ListHead
;
1234 SHELL_FILE_ARG
*OldInfo
;
1236 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1239 // ASSERT that FileList is not NULL
1241 ASSERT(FileList
!= NULL
);
1244 // Allocate our list head and initialize the list
1246 ListHead
= AllocateZeroPool(sizeof(LIST_ENTRY
));
1247 ASSERT (ListHead
!= NULL
);
1248 ListHead
= InitializeListHead (ListHead
);
1251 // enumerate through each member of the old list and copy
1253 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1254 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1257 // make sure the old list was valid
1259 ASSERT(OldInfo
!= NULL
);
1260 ASSERT(OldInfo
->Info
!= NULL
);
1261 ASSERT(OldInfo
->FullName
!= NULL
);
1262 ASSERT(OldInfo
->FileName
!= NULL
);
1265 // allocate a new EFI_SHELL_FILE_INFO object
1267 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1270 // copy the simple items
1272 NewInfo
->Handle
= OldInfo
->Handle
;
1273 NewInfo
->Status
= OldInfo
->Status
;
1275 // old shell checks for 0 not NULL
1276 OldInfo
->Handle
= 0;
1279 // allocate new space to copy strings and structure
1281 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1282 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1283 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1286 // make sure all the memory allocations were sucessful
1288 ASSERT(NewInfo
->FullName
!= NULL
);
1289 ASSERT(NewInfo
->FileName
!= NULL
);
1290 ASSERT(NewInfo
->Info
!= NULL
);
1293 // Copt the strings and structure
1295 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1296 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1297 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1300 // add that to the list
1302 InsertTailList(ListHead
, (LIST_ENTRY
*)NewInfo
);
1307 Opens a group of files based on a path.
1309 This function uses the Arg to open all the matching files. Each matched
1310 file has a SHELL_FILE_ARG structure to record the file information. These
1311 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1312 structures from ListHead to access each file. This function supports wildcards
1313 and will process '?' and '*' as such. the list must be freed with a call to
1314 ShellCloseFileMetaArg().
1316 This function will fail if called sequentially without freeing the list in the middle.
1318 @param Arg pointer to path string
1319 @param OpenMode mode to open files with
1320 @param ListHead head of linked list of results
1322 @retval EFI_SUCCESS the operation was sucessful and the list head
1323 contains the list of opened files
1324 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1325 *ListHead is set to NULL.
1326 @return != EFI_SUCCESS the operation failed
1328 @sa InternalShellConvertFileListType
1332 ShellOpenFileMetaArg (
1335 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1339 LIST_ENTRY
*EmptyNode
;
1340 LIST_ENTRY
*mOldStyleFileList
;
1343 // ASSERT that Arg and ListHead are not NULL
1345 ASSERT(Arg
!= NULL
);
1346 ASSERT(ListHead
!= NULL
);
1349 // Check for UEFI Shell 2.0 protocols
1351 if (mEfiShellProtocol
!= NULL
) {
1352 return (mEfiShellProtocol
->OpenFileList(Arg
,
1358 // ASSERT that we must have EFI shell
1360 ASSERT(mEfiShellEnvironment2
!= NULL
);
1363 // allocate memory for old list head
1365 mOldStyleFileList
= (LIST_ENTRY
*)AllocatePool(sizeof(LIST_ENTRY
));
1366 ASSERT(mOldStyleFileList
!= NULL
);
1369 // make sure the list head is initialized
1371 InitializeListHead((LIST_ENTRY
*)mOldStyleFileList
);
1374 // Get the EFI Shell list of files
1376 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, mOldStyleFileList
);
1377 if (EFI_ERROR(Status
)) {
1383 // Convert that to equivalent of UEFI Shell 2.0 structure
1385 EmptyNode
= InternalShellConvertFileListType(mOldStyleFileList
);
1388 // Free the EFI Shell version that was converted.
1390 ASSERT_EFI_ERROR(mEfiShellEnvironment2
->FreeFileList(mOldStyleFileList
));
1391 FreePool(mOldStyleFileList
);
1392 mOldStyleFileList
= NULL
;
1395 // remove the empty head of the list
1397 *ListHead
= (EFI_SHELL_FILE_INFO
*)RemoveEntryList(EmptyNode
);
1398 FreePool(EmptyNode
);
1403 Free the linked list returned from ShellOpenFileMetaArg
1405 if ListHead is NULL then ASSERT()
1407 @param ListHead the pointer to free
1409 @retval EFI_SUCCESS the operation was sucessful
1413 ShellCloseFileMetaArg (
1414 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1420 // ASSERT that ListHead is not NULL
1422 ASSERT(ListHead
!= NULL
);
1425 // Check for UEFI Shell 2.0 protocols
1427 if (mEfiShellProtocol
!= NULL
) {
1428 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1431 // Since this is EFI Shell version we need to free our internally made copy
1434 for (Node
= GetFirstNode((LIST_ENTRY
*)*ListHead
) ; IsListEmpty((LIST_ENTRY
*)*ListHead
) == FALSE
; Node
= GetFirstNode((LIST_ENTRY
*)*ListHead
)) {
1435 RemoveEntryList(Node
);
1436 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1437 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1438 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1439 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1440 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1451 UINTN OriginalPosition
;
1452 } SHELL_PARAM_PACKAGE
;
1455 Checks the list of valid arguments and returns TRUE if the item was found. If the
1456 return value is TRUE then the type parameter is set also.
1458 if CheckList is NULL then ASSERT();
1459 if Name is NULL then ASSERT();
1460 if Type is NULL then ASSERT();
1462 @param Type pointer to type of parameter if it was found
1463 @param Name pointer to Name of parameter found
1464 @param CheckList List to check against
1466 @retval TRUE the Parameter was found. Type is valid.
1467 @retval FALSE the Parameter was not found. Type is not valid.
1471 InternalIsOnCheckList (
1472 IN CONST CHAR16
*Name
,
1473 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1477 SHELL_PARAM_ITEM
*TempListItem
;
1480 // ASSERT that all 3 pointer parameters aren't NULL
1482 ASSERT(CheckList
!= NULL
);
1483 ASSERT(Type
!= NULL
);
1484 ASSERT(Name
!= NULL
);
1487 // question mark and page break mode are always supported
1489 if ((StrCmp(Name
, L
"-?") == 0) ||
1490 (StrCmp(Name
, L
"-b") == 0)
1496 // Enumerate through the list
1498 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1500 // If the Name matches set the type and return TRUE
1502 if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1503 *Type
= TempListItem
->Type
;
1510 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1512 @param Name pointer to Name of parameter found
1514 @retval TRUE the Parameter is a flag.
1515 @retval FALSE the Parameter not a flag
1520 IN CONST CHAR16
*Name
1524 // ASSERT that Name isn't NULL
1526 ASSERT(Name
!= NULL
);
1529 // If the Name has a / or - as the first character return TRUE
1531 if ((Name
[0] == L
'/') ||
1532 (Name
[0] == L
'-') ||
1541 Checks the command line arguments passed against the list of valid ones.
1543 If no initialization is required, then return RETURN_SUCCESS.
1545 @param CheckList pointer to list of parameters to check
1546 @param CheckPackage pointer to pointer to list checked values
1547 @param ProblemParam optional pointer to pointer to unicode string for
1548 the paramater that caused failure. If used then the
1549 caller is responsible for freeing the memory.
1550 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1551 @param Argc Count of parameters in Argv
1552 @param Argv pointer to array of parameters
1554 @retval EFI_SUCCESS The operation completed sucessfully.
1555 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1556 @retval EFI_INVALID_PARAMETER A parameter was invalid
1557 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1558 duplicated. the duplicated command line argument
1559 was returned in ProblemParam if provided.
1560 @retval EFI_NOT_FOUND a argument required a value that was missing.
1561 the invalid command line argument was returned in
1562 ProblemParam if provided.
1566 InternalCommandLineParse (
1567 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1568 OUT LIST_ENTRY
**CheckPackage
,
1569 OUT CHAR16
**ProblemParam OPTIONAL
,
1570 IN BOOLEAN AutoPageBreak
,
1571 IN CONST CHAR16
**Argv
,
1577 ParamType CurrentItemType
;
1578 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1579 BOOLEAN GetItemValue
;
1581 CurrentItemPackage
= NULL
;
1586 ASSERT(CheckList
!= NULL
);
1587 ASSERT(Argv
!= NULL
);
1590 GetItemValue
= FALSE
;
1593 // If there is only 1 item we dont need to do anything
1596 *CheckPackage
= NULL
;
1597 return (EFI_SUCCESS
);
1601 // initialize the linked list
1603 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1604 InitializeListHead(*CheckPackage
);
1607 // loop through each of the arguments
1609 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1610 if (Argv
[LoopCounter
] == NULL
) {
1612 // do nothing for NULL argv
1614 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) == TRUE
) {
1618 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1619 ASSERT(CurrentItemPackage
!= NULL
);
1620 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1621 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1622 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1623 CurrentItemPackage
->Type
= CurrentItemType
;
1624 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1625 CurrentItemPackage
->Value
= NULL
;
1628 // Does this flag require a value
1630 if (CurrentItemPackage
->Type
== TypeValue
) {
1632 // trigger the next loop to populate the value of this item
1634 GetItemValue
= TRUE
;
1637 // this item has no value expected; we are done
1639 InsertHeadList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1641 } else if (GetItemValue
== TRUE
&& InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1642 ASSERT(CurrentItemPackage
!= NULL
);
1644 // get the item VALUE for the previous flag
1646 GetItemValue
= FALSE
;
1647 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1648 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1649 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1650 InsertHeadList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1651 } else if (InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1653 // add this one as a non-flag
1655 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1656 ASSERT(CurrentItemPackage
!= NULL
);
1657 CurrentItemPackage
->Name
= NULL
;
1658 CurrentItemPackage
->Type
= TypePosition
;
1659 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1660 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1661 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1662 CurrentItemPackage
->OriginalPosition
= Count
++;
1663 InsertHeadList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1664 } else if (ProblemParam
) {
1666 // this was a non-recognised flag... error!
1668 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1669 ASSERT(*ProblemParam
!= NULL
);
1670 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1671 ShellCommandLineFreeVarList(*CheckPackage
);
1672 *CheckPackage
= NULL
;
1673 return (EFI_VOLUME_CORRUPTED
);
1675 ShellCommandLineFreeVarList(*CheckPackage
);
1676 *CheckPackage
= NULL
;
1677 return (EFI_VOLUME_CORRUPTED
);
1681 // support for AutoPageBreak
1683 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1684 ShellSetPageBreakMode(TRUE
);
1686 return (EFI_SUCCESS
);
1690 Checks the command line arguments passed against the list of valid ones.
1691 Optionally removes NULL values first.
1693 If no initialization is required, then return RETURN_SUCCESS.
1695 @param CheckList pointer to list of parameters to check
1696 @param CheckPackage pointer to pointer to list checked values
1697 @param ProblemParam optional pointer to pointer to unicode string for
1698 the paramater that caused failure.
1699 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1701 @retval EFI_SUCCESS The operation completed sucessfully.
1702 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1703 @retval EFI_INVALID_PARAMETER A parameter was invalid
1704 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1705 duplicated. the duplicated command line argument
1706 was returned in ProblemParam if provided.
1707 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1708 of the command line arguments was returned in
1709 ProblemParam if provided.
1710 @retval EFI_NOT_FOUND a argument required a value that was missing.
1711 the invalid command line argument was returned in
1712 ProblemParam if provided.
1716 ShellCommandLineParse (
1717 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1718 OUT LIST_ENTRY
**CheckPackage
,
1719 OUT CHAR16
**ProblemParam OPTIONAL
,
1720 IN BOOLEAN AutoPageBreak
1724 // ASSERT that CheckList and CheckPackage aren't NULL
1726 ASSERT(CheckList
!= NULL
);
1727 ASSERT(CheckPackage
!= NULL
);
1730 // Check for UEFI Shell 2.0 protocols
1732 if (mEfiShellParametersProtocol
!= NULL
) {
1733 return (InternalCommandLineParse(CheckList
,
1737 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
1738 mEfiShellParametersProtocol
->Argc
));
1742 // ASSERT That EFI Shell is not required
1744 ASSERT (mEfiShellInterface
!= NULL
);
1745 return (InternalCommandLineParse(CheckList
,
1749 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
1750 mEfiShellInterface
->Argc
));
1754 Frees shell variable list that was returned from ShellCommandLineParse.
1756 This function will free all the memory that was used for the CheckPackage
1757 list of postprocessed shell arguments.
1759 this function has no return value.
1761 if CheckPackage is NULL, then return
1763 @param CheckPackage the list to de-allocate
1767 ShellCommandLineFreeVarList (
1768 IN LIST_ENTRY
*CheckPackage
1774 // check for CheckPackage == NULL
1776 if (CheckPackage
== NULL
) {
1781 // for each node in the list
1783 for (Node
= GetFirstNode(CheckPackage
); Node
!= CheckPackage
; Node
= GetFirstNode(CheckPackage
)) {
1785 // Remove it from the list
1787 RemoveEntryList(Node
);
1790 // if it has a name free the name
1792 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1793 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1797 // if it has a value free the value
1799 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
1800 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1804 // free the node structure
1806 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
1809 // free the list head node
1811 FreePool(CheckPackage
);
1814 Checks for presence of a flag parameter
1816 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1818 if CheckPackage is NULL then return FALSE.
1819 if KeyString is NULL then ASSERT()
1821 @param CheckPackage The package of parsed command line arguments
1822 @param KeyString the Key of the command line argument to check for
1824 @retval TRUE the flag is on the command line
1825 @retval FALSE the flag is not on the command line
1829 ShellCommandLineGetFlag (
1830 IN CONST LIST_ENTRY
*CheckPackage
,
1831 IN CHAR16
*KeyString
1837 // ASSERT that both CheckPackage and KeyString aren't NULL
1839 ASSERT(KeyString
!= NULL
);
1842 // return FALSE for no package
1844 if (CheckPackage
== NULL
) {
1849 // enumerate through the list of parametrs
1851 for (Node
= GetFirstNode(CheckPackage
) ; !IsNull (CheckPackage
, Node
) ; Node
= GetNextNode(CheckPackage
, Node
) ) {
1853 // If the Name matches, return TRUE (and there may be NULL name)
1855 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1856 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1864 returns value from command line argument
1866 value parameters are in the form of "-<Key> value" or "/<Key> value"
1868 if CheckPackage is NULL, then return NULL;
1870 @param CheckPackage The package of parsed command line arguments
1871 @param KeyString the Key of the command line argument to check for
1873 @retval NULL the flag is not on the command line
1874 @return !=NULL pointer to unicode string of the value
1878 ShellCommandLineGetValue (
1879 IN CONST LIST_ENTRY
*CheckPackage
,
1880 IN CHAR16
*KeyString
1886 // check for CheckPackage == NULL
1888 if (CheckPackage
== NULL
) {
1893 // enumerate through the list of parametrs
1895 for (Node
= GetFirstNode(CheckPackage
) ; !IsNull (CheckPackage
, Node
) ; Node
= GetNextNode(CheckPackage
, Node
) ) {
1897 // If the Name matches, return the value (name can be NULL)
1899 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1900 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1901 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1908 returns raw value from command line argument
1910 raw value parameters are in the form of "value" in a specific position in the list
1912 if CheckPackage is NULL, then return NULL;
1914 @param CheckPackage The package of parsed command line arguments
1915 @param Position the position of the value
1917 @retval NULL the flag is not on the command line
1918 @return !=NULL pointer to unicode string of the value
1922 ShellCommandLineGetRawValue (
1923 IN CONST LIST_ENTRY
*CheckPackage
,
1930 // check for CheckPackage == NULL
1932 if (CheckPackage
== NULL
) {
1937 // enumerate through the list of parametrs
1939 for (Node
= GetFirstNode(CheckPackage
) ; !IsNull (CheckPackage
, Node
) ; Node
= GetNextNode(CheckPackage
, Node
) ) {
1941 // If the position matches, return the value
1943 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
1944 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1950 This is a find and replace function. it will return the NewString as a copy of
1951 SourceString with each instance of FindTarget replaced with ReplaceWith.
1953 If the string would grow bigger than NewSize it will halt and return error.
1955 @param[in] SourceString String with source buffer
1956 @param[in][out] NewString String with resultant buffer
1957 @param[in] NewSize Size in bytes of NewString
1958 @param[in] FindTarget String to look for
1959 @param[in[ ReplaceWith String to replace FindTarget with
1961 @retval EFI_INVALID_PARAMETER SourceString was NULL
1962 @retval EFI_INVALID_PARAMETER NewString was NULL
1963 @retval EFI_INVALID_PARAMETER FindTarget was NULL
1964 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
1965 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
1966 @retval EFI_INVALID_PARAMETER SourceString had length < 1
1967 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
1968 the new string (truncation occurred)
1969 @retval EFI_SUCCESS the string was sucessfully copied with replacement
1975 IN CHAR16 CONST
*SourceString
,
1976 IN CHAR16
*NewString
,
1978 IN CONST CHAR16
*FindTarget
,
1979 IN CONST CHAR16
*ReplaceWith
1981 if ( (SourceString
== NULL
)
1982 || (NewString
== NULL
)
1983 || (FindTarget
== NULL
)
1984 || (ReplaceWith
== NULL
)
1985 || (StrLen(FindTarget
) < 1)
1986 || (StrLen(SourceString
) < 1)
1988 return (EFI_INVALID_PARAMETER
);
1990 NewString
= SetMem16(NewString
, NewSize
, L
'\0');
1991 while (*SourceString
!= L
'\0') {
1992 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0) {
1993 SourceString
+= StrLen(FindTarget
);
1994 if (StrSize(NewString
) + (StrLen(ReplaceWith
)*sizeof(CHAR16
)) > NewSize
) {
1995 return (EFI_BUFFER_TOO_SMALL
);
1997 StrCat(NewString
, ReplaceWith
);
1999 if (StrSize(NewString
) + sizeof(CHAR16
) > NewSize
) {
2000 return (EFI_BUFFER_TOO_SMALL
);
2002 StrnCat(NewString
, SourceString
, 1);
2006 return (EFI_SUCCESS
);
2010 Print at a specific location on the screen.
2012 This function will move the cursor to a given screen location and print the specified string
2014 If -1 is specified for either the Row or Col the current screen location for BOTH
2017 if either Row or Col is out of range for the current console, then ASSERT
2018 if Format is NULL, then ASSERT
2020 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2021 the following additional flags:
2022 %N - Set output attribute to normal
2023 %H - Set output attribute to highlight
2024 %E - Set output attribute to error
2025 %B - Set output attribute to blue color
2026 %V - Set output attribute to green color
2028 Note: The background color is controlled by the shell command cls.
2030 @param[in] Row the row to print at
2031 @param[in] Col the column to print at
2032 @param[in] Format the format string
2034 @return the number of characters printed to the screen
2040 IN INT32 Col OPTIONAL
,
2041 IN INT32 Row OPTIONAL
,
2042 IN CONST CHAR16
*Format
,
2047 CHAR16
*PostReplaceFormat
;
2048 CHAR16
*PostReplaceFormat2
;
2052 UINTN NormalAttribute
;
2053 CHAR16
*ResumeLocation
;
2054 CHAR16
*FormatWalker
;
2056 VA_START (Marker
, Format
);
2058 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
2059 PostReplaceFormat
= AllocateZeroPool (BufferSize
);
2060 ASSERT (PostReplaceFormat
!= NULL
);
2061 PostReplaceFormat2
= AllocateZeroPool (BufferSize
);
2062 ASSERT (PostReplaceFormat2
!= NULL
);
2065 // Back and forth each time fixing up 1 of our flags...
2067 Status
= CopyReplace(Format
, PostReplaceFormat
, BufferSize
, L
"%N", L
"%%N");
2068 ASSERT_EFI_ERROR(Status
);
2069 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%E", L
"%%E");
2070 ASSERT_EFI_ERROR(Status
);
2071 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%H", L
"%%H");
2072 ASSERT_EFI_ERROR(Status
);
2073 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%B", L
"%%B");
2074 ASSERT_EFI_ERROR(Status
);
2075 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%V", L
"%%V");
2076 ASSERT_EFI_ERROR(Status
);
2079 // Use the last buffer from replacing to print from...
2081 Return
= UnicodeVSPrint (PostReplaceFormat2
, BufferSize
, PostReplaceFormat
, Marker
);
2083 FreePool(PostReplaceFormat
);
2085 if (Col
!= -1 && Row
!= -1) {
2086 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2087 ASSERT_EFI_ERROR(Status
);
2090 NormalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2091 FormatWalker
= PostReplaceFormat2
;
2092 while (*FormatWalker
!= L
'\0') {
2094 // Find the next attribute change request
2096 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2097 if (ResumeLocation
!= NULL
) {
2098 *ResumeLocation
= L
'\0';
2101 // print the current FormatWalker string
2103 Status
= gST
->ConOut
->OutputString(gST
->ConOut
, FormatWalker
);
2104 ASSERT_EFI_ERROR(Status
);
2106 // update the attribute
2108 if (ResumeLocation
!= NULL
) {
2109 switch (*(ResumeLocation
+1)) {
2111 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2114 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2117 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2120 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2123 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2131 // reset to normal now...
2133 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2138 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2140 FormatWalker
= ResumeLocation
+ 2;
2143 FreePool(PostReplaceFormat2
);