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
262 if (mEfiShellEnvironment2
!= NULL
) {
263 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
264 &gEfiShellEnvironment2Guid
,
267 mEfiShellEnvironment2
= NULL
;
269 if (mEfiShellInterface
!= NULL
) {
270 gBS
->CloseProtocol(ImageHandle
,
271 &gEfiShellInterfaceGuid
,
274 mEfiShellInterface
= NULL
;
276 if (mEfiShellProtocol
!= NULL
) {
277 gBS
->CloseProtocol(ImageHandle
,
278 &gEfiShellProtocolGuid
,
281 mEfiShellProtocol
= NULL
;
283 if (mEfiShellParametersProtocol
!= NULL
) {
284 gBS
->CloseProtocol(ImageHandle
,
285 &gEfiShellParametersProtocolGuid
,
288 mEfiShellParametersProtocol
= NULL
;
290 mEfiShellEnvironment2Handle
= NULL
;
291 return (EFI_SUCCESS
);
295 This function causes the shell library to initialize itself. If the shell library
296 is already initialized it will de-initialize all the current protocol poitners and
297 re-populate them again.
299 When the library is used with PcdShellLibAutoInitialize set to true this function
300 will return EFI_SUCCESS and perform no actions.
302 This function is intended for internal access for shell commands only.
304 @retval EFI_SUCCESS the initialization was complete sucessfully
312 // if auto initialize is not false then skip
314 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
315 return (EFI_SUCCESS
);
319 // deinit the current stuff
321 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
324 // init the new stuff
326 return (ShellLibConstructorWorker(gImageHandle
, gST
));
330 This function will retrieve the information about the file for the handle
331 specified and store it in allocated pool memory.
333 This function allocates a buffer to store the file's information. It is the
334 caller's responsibility to free the buffer
336 @param FileHandle The file handle of the file for which information is
339 @retval NULL information could not be retrieved.
341 @return the information about the file
346 IN EFI_FILE_HANDLE FileHandle
349 return (FileFunctionMap
.GetFileInfo(FileHandle
));
353 This function will set the information about the file for the opened handle
356 @param FileHandle The file handle of the file for which information
359 @param FileInfo The infotmation to set.
361 @retval EFI_SUCCESS The information was set.
362 @retval EFI_UNSUPPORTED The InformationType is not known.
363 @retval EFI_NO_MEDIA The device has no medium.
364 @retval EFI_DEVICE_ERROR The device reported an error.
365 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
366 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
367 @retval EFI_ACCESS_DENIED The file was opened read only.
368 @retval EFI_VOLUME_FULL The volume is full.
373 IN EFI_FILE_HANDLE FileHandle
,
374 IN EFI_FILE_INFO
*FileInfo
377 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
381 This function will open a file or directory referenced by DevicePath.
383 This function opens a file with the open mode according to the file path. The
384 Attributes is valid only for EFI_FILE_MODE_CREATE.
386 @param FilePath on input the device path to the file. On output
387 the remaining device path.
388 @param DeviceHandle pointer to the system device handle.
389 @param FileHandle pointer to the file handle.
390 @param OpenMode the mode to open the file with.
391 @param Attributes the file's file attributes.
393 @retval EFI_SUCCESS The information was set.
394 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
395 @retval EFI_UNSUPPORTED Could not open the file path.
396 @retval EFI_NOT_FOUND The specified file could not be found on the
397 device or the file system could not be found on
399 @retval EFI_NO_MEDIA The device has no medium.
400 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
401 medium is no longer supported.
402 @retval EFI_DEVICE_ERROR The device reported an error.
403 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
404 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
405 @retval EFI_ACCESS_DENIED The file was opened read only.
406 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
408 @retval EFI_VOLUME_FULL The volume is full.
412 ShellOpenFileByDevicePath(
413 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
414 OUT EFI_HANDLE
*DeviceHandle
,
415 OUT EFI_FILE_HANDLE
*FileHandle
,
422 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
423 EFI_FILE_HANDLE LastHandle
;
426 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
428 ASSERT(FilePath
!= NULL
);
429 ASSERT(FileHandle
!= NULL
);
430 ASSERT(DeviceHandle
!= NULL
);
432 // which shell interface should we use
434 if (mEfiShellProtocol
!= NULL
) {
436 // use UEFI Shell 2.0 method.
438 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
439 if (FileName
== NULL
) {
440 return (EFI_INVALID_PARAMETER
);
442 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
449 // use old shell method.
451 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
454 if (EFI_ERROR (Status
)) {
457 Status
= gBS
->OpenProtocol(*DeviceHandle
,
458 &gEfiSimpleFileSystemProtocolGuid
,
459 (VOID
**)&EfiSimpleFileSystemProtocol
,
462 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
463 if (EFI_ERROR (Status
)) {
466 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
467 if (EFI_ERROR (Status
)) {
473 // go down directories one node at a time.
475 while (!IsDevicePathEnd (*FilePath
)) {
477 // For file system access each node should be a file path component
479 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
480 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
483 return (EFI_INVALID_PARAMETER
);
486 // Open this file path node
488 LastHandle
= *FileHandle
;
492 // Try to test opening an existing file
494 Status
= LastHandle
->Open (
497 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
498 OpenMode
&~EFI_FILE_MODE_CREATE
,
503 // see if the error was that it needs to be created
505 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
506 Status
= LastHandle
->Open (
509 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
515 // Close the last node
517 LastHandle
->Close (LastHandle
);
519 if (EFI_ERROR(Status
)) {
526 *FilePath
= NextDevicePathNode (*FilePath
);
528 return (EFI_SUCCESS
);
532 This function will open a file or directory referenced by filename.
534 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
535 otherwise, the Filehandle is NULL. The Attributes is valid only for
536 EFI_FILE_MODE_CREATE.
538 if FileNAme is NULL then ASSERT()
540 @param FileName pointer to file name
541 @param FileHandle pointer to the file handle.
542 @param OpenMode the mode to open the file with.
543 @param Attributes the file's file attributes.
545 @retval EFI_SUCCESS The information was set.
546 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
547 @retval EFI_UNSUPPORTED Could not open the file path.
548 @retval EFI_NOT_FOUND The specified file could not be found on the
549 device or the file system could not be found
551 @retval EFI_NO_MEDIA The device has no medium.
552 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
553 medium is no longer supported.
554 @retval EFI_DEVICE_ERROR The device reported an error.
555 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
556 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
557 @retval EFI_ACCESS_DENIED The file was opened read only.
558 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
560 @retval EFI_VOLUME_FULL The volume is full.
565 IN CONST CHAR16
*FileName
,
566 OUT EFI_FILE_HANDLE
*FileHandle
,
571 EFI_HANDLE DeviceHandle
;
572 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
574 EFI_FILE_INFO
*FileInfo
;
577 // ASSERT if FileName is NULL
579 ASSERT(FileName
!= NULL
);
581 if (mEfiShellProtocol
!= NULL
) {
583 // Use UEFI Shell 2.0 method
585 Status
= mEfiShellProtocol
->OpenFileByName(FileName
,
588 if (!EFI_ERROR(Status
)){
589 FileInfo
= FileHandleGetInfo(*FileHandle
);
590 ASSERT(FileInfo
!= NULL
);
591 FileInfo
->Attribute
= Attributes
;
592 Status
= FileHandleSetInfo(*FileHandle
, FileInfo
);
597 // Using EFI Shell version
598 // this means convert name to path and call that function
599 // since this will use EFI method again that will open it.
601 ASSERT(mEfiShellEnvironment2
!= NULL
);
602 FilePath
= mEfiShellEnvironment2
->NameToPath ((CHAR16
*)FileName
);
603 if (FileDevicePath
!= NULL
) {
604 return (ShellOpenFileByDevicePath(&FilePath
,
610 return (EFI_DEVICE_ERROR
);
613 This function create a directory
615 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
616 otherwise, the Filehandle is NULL. If the directory already existed, this
617 function opens the existing directory.
619 @param DirectoryName pointer to directory name
620 @param FileHandle pointer to the file handle.
622 @retval EFI_SUCCESS The information was set.
623 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
624 @retval EFI_UNSUPPORTED Could not open the file path.
625 @retval EFI_NOT_FOUND The specified file could not be found on the
626 device or the file system could not be found
628 @retval EFI_NO_MEDIA The device has no medium.
629 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
630 medium is no longer supported.
631 @retval EFI_DEVICE_ERROR The device reported an error.
632 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
633 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
634 @retval EFI_ACCESS_DENIED The file was opened read only.
635 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
637 @retval EFI_VOLUME_FULL The volume is full.
638 @sa ShellOpenFileByName
642 ShellCreateDirectory(
643 IN CONST CHAR16
*DirectoryName
,
644 OUT EFI_FILE_HANDLE
*FileHandle
648 // this is a pass thru to the open file function with sepcific open mode and attributes
650 return (ShellOpenFileByName(DirectoryName
,
652 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
658 This function reads information from an opened file.
660 If FileHandle is not a directory, the function reads the requested number of
661 bytes from the file at the file's current position and returns them in Buffer.
662 If the read goes beyond the end of the file, the read length is truncated to the
663 end of the file. The file's current position is increased by the number of bytes
664 returned. If FileHandle is a directory, the function reads the directory entry
665 at the file's current position and returns the entry in Buffer. If the Buffer
666 is not large enough to hold the current directory entry, then
667 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
668 BufferSize is set to be the size of the buffer needed to read the entry. On
669 success, the current position is updated to the next directory entry. If there
670 are no more directory entries, the read returns a zero-length buffer.
671 EFI_FILE_INFO is the structure returned as the directory entry.
673 @param FileHandle the opened file handle
674 @param BufferSize on input the size of buffer in bytes. on return
675 the number of bytes written.
676 @param Buffer the buffer to put read data into.
678 @retval EFI_SUCCESS Data was read.
679 @retval EFI_NO_MEDIA The device has no media.
680 @retval EFI_DEVICE_ERROR The device reported an error.
681 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
682 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
689 IN EFI_FILE_HANDLE FileHandle
,
690 IN OUT UINTN
*BufferSize
,
694 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
699 Write data to a file.
701 This function writes the specified number of bytes to the file at the current
702 file position. The current file position is advanced the actual number of bytes
703 written, which is returned in BufferSize. Partial writes only occur when there
704 has been a data error during the write attempt (such as "volume space full").
705 The file is automatically grown to hold the data if required. Direct writes to
706 opened directories are not supported.
708 @param FileHandle The opened file for writing
709 @param BufferSize on input the number of bytes in Buffer. On output
710 the number of bytes written.
711 @param Buffer the buffer containing data to write is stored.
713 @retval EFI_SUCCESS Data was written.
714 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
715 @retval EFI_NO_MEDIA The device has no media.
716 @retval EFI_DEVICE_ERROR The device reported an error.
717 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
718 @retval EFI_WRITE_PROTECTED The device is write-protected.
719 @retval EFI_ACCESS_DENIED The file was open for read only.
720 @retval EFI_VOLUME_FULL The volume is full.
725 IN EFI_FILE_HANDLE FileHandle
,
726 IN OUT UINTN
*BufferSize
,
730 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
734 Close an open file handle.
736 This function closes a specified file handle. All "dirty" cached file data is
737 flushed to the device, and the file is closed. In all cases the handle is
740 @param FileHandle the file handle to close.
742 @retval EFI_SUCCESS the file handle was closed sucessfully.
747 IN EFI_FILE_HANDLE
*FileHandle
750 return (FileFunctionMap
.CloseFile(*FileHandle
));
754 Delete a file and close the handle
756 This function closes and deletes a file. In all cases the file handle is closed.
757 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
758 returned, but the handle is still closed.
760 @param FileHandle the file handle to delete
762 @retval EFI_SUCCESS the file was closed sucessfully
763 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
765 @retval INVALID_PARAMETER One of the parameters has an invalid value.
770 IN EFI_FILE_HANDLE
*FileHandle
773 return (FileFunctionMap
.DeleteFile(*FileHandle
));
777 Set the current position in a file.
779 This function sets the current file position for the handle to the position
780 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
781 absolute positioning is supported, and seeking past the end of the file is
782 allowed (a subsequent write would grow the file). Seeking to position
783 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
784 If FileHandle is a directory, the only position that may be set is zero. This
785 has the effect of starting the read process of the directory entries over.
787 @param FileHandle The file handle on which the position is being set
788 @param Position Byte position from begining of file
790 @retval EFI_SUCCESS Operation completed sucessfully.
791 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
793 @retval INVALID_PARAMETER One of the parameters has an invalid value.
797 ShellSetFilePosition (
798 IN EFI_FILE_HANDLE FileHandle
,
802 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
806 Gets a file's current position
808 This function retrieves the current file position for the file handle. For
809 directories, the current file position has no meaning outside of the file
810 system driver and as such the operation is not supported. An error is returned
811 if FileHandle is a directory.
813 @param FileHandle The open file handle on which to get the position.
814 @param Position Byte position from begining of file.
816 @retval EFI_SUCCESS the operation completed sucessfully.
817 @retval INVALID_PARAMETER One of the parameters has an invalid value.
818 @retval EFI_UNSUPPORTED the request is not valid on directories.
822 ShellGetFilePosition (
823 IN EFI_FILE_HANDLE FileHandle
,
827 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
830 Flushes data on a file
832 This function flushes all modified data associated with a file to a device.
834 @param FileHandle The file handle on which to flush data
836 @retval EFI_SUCCESS The data was flushed.
837 @retval EFI_NO_MEDIA The device has no media.
838 @retval EFI_DEVICE_ERROR The device reported an error.
839 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
840 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
841 @retval EFI_ACCESS_DENIED The file was opened for read only.
846 IN EFI_FILE_HANDLE FileHandle
849 return (FileFunctionMap
.FlushFile(FileHandle
));
853 Retrieves the first file from a directory
855 This function opens a directory and gets the first file's info in the
856 directory. Caller can use ShellFindNextFile() to get other files. When
857 complete the caller is responsible for calling FreePool() on Buffer.
859 @param DirHandle The file handle of the directory to search
860 @param Buffer Pointer to buffer for file's information
862 @retval EFI_SUCCESS Found the first file.
863 @retval EFI_NOT_FOUND Cannot find the directory.
864 @retval EFI_NO_MEDIA The device has no media.
865 @retval EFI_DEVICE_ERROR The device reported an error.
866 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
867 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
873 IN EFI_FILE_HANDLE DirHandle
,
874 OUT EFI_FILE_INFO
**Buffer
878 // pass to file handle lib
880 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
883 Retrieves the next file in a directory.
885 To use this function, caller must call the LibFindFirstFile() to get the
886 first file, and then use this function get other files. This function can be
887 called for several times to get each file's information in the directory. If
888 the call of ShellFindNextFile() got the last file in the directory, the next
889 call of this function has no file to get. *NoFile will be set to TRUE and the
890 Buffer memory will be automatically freed.
892 @param DirHandle the file handle of the directory
893 @param Buffer pointer to buffer for file's information
894 @param NoFile pointer to boolean when last file is found
896 @retval EFI_SUCCESS Found the next file, or reached last file
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.
904 IN EFI_FILE_HANDLE DirHandle
,
905 OUT EFI_FILE_INFO
*Buffer
,
910 // pass to file handle lib
912 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
915 Retrieve the size of a file.
917 if FileHandle is NULL then ASSERT()
918 if Size is NULL then ASSERT()
920 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
923 @param FileHandle file handle from which size is retrieved
924 @param Size pointer to size
926 @retval EFI_SUCCESS operation was completed sucessfully
927 @retval EFI_DEVICE_ERROR cannot access the file
932 IN EFI_FILE_HANDLE FileHandle
,
936 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
939 Retrieves the status of the break execution flag
941 this function is useful to check whether the application is being asked to halt by the shell.
943 @retval TRUE the execution break is enabled
944 @retval FALSE the execution break is not enabled
948 ShellGetExecutionBreakFlag(
953 // Check for UEFI Shell 2.0 protocols
955 if (mEfiShellProtocol
!= NULL
) {
958 // We are using UEFI Shell 2.0; see if the event has been triggered
960 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
967 // using EFI Shell; call the function to check
969 ASSERT(mEfiShellEnvironment2
!= NULL
);
970 return (mEfiShellEnvironment2
->GetExecutionBreak());
973 return the value of an environment variable
975 this function gets the value of the environment variable set by the
976 ShellSetEnvironmentVariable function
978 @param EnvKey The key name of the environment variable.
980 @retval NULL the named environment variable does not exist.
981 @return != NULL pointer to the value of the environment variable
985 ShellGetEnvironmentVariable (
986 IN CONST CHAR16
*EnvKey
990 // Check for UEFI Shell 2.0 protocols
992 if (mEfiShellProtocol
!= NULL
) {
993 return (mEfiShellProtocol
->GetEnv(EnvKey
));
997 // ASSERT that we must have EFI shell
999 ASSERT(mEfiShellEnvironment2
!= NULL
);
1004 return (mEfiShellEnvironment2
->GetEnv((CHAR16
*)EnvKey
));
1007 set the value of an environment variable
1009 This function changes the current value of the specified environment variable. If the
1010 environment variable exists and the Value is an empty string, then the environment
1011 variable is deleted. If the environment variable exists and the Value is not an empty
1012 string, then the value of the environment variable is changed. If the environment
1013 variable does not exist and the Value is an empty string, there is no action. If the
1014 environment variable does not exist and the Value is a non-empty string, then the
1015 environment variable is created and assigned the specified value.
1017 This is not supported pre-UEFI Shell 2.0.
1019 @param EnvKey The key name of the environment variable.
1020 @param EnvVal The Value of the environment variable
1021 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1023 @retval EFI_SUCCESS the operation was completed sucessfully
1024 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1028 ShellSetEnvironmentVariable (
1029 IN CONST CHAR16
*EnvKey
,
1030 IN CONST CHAR16
*EnvVal
,
1035 // Check for UEFI Shell 2.0 protocols
1037 if (mEfiShellProtocol
!= NULL
) {
1038 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1042 // This feature does not exist under EFI shell
1044 return (EFI_UNSUPPORTED
);
1047 cause the shell to parse and execute a command line.
1049 This function creates a nested instance of the shell and executes the specified
1050 command (CommandLine) with the specified environment (Environment). Upon return,
1051 the status code returned by the specified command is placed in StatusCode.
1052 If Environment is NULL, then the current environment is used and all changes made
1053 by the commands executed will be reflected in the current environment. If the
1054 Environment is non-NULL, then the changes made will be discarded.
1055 The CommandLine is executed from the current working directory on the current
1058 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1059 Output is only supported for pre-UEFI Shell 2.0
1061 @param ImageHandle Parent image that is starting the operation
1062 @param CommandLine pointer to null terminated command line.
1063 @param Output true to display debug output. false to hide it.
1064 @param EnvironmentVariables optional pointer to array of environment variables
1065 in the form "x=y". if NULL current set is used.
1066 @param Status the status of the run command line.
1068 @retval EFI_SUCCESS the operation completed sucessfully. Status
1069 contains the status code returned.
1070 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1071 @retval EFI_OUT_OF_RESOURCES out of resources
1072 @retval EFI_UNSUPPORTED the operation is not allowed.
1077 IN EFI_HANDLE
*ParentHandle
,
1078 IN CHAR16
*CommandLine OPTIONAL
,
1079 IN BOOLEAN Output OPTIONAL
,
1080 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1081 OUT EFI_STATUS
*Status OPTIONAL
1085 // Check for UEFI Shell 2.0 protocols
1087 if (mEfiShellProtocol
!= NULL
) {
1089 // Call UEFI Shell 2.0 version (not using Output parameter)
1091 return (mEfiShellProtocol
->Execute(ParentHandle
,
1093 EnvironmentVariables
,
1097 // ASSERT that we must have EFI shell
1099 ASSERT(mEfiShellEnvironment2
!= NULL
);
1101 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1102 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1104 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1109 Retreives the current directory path
1111 If the DeviceName is NULL, it returns the current device's current directory
1112 name. If the DeviceName is not NULL, it returns the current directory name
1115 @param DeviceName the name of the drive to get directory on
1117 @retval NULL the directory does not exist
1118 @return != NULL the directory
1122 ShellGetCurrentDir (
1123 IN CHAR16
*DeviceName OPTIONAL
1127 // Check for UEFI Shell 2.0 protocols
1129 if (mEfiShellProtocol
!= NULL
) {
1130 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1133 // ASSERT that we must have EFI shell
1135 ASSERT(mEfiShellEnvironment2
!= NULL
);
1136 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1139 sets (enabled or disabled) the page break mode
1141 when page break mode is enabled the screen will stop scrolling
1142 and wait for operator input before scrolling a subsequent screen.
1144 @param CurrentState TRUE to enable and FALSE to disable
1148 ShellSetPageBreakMode (
1149 IN BOOLEAN CurrentState
1153 // check for enabling
1155 if (CurrentState
!= 0x00) {
1157 // check for UEFI Shell 2.0
1159 if (mEfiShellProtocol
!= NULL
) {
1161 // Enable with UEFI 2.0 Shell
1163 mEfiShellProtocol
->EnablePageBreak();
1167 // ASSERT that must have EFI Shell
1169 ASSERT(mEfiShellEnvironment2
!= NULL
);
1171 // Enable with EFI Shell
1173 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1178 // check for UEFI Shell 2.0
1180 if (mEfiShellProtocol
!= NULL
) {
1182 // Disable with UEFI 2.0 Shell
1184 mEfiShellProtocol
->DisablePageBreak();
1188 // ASSERT that must have EFI Shell
1190 ASSERT(mEfiShellEnvironment2
!= NULL
);
1192 // Disable with EFI Shell
1194 mEfiShellEnvironment2
->DisablePageBreak ();
1201 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1202 /// This allows for the struct to be populated.
1209 EFI_FILE_HANDLE Handle
;
1210 EFI_FILE_INFO
*Info
;
1211 } EFI_SHELL_FILE_INFO_NO_CONST
;
1214 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1216 if OldStyleFileList is NULL then ASSERT()
1218 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1219 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1220 the ShellCloseFileMetaArg function.
1222 @param[in] FileList the EFI shell list type
1223 @param[in,out] ListHead the list to add to
1225 @retval the resultant head of the double linked new format list;
1229 InternalShellConvertFileListType (
1230 IN LIST_ENTRY
*FileList
,
1231 IN OUT LIST_ENTRY
*ListHead
1233 SHELL_FILE_ARG
*OldInfo
;
1235 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1240 ASSERT(FileList
!= NULL
);
1241 ASSERT(ListHead
!= NULL
);
1244 // enumerate through each member of the old list and copy
1246 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1247 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1250 // make sure the old list was valid
1252 ASSERT(OldInfo
!= NULL
);
1253 ASSERT(OldInfo
->Info
!= NULL
);
1254 ASSERT(OldInfo
->FullName
!= NULL
);
1255 ASSERT(OldInfo
->FileName
!= NULL
);
1258 // allocate a new EFI_SHELL_FILE_INFO object
1260 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1263 // copy the simple items
1265 NewInfo
->Handle
= OldInfo
->Handle
;
1266 NewInfo
->Status
= OldInfo
->Status
;
1268 // old shell checks for 0 not NULL
1269 OldInfo
->Handle
= 0;
1272 // allocate new space to copy strings and structure
1274 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1275 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1276 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1279 // make sure all the memory allocations were sucessful
1281 ASSERT(NewInfo
->FullName
!= NULL
);
1282 ASSERT(NewInfo
->FileName
!= NULL
);
1283 ASSERT(NewInfo
->Info
!= NULL
);
1286 // Copt the strings and structure
1288 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1289 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1290 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1293 // add that to the list
1295 InsertTailList(ListHead
, &NewInfo
->Link
);
1300 Opens a group of files based on a path.
1302 This function uses the Arg to open all the matching files. Each matched
1303 file has a SHELL_FILE_ARG structure to record the file information. These
1304 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1305 structures from ListHead to access each file. This function supports wildcards
1306 and will process '?' and '*' as such. the list must be freed with a call to
1307 ShellCloseFileMetaArg().
1309 If you are NOT appending to an existing list *ListHead must be NULL.
1311 @param Arg pointer to path string
1312 @param OpenMode mode to open files with
1313 @param ListHead head of linked list of results
1315 @retval EFI_SUCCESS the operation was sucessful and the list head
1316 contains the list of opened files
1317 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1318 *ListHead is set to NULL.
1319 @return != EFI_SUCCESS the operation failed
1321 @sa InternalShellConvertFileListType
1325 ShellOpenFileMetaArg (
1328 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1332 LIST_ENTRY mOldStyleFileList
;
1335 // ASSERT that Arg and ListHead are not NULL
1337 ASSERT(Arg
!= NULL
);
1338 ASSERT(ListHead
!= NULL
);
1341 // Check for UEFI Shell 2.0 protocols
1343 if (mEfiShellProtocol
!= NULL
) {
1344 return (mEfiShellProtocol
->OpenFileList(Arg
,
1350 // ASSERT that we must have EFI shell
1352 ASSERT(mEfiShellEnvironment2
!= NULL
);
1355 // make sure the list head is initialized
1357 InitializeListHead(&mOldStyleFileList
);
1360 // Get the EFI Shell list of files
1362 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, &mOldStyleFileList
);
1363 if (EFI_ERROR(Status
)) {
1368 if (*ListHead
== NULL
) {
1369 *ListHead
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1370 if (*ListHead
== NULL
) {
1371 return (EFI_OUT_OF_RESOURCES
);
1376 // Convert that to equivalent of UEFI Shell 2.0 structure
1378 InternalShellConvertFileListType(&mOldStyleFileList
, &(*ListHead
)->Link
);
1381 // Free the EFI Shell version that was converted.
1383 mEfiShellEnvironment2
->FreeFileList(&mOldStyleFileList
);
1388 Free the linked list returned from ShellOpenFileMetaArg
1390 if ListHead is NULL then ASSERT()
1392 @param ListHead the pointer to free
1394 @retval EFI_SUCCESS the operation was sucessful
1398 ShellCloseFileMetaArg (
1399 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1405 // ASSERT that ListHead is not NULL
1407 ASSERT(ListHead
!= NULL
);
1410 // Check for UEFI Shell 2.0 protocols
1412 if (mEfiShellProtocol
!= NULL
) {
1413 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1416 // Since this is EFI Shell version we need to free our internally made copy
1419 for ( Node
= GetFirstNode(&(*ListHead
)->Link
)
1420 ; IsListEmpty(&(*ListHead
)->Link
) == FALSE
1421 ; Node
= GetFirstNode(&(*ListHead
)->Link
)) {
1422 RemoveEntryList(Node
);
1423 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1424 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1425 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1426 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1427 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1438 UINTN OriginalPosition
;
1439 } SHELL_PARAM_PACKAGE
;
1442 Checks the list of valid arguments and returns TRUE if the item was found. If the
1443 return value is TRUE then the type parameter is set also.
1445 if CheckList is NULL then ASSERT();
1446 if Name is NULL then ASSERT();
1447 if Type is NULL then ASSERT();
1449 @param Type pointer to type of parameter if it was found
1450 @param Name pointer to Name of parameter found
1451 @param CheckList List to check against
1453 @retval TRUE the Parameter was found. Type is valid.
1454 @retval FALSE the Parameter was not found. Type is not valid.
1458 InternalIsOnCheckList (
1459 IN CONST CHAR16
*Name
,
1460 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1464 SHELL_PARAM_ITEM
*TempListItem
;
1467 // ASSERT that all 3 pointer parameters aren't NULL
1469 ASSERT(CheckList
!= NULL
);
1470 ASSERT(Type
!= NULL
);
1471 ASSERT(Name
!= NULL
);
1474 // question mark and page break mode are always supported
1476 if ((StrCmp(Name
, L
"-?") == 0) ||
1477 (StrCmp(Name
, L
"-b") == 0)
1483 // Enumerate through the list
1485 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1487 // If the Name matches set the type and return TRUE
1489 if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1490 *Type
= TempListItem
->Type
;
1497 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1499 @param Name pointer to Name of parameter found
1501 @retval TRUE the Parameter is a flag.
1502 @retval FALSE the Parameter not a flag
1507 IN CONST CHAR16
*Name
1511 // ASSERT that Name isn't NULL
1513 ASSERT(Name
!= NULL
);
1516 // If the Name has a / or - as the first character return TRUE
1518 if ((Name
[0] == L
'/') ||
1519 (Name
[0] == L
'-') ||
1528 Checks the command line arguments passed against the list of valid ones.
1530 If no initialization is required, then return RETURN_SUCCESS.
1532 @param CheckList pointer to list of parameters to check
1533 @param CheckPackage pointer to pointer to list checked values
1534 @param ProblemParam optional pointer to pointer to unicode string for
1535 the paramater that caused failure. If used then the
1536 caller is responsible for freeing the memory.
1537 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1538 @param Argc Count of parameters in Argv
1539 @param Argv pointer to array of parameters
1541 @retval EFI_SUCCESS The operation completed sucessfully.
1542 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1543 @retval EFI_INVALID_PARAMETER A parameter was invalid
1544 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1545 duplicated. the duplicated command line argument
1546 was returned in ProblemParam if provided.
1547 @retval EFI_NOT_FOUND a argument required a value that was missing.
1548 the invalid command line argument was returned in
1549 ProblemParam if provided.
1553 InternalCommandLineParse (
1554 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1555 OUT LIST_ENTRY
**CheckPackage
,
1556 OUT CHAR16
**ProblemParam OPTIONAL
,
1557 IN BOOLEAN AutoPageBreak
,
1558 IN CONST CHAR16
**Argv
,
1564 ParamType CurrentItemType
;
1565 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1566 BOOLEAN GetItemValue
;
1568 CurrentItemPackage
= NULL
;
1573 ASSERT(CheckList
!= NULL
);
1574 ASSERT(Argv
!= NULL
);
1577 GetItemValue
= FALSE
;
1580 // If there is only 1 item we dont need to do anything
1583 *CheckPackage
= NULL
;
1584 return (EFI_SUCCESS
);
1588 // initialize the linked list
1590 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1591 InitializeListHead(*CheckPackage
);
1594 // loop through each of the arguments
1596 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1597 if (Argv
[LoopCounter
] == NULL
) {
1599 // do nothing for NULL argv
1601 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) == TRUE
) {
1605 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1606 ASSERT(CurrentItemPackage
!= NULL
);
1607 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1608 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1609 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1610 CurrentItemPackage
->Type
= CurrentItemType
;
1611 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1612 CurrentItemPackage
->Value
= NULL
;
1615 // Does this flag require a value
1617 if (CurrentItemPackage
->Type
== TypeValue
) {
1619 // trigger the next loop to populate the value of this item
1621 GetItemValue
= TRUE
;
1624 // this item has no value expected; we are done
1626 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1628 } else if (GetItemValue
== TRUE
&& InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1629 ASSERT(CurrentItemPackage
!= NULL
);
1631 // get the item VALUE for the previous flag
1633 GetItemValue
= FALSE
;
1634 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1635 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1636 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1637 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1638 } else if (InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1640 // add this one as a non-flag
1642 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1643 ASSERT(CurrentItemPackage
!= NULL
);
1644 CurrentItemPackage
->Name
= NULL
;
1645 CurrentItemPackage
->Type
= TypePosition
;
1646 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1647 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1648 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1649 CurrentItemPackage
->OriginalPosition
= Count
++;
1650 InsertHeadList(*CheckPackage
, &CurrentItemPackage
->Link
);
1651 } else if (ProblemParam
) {
1653 // this was a non-recognised flag... error!
1655 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1656 ASSERT(*ProblemParam
!= NULL
);
1657 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1658 ShellCommandLineFreeVarList(*CheckPackage
);
1659 *CheckPackage
= NULL
;
1660 return (EFI_VOLUME_CORRUPTED
);
1662 ShellCommandLineFreeVarList(*CheckPackage
);
1663 *CheckPackage
= NULL
;
1664 return (EFI_VOLUME_CORRUPTED
);
1668 // support for AutoPageBreak
1670 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1671 ShellSetPageBreakMode(TRUE
);
1673 return (EFI_SUCCESS
);
1677 Checks the command line arguments passed against the list of valid ones.
1678 Optionally removes NULL values first.
1680 If no initialization is required, then return RETURN_SUCCESS.
1682 @param CheckList pointer to list of parameters to check
1683 @param CheckPackage pointer to pointer to list checked values
1684 @param ProblemParam optional pointer to pointer to unicode string for
1685 the paramater that caused failure.
1686 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1688 @retval EFI_SUCCESS The operation completed sucessfully.
1689 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1690 @retval EFI_INVALID_PARAMETER A parameter was invalid
1691 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1692 duplicated. the duplicated command line argument
1693 was returned in ProblemParam if provided.
1694 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1695 of the command line arguments was returned in
1696 ProblemParam if provided.
1697 @retval EFI_NOT_FOUND a argument required a value that was missing.
1698 the invalid command line argument was returned in
1699 ProblemParam if provided.
1703 ShellCommandLineParse (
1704 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1705 OUT LIST_ENTRY
**CheckPackage
,
1706 OUT CHAR16
**ProblemParam OPTIONAL
,
1707 IN BOOLEAN AutoPageBreak
1711 // ASSERT that CheckList and CheckPackage aren't NULL
1713 ASSERT(CheckList
!= NULL
);
1714 ASSERT(CheckPackage
!= NULL
);
1717 // Check for UEFI Shell 2.0 protocols
1719 if (mEfiShellParametersProtocol
!= NULL
) {
1720 return (InternalCommandLineParse(CheckList
,
1724 (CONST CHAR16
**) mEfiShellParametersProtocol
->Argv
,
1725 mEfiShellParametersProtocol
->Argc
));
1729 // ASSERT That EFI Shell is not required
1731 ASSERT (mEfiShellInterface
!= NULL
);
1732 return (InternalCommandLineParse(CheckList
,
1736 (CONST CHAR16
**) mEfiShellInterface
->Argv
,
1737 mEfiShellInterface
->Argc
));
1741 Frees shell variable list that was returned from ShellCommandLineParse.
1743 This function will free all the memory that was used for the CheckPackage
1744 list of postprocessed shell arguments.
1746 this function has no return value.
1748 if CheckPackage is NULL, then return
1750 @param CheckPackage the list to de-allocate
1754 ShellCommandLineFreeVarList (
1755 IN LIST_ENTRY
*CheckPackage
1761 // check for CheckPackage == NULL
1763 if (CheckPackage
== NULL
) {
1768 // for each node in the list
1770 for (Node
= GetFirstNode(CheckPackage
); Node
!= CheckPackage
; Node
= GetFirstNode(CheckPackage
)) {
1772 // Remove it from the list
1774 RemoveEntryList(Node
);
1777 // if it has a name free the name
1779 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1780 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1784 // if it has a value free the value
1786 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
1787 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1791 // free the node structure
1793 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
1796 // free the list head node
1798 FreePool(CheckPackage
);
1801 Checks for presence of a flag parameter
1803 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1805 if CheckPackage is NULL then return FALSE.
1806 if KeyString is NULL then ASSERT()
1808 @param CheckPackage The package of parsed command line arguments
1809 @param KeyString the Key of the command line argument to check for
1811 @retval TRUE the flag is on the command line
1812 @retval FALSE the flag is not on the command line
1816 ShellCommandLineGetFlag (
1817 IN CONST LIST_ENTRY
*CheckPackage
,
1818 IN CHAR16
*KeyString
1824 // ASSERT that both CheckPackage and KeyString aren't NULL
1826 ASSERT(KeyString
!= NULL
);
1829 // return FALSE for no package
1831 if (CheckPackage
== NULL
) {
1836 // enumerate through the list of parametrs
1838 for (Node
= GetFirstNode(CheckPackage
) ; !IsNull (CheckPackage
, Node
) ; Node
= GetNextNode(CheckPackage
, Node
) ) {
1840 // If the Name matches, return TRUE (and there may be NULL name)
1842 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1843 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1851 returns value from command line argument
1853 value parameters are in the form of "-<Key> value" or "/<Key> value"
1855 if CheckPackage is NULL, then return NULL;
1857 @param CheckPackage The package of parsed command line arguments
1858 @param KeyString the Key of the command line argument to check for
1860 @retval NULL the flag is not on the command line
1861 @return !=NULL pointer to unicode string of the value
1865 ShellCommandLineGetValue (
1866 IN CONST LIST_ENTRY
*CheckPackage
,
1867 IN CHAR16
*KeyString
1873 // check for CheckPackage == NULL
1875 if (CheckPackage
== NULL
) {
1880 // enumerate through the list of parametrs
1882 for (Node
= GetFirstNode(CheckPackage
) ; !IsNull (CheckPackage
, Node
) ; Node
= GetNextNode(CheckPackage
, Node
) ) {
1884 // If the Name matches, return the value (name can be NULL)
1886 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1887 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1888 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1895 returns raw value from command line argument
1897 raw value parameters are in the form of "value" in a specific position in the list
1899 if CheckPackage is NULL, then return NULL;
1901 @param CheckPackage The package of parsed command line arguments
1902 @param Position the position of the value
1904 @retval NULL the flag is not on the command line
1905 @return !=NULL pointer to unicode string of the value
1909 ShellCommandLineGetRawValue (
1910 IN CONST LIST_ENTRY
*CheckPackage
,
1917 // check for CheckPackage == NULL
1919 if (CheckPackage
== NULL
) {
1924 // enumerate through the list of parametrs
1926 for ( Node
= GetFirstNode(CheckPackage
)
1927 ; !IsNull (CheckPackage
, Node
)
1928 ; Node
= GetNextNode(CheckPackage
, Node
)
1931 // If the position matches, return the value
1933 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
1934 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1940 This is a find and replace function. it will return the NewString as a copy of
1941 SourceString with each instance of FindTarget replaced with ReplaceWith.
1943 If the string would grow bigger than NewSize it will halt and return error.
1945 @param[in] SourceString String with source buffer
1946 @param[in,out] NewString String with resultant buffer
1947 @param[in] NewSize Size in bytes of NewString
1948 @param[in] FindTarget String to look for
1949 @param[in[ ReplaceWith String to replace FindTarget with
1951 @retval EFI_INVALID_PARAMETER SourceString was NULL
1952 @retval EFI_INVALID_PARAMETER NewString was NULL
1953 @retval EFI_INVALID_PARAMETER FindTarget was NULL
1954 @retval EFI_INVALID_PARAMETER ReplaceWith was NULL
1955 @retval EFI_INVALID_PARAMETER FindTarget had length < 1
1956 @retval EFI_INVALID_PARAMETER SourceString had length < 1
1957 @retval EFI_BUFFER_TOO_SMALL NewSize was less than the minimum size to hold
1958 the new string (truncation occurred)
1959 @retval EFI_SUCCESS the string was sucessfully copied with replacement
1965 IN CHAR16 CONST
*SourceString
,
1966 IN CHAR16
*NewString
,
1968 IN CONST CHAR16
*FindTarget
,
1969 IN CONST CHAR16
*ReplaceWith
1971 if ( (SourceString
== NULL
)
1972 || (NewString
== NULL
)
1973 || (FindTarget
== NULL
)
1974 || (ReplaceWith
== NULL
)
1975 || (StrLen(FindTarget
) < 1)
1976 || (StrLen(SourceString
) < 1)
1978 return (EFI_INVALID_PARAMETER
);
1980 NewString
= SetMem16(NewString
, NewSize
, L
'\0');
1981 while (*SourceString
!= L
'\0') {
1982 if (StrnCmp(SourceString
, FindTarget
, StrLen(FindTarget
)) == 0) {
1983 SourceString
+= StrLen(FindTarget
);
1984 if (StrSize(NewString
) + (StrLen(ReplaceWith
)*sizeof(CHAR16
)) > NewSize
) {
1985 return (EFI_BUFFER_TOO_SMALL
);
1987 StrCat(NewString
, ReplaceWith
);
1989 if (StrSize(NewString
) + sizeof(CHAR16
) > NewSize
) {
1990 return (EFI_BUFFER_TOO_SMALL
);
1992 StrnCat(NewString
, SourceString
, 1);
1996 return (EFI_SUCCESS
);
2000 Print at a specific location on the screen.
2002 This function will move the cursor to a given screen location and print the specified string
2004 If -1 is specified for either the Row or Col the current screen location for BOTH
2007 if either Row or Col is out of range for the current console, then ASSERT
2008 if Format is NULL, then ASSERT
2010 In addition to the standard %-based flags as supported by UefiLib Print() this supports
2011 the following additional flags:
2012 %N - Set output attribute to normal
2013 %H - Set output attribute to highlight
2014 %E - Set output attribute to error
2015 %B - Set output attribute to blue color
2016 %V - Set output attribute to green color
2018 Note: The background color is controlled by the shell command cls.
2020 @param[in] Row the row to print at
2021 @param[in] Col the column to print at
2022 @param[in] Format the format string
2024 @return the number of characters printed to the screen
2030 IN INT32 Col OPTIONAL
,
2031 IN INT32 Row OPTIONAL
,
2032 IN CONST CHAR16
*Format
,
2037 CHAR16
*PostReplaceFormat
;
2038 CHAR16
*PostReplaceFormat2
;
2042 UINTN NormalAttribute
;
2043 CHAR16
*ResumeLocation
;
2044 CHAR16
*FormatWalker
;
2046 VA_START (Marker
, Format
);
2048 BufferSize
= (PcdGet32 (PcdUefiLibMaxPrintBufferSize
) + 1) * sizeof (CHAR16
);
2049 PostReplaceFormat
= AllocateZeroPool (BufferSize
);
2050 ASSERT (PostReplaceFormat
!= NULL
);
2051 PostReplaceFormat2
= AllocateZeroPool (BufferSize
);
2052 ASSERT (PostReplaceFormat2
!= NULL
);
2055 // Back and forth each time fixing up 1 of our flags...
2057 Status
= CopyReplace(Format
, PostReplaceFormat
, BufferSize
, L
"%N", L
"%%N");
2058 ASSERT_EFI_ERROR(Status
);
2059 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%E", L
"%%E");
2060 ASSERT_EFI_ERROR(Status
);
2061 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%H", L
"%%H");
2062 ASSERT_EFI_ERROR(Status
);
2063 Status
= CopyReplace(PostReplaceFormat
, PostReplaceFormat2
, BufferSize
, L
"%B", L
"%%B");
2064 ASSERT_EFI_ERROR(Status
);
2065 Status
= CopyReplace(PostReplaceFormat2
, PostReplaceFormat
, BufferSize
, L
"%V", L
"%%V");
2066 ASSERT_EFI_ERROR(Status
);
2069 // Use the last buffer from replacing to print from...
2071 Return
= UnicodeVSPrint (PostReplaceFormat2
, BufferSize
, PostReplaceFormat
, Marker
);
2073 FreePool(PostReplaceFormat
);
2075 if (Col
!= -1 && Row
!= -1) {
2076 Status
= gST
->ConOut
->SetCursorPosition(gST
->ConOut
, Col
, Row
);
2077 ASSERT_EFI_ERROR(Status
);
2080 NormalAttribute
= gST
->ConOut
->Mode
->Attribute
;
2081 FormatWalker
= PostReplaceFormat2
;
2082 while (*FormatWalker
!= L
'\0') {
2084 // Find the next attribute change request
2086 ResumeLocation
= StrStr(FormatWalker
, L
"%");
2087 if (ResumeLocation
!= NULL
) {
2088 *ResumeLocation
= L
'\0';
2091 // print the current FormatWalker string
2093 Status
= gST
->ConOut
->OutputString(gST
->ConOut
, FormatWalker
);
2094 ASSERT_EFI_ERROR(Status
);
2096 // update the attribute
2098 if (ResumeLocation
!= NULL
) {
2099 switch (*(ResumeLocation
+1)) {
2101 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2104 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_YELLOW
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2107 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_WHITE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2110 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_BLUE
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2113 gST
->ConOut
->SetAttribute(gST
->ConOut
, EFI_TEXT_ATTR(EFI_GREEN
, ((NormalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)));
2121 // reset to normal now...
2123 gST
->ConOut
->SetAttribute(gST
->ConOut
, NormalAttribute
);
2128 // update FormatWalker to Resume + 2 (skip the % and the indicator)
2130 FormatWalker
= ResumeLocation
+ 2;
2133 FreePool(PostReplaceFormat2
);