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 <Protocol/EfiShellEnvironment2.h>
26 #include <Protocol/EfiShellInterface.h>
27 #include <Protocol/EfiShell.h>
28 #include <Protocol/EfiShellParameters.h>
29 #include <Protocol/SimpleFileSystem.h>
31 #include "BaseShellLib.h"
33 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
34 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
37 // This is not static since it's extern in the .h file
39 SHELL_PARAM_ITEM EmptyParamList
[] = {
44 // Static file globals for the shell library
46 STATIC EFI_SHELL_ENVIRONMENT2
*mEfiShellEnvironment2
;
47 STATIC EFI_SHELL_INTERFACE
*mEfiShellInterface
;
48 STATIC EFI_SHELL_PROTOCOL
*mEfiShellProtocol
;
49 STATIC EFI_SHELL_PARAMETERS_PROTOCOL
*mEfiShellParametersProtocol
;
50 STATIC EFI_HANDLE mEfiShellEnvironment2Handle
;
51 STATIC FILE_HANDLE_FUNCTION_MAP FileFunctionMap
;
54 helper function to find ShellEnvironment2 for constructor
59 IN EFI_HANDLE ImageHandle
69 Status
= gBS
->OpenProtocol(ImageHandle
,
70 &gEfiShellEnvironment2Guid
,
71 (VOID
**)&mEfiShellEnvironment2
,
74 EFI_OPEN_PROTOCOL_GET_PROTOCOL
77 // look for the mEfiShellEnvironment2 protocol at a higher level
79 if (EFI_ERROR (Status
) || !(CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
&&
80 (mEfiShellEnvironment2
->MajorVersion
> EFI_SHELL_MAJOR_VER
||
81 (mEfiShellEnvironment2
->MajorVersion
== EFI_SHELL_MAJOR_VER
&& mEfiShellEnvironment2
->MinorVersion
>= EFI_SHELL_MINOR_VER
)))) {
83 // figure out how big of a buffer we need.
85 Status
= gBS
->LocateHandle (ByProtocol
,
86 &gEfiShellEnvironment2Guid
,
87 NULL
, // ignored for ByProtocol
91 ASSERT(Status
== EFI_BUFFER_TOO_SMALL
);
92 Buffer
= (EFI_HANDLE
*)AllocatePool(BufferSize
);
93 ASSERT(Buffer
!= NULL
);
94 Status
= gBS
->LocateHandle (ByProtocol
,
95 &gEfiShellEnvironment2Guid
,
96 NULL
, // ignored for ByProtocol
100 if (!EFI_ERROR (Status
)) {
102 // now parse the list of returned handles
104 Status
= EFI_NOT_FOUND
;
105 for (HandleIndex
= 0; HandleIndex
< (BufferSize
/sizeof(Buffer
[0])); HandleIndex
++) {
106 Status
= gBS
->OpenProtocol(Buffer
[HandleIndex
],
107 &gEfiShellEnvironment2Guid
,
108 (VOID
**)&mEfiShellEnvironment2
,
111 EFI_OPEN_PROTOCOL_GET_PROTOCOL
113 if (CompareGuid (&mEfiShellEnvironment2
->SESGuid
, &gEfiShellEnvironment2ExtGuid
) != FALSE
&&
114 (mEfiShellEnvironment2
->MajorVersion
> EFI_SHELL_MAJOR_VER
||
115 (mEfiShellEnvironment2
->MajorVersion
== EFI_SHELL_MAJOR_VER
&& mEfiShellEnvironment2
->MinorVersion
>= EFI_SHELL_MINOR_VER
))) {
116 mEfiShellEnvironment2Handle
= Buffer
[HandleIndex
];
117 Status
= EFI_SUCCESS
;
123 if (Buffer
!= NULL
) {
131 ShellLibConstructorWorker (
132 IN EFI_HANDLE ImageHandle
,
133 IN EFI_SYSTEM_TABLE
*SystemTable
138 // UEFI 2.0 shell interfaces (used preferentially)
140 Status
= gBS
->OpenProtocol(ImageHandle
,
141 &gEfiShellProtocolGuid
,
142 (VOID
**)&mEfiShellProtocol
,
145 EFI_OPEN_PROTOCOL_GET_PROTOCOL
147 if (EFI_ERROR(Status
)) {
148 mEfiShellProtocol
= NULL
;
150 Status
= gBS
->OpenProtocol(ImageHandle
,
151 &gEfiShellParametersProtocolGuid
,
152 (VOID
**)&mEfiShellParametersProtocol
,
155 EFI_OPEN_PROTOCOL_GET_PROTOCOL
157 if (EFI_ERROR(Status
)) {
158 mEfiShellParametersProtocol
= NULL
;
161 if (mEfiShellParametersProtocol
== NULL
|| mEfiShellProtocol
== NULL
) {
163 // Moved to seperate function due to complexity
165 Status
= ShellFindSE2(ImageHandle
);
167 if (EFI_ERROR(Status
)) {
168 DEBUG((DEBUG_ERROR
, "Status: 0x%08x\r\n", Status
));
169 mEfiShellEnvironment2
= NULL
;
171 Status
= gBS
->OpenProtocol(ImageHandle
,
172 &gEfiShellInterfaceGuid
,
173 (VOID
**)&mEfiShellInterface
,
176 EFI_OPEN_PROTOCOL_GET_PROTOCOL
178 if (EFI_ERROR(Status
)) {
179 mEfiShellInterface
= NULL
;
183 // only success getting 2 of either the old or new, but no 1/2 and 1/2
185 if ((mEfiShellEnvironment2
!= NULL
&& mEfiShellInterface
!= NULL
) ||
186 (mEfiShellProtocol
!= NULL
&& mEfiShellParametersProtocol
!= NULL
) ) {
187 if (mEfiShellProtocol
!= NULL
) {
188 FileFunctionMap
.GetFileInfo
= mEfiShellProtocol
->GetFileInfo
;
189 FileFunctionMap
.SetFileInfo
= mEfiShellProtocol
->SetFileInfo
;
190 FileFunctionMap
.ReadFile
= mEfiShellProtocol
->ReadFile
;
191 FileFunctionMap
.WriteFile
= mEfiShellProtocol
->WriteFile
;
192 FileFunctionMap
.CloseFile
= mEfiShellProtocol
->CloseFile
;
193 FileFunctionMap
.DeleteFile
= mEfiShellProtocol
->DeleteFile
;
194 FileFunctionMap
.GetFilePosition
= mEfiShellProtocol
->GetFilePosition
;
195 FileFunctionMap
.SetFilePosition
= mEfiShellProtocol
->SetFilePosition
;
196 FileFunctionMap
.FlushFile
= mEfiShellProtocol
->FlushFile
;
197 FileFunctionMap
.GetFileSize
= mEfiShellProtocol
->GetFileSize
;
199 FileFunctionMap
.GetFileInfo
= FileHandleGetInfo
;
200 FileFunctionMap
.SetFileInfo
= FileHandleSetInfo
;
201 FileFunctionMap
.ReadFile
= FileHandleRead
;
202 FileFunctionMap
.WriteFile
= FileHandleWrite
;
203 FileFunctionMap
.CloseFile
= FileHandleClose
;
204 FileFunctionMap
.DeleteFile
= FileHandleDelete
;
205 FileFunctionMap
.GetFilePosition
= FileHandleGetPosition
;
206 FileFunctionMap
.SetFilePosition
= FileHandleSetPosition
;
207 FileFunctionMap
.FlushFile
= FileHandleFlush
;
208 FileFunctionMap
.GetFileSize
= FileHandleGetSize
;
210 return (EFI_SUCCESS
);
212 return (EFI_NOT_FOUND
);
215 Constructor for the Shell library.
217 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
219 @param ImageHandle the image handle of the process
220 @param SystemTable the EFI System Table pointer
222 @retval EFI_SUCCESS the initialization was complete sucessfully
223 @return others an error ocurred during initialization
227 ShellLibConstructor (
228 IN EFI_HANDLE ImageHandle
,
229 IN EFI_SYSTEM_TABLE
*SystemTable
234 mEfiShellEnvironment2
= NULL
;
235 mEfiShellProtocol
= NULL
;
236 mEfiShellParametersProtocol
= NULL
;
237 mEfiShellInterface
= NULL
;
238 mEfiShellEnvironment2Handle
= NULL
;
240 ///@todo make a worker constructor so initialize function works
242 // verify that auto initialize is not set false
244 if (PcdGetBool(PcdShellLibAutoInitialize
) == 0) {
245 return (EFI_SUCCESS
);
248 return (ShellLibConstructorWorker(ImageHandle
, SystemTable
));
252 Destructory for the library. free any resources.
257 IN EFI_HANDLE ImageHandle
,
258 IN EFI_SYSTEM_TABLE
*SystemTable
261 if (mEfiShellEnvironment2
!= NULL
) {
262 gBS
->CloseProtocol(mEfiShellEnvironment2Handle
==NULL
?ImageHandle
:mEfiShellEnvironment2Handle
,
263 &gEfiShellEnvironment2Guid
,
266 mEfiShellEnvironment2
= NULL
;
268 if (mEfiShellInterface
!= NULL
) {
269 gBS
->CloseProtocol(ImageHandle
,
270 &gEfiShellInterfaceGuid
,
273 mEfiShellInterface
= NULL
;
275 if (mEfiShellProtocol
!= NULL
) {
276 gBS
->CloseProtocol(ImageHandle
,
277 &gEfiShellProtocolGuid
,
280 mEfiShellProtocol
= NULL
;
282 if (mEfiShellParametersProtocol
!= NULL
) {
283 gBS
->CloseProtocol(ImageHandle
,
284 &gEfiShellParametersProtocolGuid
,
287 mEfiShellParametersProtocol
= NULL
;
289 mEfiShellEnvironment2Handle
= NULL
;
290 return (EFI_SUCCESS
);
294 This function causes the shell library to initialize itself. If the shell library
295 is already initialized it will de-initialize all the current protocol poitners and
296 re-populate them again.
298 When the library is used with PcdShellLibAutoInitialize set to true this function
299 will return EFI_SUCCESS and perform no actions.
301 This function is intended for internal access for shell commands only.
303 @retval EFI_SUCCESS the initialization was complete sucessfully
311 // if auto initialize is not false then skip
313 if (PcdGetBool(PcdShellLibAutoInitialize
) != 0) {
314 return (EFI_SUCCESS
);
318 // deinit the current stuff
320 ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle
, gST
));
323 // init the new stuff
325 return (ShellLibConstructorWorker(gImageHandle
, gST
));
329 This function will retrieve the information about the file for the handle
330 specified and store it in allocated pool memory.
332 This function allocates a buffer to store the file's information. It is the
333 caller's responsibility to free the buffer
335 @param FileHandle The file handle of the file for which information is
338 @retval NULL information could not be retrieved.
340 @return the information about the file
345 IN EFI_FILE_HANDLE FileHandle
348 return (FileFunctionMap
.GetFileInfo(FileHandle
));
352 This function will set the information about the file for the opened handle
355 @param FileHandle The file handle of the file for which information
358 @param FileInfo The infotmation to set.
360 @retval EFI_SUCCESS The information was set.
361 @retval EFI_UNSUPPORTED The InformationType is not known.
362 @retval EFI_NO_MEDIA The device has no medium.
363 @retval EFI_DEVICE_ERROR The device reported an error.
364 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
365 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
366 @retval EFI_ACCESS_DENIED The file was opened read only.
367 @retval EFI_VOLUME_FULL The volume is full.
372 IN EFI_FILE_HANDLE FileHandle
,
373 IN EFI_FILE_INFO
*FileInfo
376 return (FileFunctionMap
.SetFileInfo(FileHandle
, FileInfo
));
380 This function will open a file or directory referenced by DevicePath.
382 This function opens a file with the open mode according to the file path. The
383 Attributes is valid only for EFI_FILE_MODE_CREATE.
385 @param FilePath on input the device path to the file. On output
386 the remaining device path.
387 @param DeviceHandle pointer to the system device handle.
388 @param FileHandle pointer to the file handle.
389 @param OpenMode the mode to open the file with.
390 @param Attributes the file's file attributes.
392 @retval EFI_SUCCESS The information was set.
393 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
394 @retval EFI_UNSUPPORTED Could not open the file path.
395 @retval EFI_NOT_FOUND The specified file could not be found on the
396 device or the file system could not be found on
398 @retval EFI_NO_MEDIA The device has no medium.
399 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
400 medium is no longer supported.
401 @retval EFI_DEVICE_ERROR The device reported an error.
402 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
403 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
404 @retval EFI_ACCESS_DENIED The file was opened read only.
405 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
407 @retval EFI_VOLUME_FULL The volume is full.
411 ShellOpenFileByDevicePath(
412 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
413 OUT EFI_HANDLE
*DeviceHandle
,
414 OUT EFI_FILE_HANDLE
*FileHandle
,
421 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
422 EFI_FILE_HANDLE LastHandle
;
425 // ASERT for FileHandle, FilePath, and DeviceHandle being NULL
427 ASSERT(FilePath
!= NULL
);
428 ASSERT(FileHandle
!= NULL
);
429 ASSERT(DeviceHandle
!= NULL
);
431 // which shell interface should we use
433 if (mEfiShellProtocol
!= NULL
) {
435 // use UEFI Shell 2.0 method.
437 FileName
= mEfiShellProtocol
->GetFilePathFromDevicePath(*FilePath
);
438 if (FileName
== NULL
) {
439 return (EFI_INVALID_PARAMETER
);
441 Status
= ShellOpenFileByName(FileName
, FileHandle
, OpenMode
, Attributes
);
448 // use old shell method.
450 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
,
453 if (EFI_ERROR (Status
)) {
456 Status
= gBS
->OpenProtocol(*DeviceHandle
,
457 &gEfiSimpleFileSystemProtocolGuid
,
458 &EfiSimpleFileSystemProtocol
,
461 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
462 if (EFI_ERROR (Status
)) {
465 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, FileHandle
);
466 if (EFI_ERROR (Status
)) {
472 // go down directories one node at a time.
474 while (!IsDevicePathEnd (*FilePath
)) {
476 // For file system access each node should be a file path component
478 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
479 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
482 return (EFI_INVALID_PARAMETER
);
485 // Open this file path node
487 LastHandle
= *FileHandle
;
491 // Try to test opening an existing file
493 Status
= LastHandle
->Open (
496 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
497 OpenMode
&~EFI_FILE_MODE_CREATE
,
502 // see if the error was that it needs to be created
504 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
505 Status
= LastHandle
->Open (
508 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
514 // Close the last node
516 LastHandle
->Close (LastHandle
);
518 if (EFI_ERROR(Status
)) {
525 *FilePath
= NextDevicePathNode (*FilePath
);
527 return (EFI_SUCCESS
);
531 This function will open a file or directory referenced by filename.
533 If return is EFI_SUCCESS, the Filehandle is the opened file's handle;
534 otherwise, the Filehandle is NULL. The Attributes is valid only for
535 EFI_FILE_MODE_CREATE.
537 if FileNAme is NULL then ASSERT()
539 @param FileName pointer to file name
540 @param FileHandle pointer to the file handle.
541 @param OpenMode the mode to open the file with.
542 @param Attributes the file's file attributes.
544 @retval EFI_SUCCESS The information was set.
545 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
546 @retval EFI_UNSUPPORTED Could not open the file path.
547 @retval EFI_NOT_FOUND The specified file could not be found on the
548 device or the file system could not be found
550 @retval EFI_NO_MEDIA The device has no medium.
551 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
552 medium is no longer supported.
553 @retval EFI_DEVICE_ERROR The device reported an error.
554 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
555 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
556 @retval EFI_ACCESS_DENIED The file was opened read only.
557 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
559 @retval EFI_VOLUME_FULL The volume is full.
565 OUT EFI_FILE_HANDLE
*FileHandle
,
570 EFI_HANDLE DeviceHandle
;
571 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
574 // ASSERT if FileName is NULL
576 ASSERT(FileName
!= NULL
);
578 if (mEfiShellProtocol
!= NULL
) {
580 // Use UEFI Shell 2.0 method
582 return (mEfiShellProtocol
->OpenFileByName(FileName
,
586 ///@todo add the attributes
589 // Using EFI Shell version
590 // this means convert name to path and call that function
591 // since this will use EFI method again that will open it.
593 ASSERT(mEfiShellEnvironment2
!= NULL
);
594 FilePath
= mEfiShellEnvironment2
->NameToPath (FileName
);
595 if (FileDevicePath
!= NULL
) {
596 return (ShellOpenFileByDevicePath(&FilePath
,
602 return (EFI_DEVICE_ERROR
);
605 This function create a directory
607 If return is EFI_SUCCESS, the Filehandle is the opened directory's handle;
608 otherwise, the Filehandle is NULL. If the directory already existed, this
609 function opens the existing directory.
611 @param DirectoryName pointer to directory name
612 @param FileHandle pointer to the file handle.
614 @retval EFI_SUCCESS The information was set.
615 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
616 @retval EFI_UNSUPPORTED Could not open the file path.
617 @retval EFI_NOT_FOUND The specified file could not be found on the
618 device or the file system could not be found
620 @retval EFI_NO_MEDIA The device has no medium.
621 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
622 medium is no longer supported.
623 @retval EFI_DEVICE_ERROR The device reported an error.
624 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
625 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
626 @retval EFI_ACCESS_DENIED The file was opened read only.
627 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
629 @retval EFI_VOLUME_FULL The volume is full.
630 @sa ShellOpenFileByName
634 ShellCreateDirectory(
635 IN CHAR16
*DirectoryName
,
636 OUT EFI_FILE_HANDLE
*FileHandle
640 // this is a pass thru to the open file function with sepcific open mode and attributes
642 return (ShellOpenFileByName(DirectoryName
,
644 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
650 This function reads information from an opened file.
652 If FileHandle is not a directory, the function reads the requested number of
653 bytes from the file at the file's current position and returns them in Buffer.
654 If the read goes beyond the end of the file, the read length is truncated to the
655 end of the file. The file's current position is increased by the number of bytes
656 returned. If FileHandle is a directory, the function reads the directory entry
657 at the file's current position and returns the entry in Buffer. If the Buffer
658 is not large enough to hold the current directory entry, then
659 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
660 BufferSize is set to be the size of the buffer needed to read the entry. On
661 success, the current position is updated to the next directory entry. If there
662 are no more directory entries, the read returns a zero-length buffer.
663 EFI_FILE_INFO is the structure returned as the directory entry.
665 @param FileHandle the opened file handle
666 @param BufferSize on input the size of buffer in bytes. on return
667 the number of bytes written.
668 @param Buffer the buffer to put read data into.
670 @retval EFI_SUCCESS Data was read.
671 @retval EFI_NO_MEDIA The device has no media.
672 @retval EFI_DEVICE_ERROR The device reported an error.
673 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
674 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
681 IN EFI_FILE_HANDLE FileHandle
,
682 IN OUT UINTN
*BufferSize
,
686 return (FileFunctionMap
.ReadFile(FileHandle
, BufferSize
, Buffer
));
691 Write data to a file.
693 This function writes the specified number of bytes to the file at the current
694 file position. The current file position is advanced the actual number of bytes
695 written, which is returned in BufferSize. Partial writes only occur when there
696 has been a data error during the write attempt (such as "volume space full").
697 The file is automatically grown to hold the data if required. Direct writes to
698 opened directories are not supported.
700 @param FileHandle The opened file for writing
701 @param BufferSize on input the number of bytes in Buffer. On output
702 the number of bytes written.
703 @param Buffer the buffer containing data to write is stored.
705 @retval EFI_SUCCESS Data was written.
706 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
707 @retval EFI_NO_MEDIA The device has no media.
708 @retval EFI_DEVICE_ERROR The device reported an error.
709 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
710 @retval EFI_WRITE_PROTECTED The device is write-protected.
711 @retval EFI_ACCESS_DENIED The file was open for read only.
712 @retval EFI_VOLUME_FULL The volume is full.
717 IN EFI_FILE_HANDLE FileHandle
,
718 IN OUT UINTN
*BufferSize
,
722 return (FileFunctionMap
.WriteFile(FileHandle
, BufferSize
, Buffer
));
726 Close an open file handle.
728 This function closes a specified file handle. All "dirty" cached file data is
729 flushed to the device, and the file is closed. In all cases the handle is
732 @param FileHandle the file handle to close.
734 @retval EFI_SUCCESS the file handle was closed sucessfully.
739 IN EFI_FILE_HANDLE
*FileHandle
742 return (FileFunctionMap
.CloseFile(*FileHandle
));
746 Delete a file and close the handle
748 This function closes and deletes a file. In all cases the file handle is closed.
749 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
750 returned, but the handle is still closed.
752 @param FileHandle the file handle to delete
754 @retval EFI_SUCCESS the file was closed sucessfully
755 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
757 @retval INVALID_PARAMETER One of the parameters has an invalid value.
762 IN EFI_FILE_HANDLE
*FileHandle
765 return (FileFunctionMap
.DeleteFile(*FileHandle
));
769 Set the current position in a file.
771 This function sets the current file position for the handle to the position
772 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
773 absolute positioning is supported, and seeking past the end of the file is
774 allowed (a subsequent write would grow the file). Seeking to position
775 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
776 If FileHandle is a directory, the only position that may be set is zero. This
777 has the effect of starting the read process of the directory entries over.
779 @param FileHandle The file handle on which the position is being set
780 @param Position Byte position from begining of file
782 @retval EFI_SUCCESS Operation completed sucessfully.
783 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
785 @retval INVALID_PARAMETER One of the parameters has an invalid value.
789 ShellSetFilePosition (
790 IN EFI_FILE_HANDLE FileHandle
,
794 return (FileFunctionMap
.SetFilePosition(FileHandle
, Position
));
798 Gets a file's current position
800 This function retrieves the current file position for the file handle. For
801 directories, the current file position has no meaning outside of the file
802 system driver and as such the operation is not supported. An error is returned
803 if FileHandle is a directory.
805 @param FileHandle The open file handle on which to get the position.
806 @param Position Byte position from begining of file.
808 @retval EFI_SUCCESS the operation completed sucessfully.
809 @retval INVALID_PARAMETER One of the parameters has an invalid value.
810 @retval EFI_UNSUPPORTED the request is not valid on directories.
814 ShellGetFilePosition (
815 IN EFI_FILE_HANDLE FileHandle
,
819 return (FileFunctionMap
.GetFilePosition(FileHandle
, Position
));
822 Flushes data on a file
824 This function flushes all modified data associated with a file to a device.
826 @param FileHandle The file handle on which to flush data
828 @retval EFI_SUCCESS The data was flushed.
829 @retval EFI_NO_MEDIA The device has no media.
830 @retval EFI_DEVICE_ERROR The device reported an error.
831 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
832 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
833 @retval EFI_ACCESS_DENIED The file was opened for read only.
838 IN EFI_FILE_HANDLE FileHandle
841 return (FileFunctionMap
.FlushFile(FileHandle
));
845 Retrieves the first file from a directory
847 This function opens a directory and gets the first file's info in the
848 directory. Caller can use ShellFindNextFile() to get other files. When
849 complete the caller is responsible for calling FreePool() on Buffer.
851 @param DirHandle The file handle of the directory to search
852 @param Buffer Pointer to buffer for file's information
854 @retval EFI_SUCCESS Found the first file.
855 @retval EFI_NOT_FOUND Cannot find the directory.
856 @retval EFI_NO_MEDIA The device has no media.
857 @retval EFI_DEVICE_ERROR The device reported an error.
858 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
859 @return Others status of ShellGetFileInfo, ShellSetFilePosition,
865 IN EFI_FILE_HANDLE DirHandle
,
866 OUT EFI_FILE_INFO
**Buffer
870 // pass to file handle lib
872 return (FileHandleFindFirstFile(DirHandle
, Buffer
));
875 Retrieves the next file in a directory.
877 To use this function, caller must call the LibFindFirstFile() to get the
878 first file, and then use this function get other files. This function can be
879 called for several times to get each file's information in the directory. If
880 the call of ShellFindNextFile() got the last file in the directory, the next
881 call of this function has no file to get. *NoFile will be set to TRUE and the
882 Buffer memory will be automatically freed.
884 @param DirHandle the file handle of the directory
885 @param Buffer pointer to buffer for file's information
886 @param NoFile pointer to boolean when last file is found
888 @retval EFI_SUCCESS Found the next file, or reached last file
889 @retval EFI_NO_MEDIA The device has no media.
890 @retval EFI_DEVICE_ERROR The device reported an error.
891 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
896 IN EFI_FILE_HANDLE DirHandle
,
897 OUT EFI_FILE_INFO
*Buffer
,
902 // pass to file handle lib
904 return (FileHandleFindNextFile(DirHandle
, Buffer
, NoFile
));
907 Retrieve the size of a file.
909 if FileHandle is NULL then ASSERT()
910 if Size is NULL then ASSERT()
912 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
915 @param FileHandle file handle from which size is retrieved
916 @param Size pointer to size
918 @retval EFI_SUCCESS operation was completed sucessfully
919 @retval EFI_DEVICE_ERROR cannot access the file
924 IN EFI_FILE_HANDLE FileHandle
,
928 return (FileFunctionMap
.GetFileSize(FileHandle
, Size
));
931 Retrieves the status of the break execution flag
933 this function is useful to check whether the application is being asked to halt by the shell.
935 @retval TRUE the execution break is enabled
936 @retval FALSE the execution break is not enabled
940 ShellGetExecutionBreakFlag(
945 // Check for UEFI Shell 2.0 protocols
947 if (mEfiShellProtocol
!= NULL
) {
950 // We are using UEFI Shell 2.0; see if the event has been triggered
952 if (gBS
->CheckEvent(mEfiShellProtocol
->ExecutionBreak
) != EFI_SUCCESS
) {
959 // using EFI Shell; call the function to check
961 ASSERT(mEfiShellEnvironment2
!= NULL
);
962 return (mEfiShellEnvironment2
->GetExecutionBreak());
965 return the value of an environment variable
967 this function gets the value of the environment variable set by the
968 ShellSetEnvironmentVariable function
970 @param EnvKey The key name of the environment variable.
972 @retval NULL the named environment variable does not exist.
973 @return != NULL pointer to the value of the environment variable
977 ShellGetEnvironmentVariable (
982 // Check for UEFI Shell 2.0 protocols
984 if (mEfiShellProtocol
!= NULL
) {
985 return (mEfiShellProtocol
->GetEnv(EnvKey
));
989 // ASSERT that we must have EFI shell
991 ASSERT(mEfiShellEnvironment2
!= NULL
);
996 return (mEfiShellEnvironment2
->GetEnv(EnvKey
));
999 set the value of an environment variable
1001 This function changes the current value of the specified environment variable. If the
1002 environment variable exists and the Value is an empty string, then the environment
1003 variable is deleted. If the environment variable exists and the Value is not an empty
1004 string, then the value of the environment variable is changed. If the environment
1005 variable does not exist and the Value is an empty string, there is no action. If the
1006 environment variable does not exist and the Value is a non-empty string, then the
1007 environment variable is created and assigned the specified value.
1009 This is not supported pre-UEFI Shell 2.0.
1011 @param EnvKey The key name of the environment variable.
1012 @param EnvVal The Value of the environment variable
1013 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
1015 @retval EFI_SUCCESS the operation was completed sucessfully
1016 @retval EFI_UNSUPPORTED This operation is not allowed in pre UEFI 2.0 Shell environments
1020 ShellSetEnvironmentVariable (
1021 IN CONST CHAR16
*EnvKey
,
1022 IN CONST CHAR16
*EnvVal
,
1027 // Check for UEFI Shell 2.0 protocols
1029 if (mEfiShellProtocol
!= NULL
) {
1030 return (mEfiShellProtocol
->SetEnv(EnvKey
, EnvVal
, Volatile
));
1034 // This feature does not exist under EFI shell
1036 return (EFI_UNSUPPORTED
);
1039 cause the shell to parse and execute a command line.
1041 This function creates a nested instance of the shell and executes the specified
1042 command (CommandLine) with the specified environment (Environment). Upon return,
1043 the status code returned by the specified command is placed in StatusCode.
1044 If Environment is NULL, then the current environment is used and all changes made
1045 by the commands executed will be reflected in the current environment. If the
1046 Environment is non-NULL, then the changes made will be discarded.
1047 The CommandLine is executed from the current working directory on the current
1050 EnvironmentVariables and Status are only supported for UEFI Shell 2.0.
1051 Output is only supported for pre-UEFI Shell 2.0
1053 @param ImageHandle Parent image that is starting the operation
1054 @param CommandLine pointer to null terminated command line.
1055 @param Output true to display debug output. false to hide it.
1056 @param EnvironmentVariables optional pointer to array of environment variables
1057 in the form "x=y". if NULL current set is used.
1058 @param Status the status of the run command line.
1060 @retval EFI_SUCCESS the operation completed sucessfully. Status
1061 contains the status code returned.
1062 @retval EFI_INVALID_PARAMETER a parameter contains an invalid value
1063 @retval EFI_OUT_OF_RESOURCES out of resources
1064 @retval EFI_UNSUPPORTED the operation is not allowed.
1069 IN EFI_HANDLE
*ParentHandle
,
1070 IN CHAR16
*CommandLine OPTIONAL
,
1071 IN BOOLEAN Output OPTIONAL
,
1072 IN CHAR16
**EnvironmentVariables OPTIONAL
,
1073 OUT EFI_STATUS
*Status OPTIONAL
1077 // Check for UEFI Shell 2.0 protocols
1079 if (mEfiShellProtocol
!= NULL
) {
1081 // Call UEFI Shell 2.0 version (not using Output parameter)
1083 return (mEfiShellProtocol
->Execute(ParentHandle
,
1085 EnvironmentVariables
,
1089 // ASSERT that we must have EFI shell
1091 ASSERT(mEfiShellEnvironment2
!= NULL
);
1093 // Call EFI Shell version (not using EnvironmentVariables or Status parameters)
1094 // Due to oddity in the EFI shell we want to dereference the ParentHandle here
1096 return (mEfiShellEnvironment2
->Execute(*ParentHandle
,
1101 Retreives the current directory path
1103 If the DeviceName is NULL, it returns the current device's current directory
1104 name. If the DeviceName is not NULL, it returns the current directory name
1107 @param DeviceName the name of the drive to get directory on
1109 @retval NULL the directory does not exist
1110 @return != NULL the directory
1114 ShellGetCurrentDir (
1115 IN CHAR16
*DeviceName OPTIONAL
1119 // Check for UEFI Shell 2.0 protocols
1121 if (mEfiShellProtocol
!= NULL
) {
1122 return (mEfiShellProtocol
->GetCurDir(DeviceName
));
1125 // ASSERT that we must have EFI shell
1127 ASSERT(mEfiShellEnvironment2
!= NULL
);
1128 return (mEfiShellEnvironment2
->CurDir(DeviceName
));
1131 sets (enabled or disabled) the page break mode
1133 when page break mode is enabled the screen will stop scrolling
1134 and wait for operator input before scrolling a subsequent screen.
1136 @param CurrentState TRUE to enable and FALSE to disable
1140 ShellSetPageBreakMode (
1141 IN BOOLEAN CurrentState
1145 // check for enabling
1147 if (CurrentState
!= 0x00) {
1149 // check for UEFI Shell 2.0
1151 if (mEfiShellProtocol
!= NULL
) {
1153 // Enable with UEFI 2.0 Shell
1155 mEfiShellProtocol
->EnablePageBreak();
1159 // ASSERT that must have EFI Shell
1161 ASSERT(mEfiShellEnvironment2
!= NULL
);
1163 // Enable with EFI Shell
1165 mEfiShellEnvironment2
->EnablePageBreak (DEFAULT_INIT_ROW
, DEFAULT_AUTO_LF
);
1170 // check for UEFI Shell 2.0
1172 if (mEfiShellProtocol
!= NULL
) {
1174 // Disable with UEFI 2.0 Shell
1176 mEfiShellProtocol
->DisablePageBreak();
1180 // ASSERT that must have EFI Shell
1182 ASSERT(mEfiShellEnvironment2
!= NULL
);
1184 // Disable with EFI Shell
1186 mEfiShellEnvironment2
->DisablePageBreak ();
1193 /// version of EFI_SHELL_FILE_INFO struct, except has no CONST pointers.
1194 /// This allows for the struct to be populated.
1201 EFI_FILE_HANDLE Handle
;
1202 EFI_FILE_INFO
*Info
;
1203 } EFI_SHELL_FILE_INFO_NO_CONST
;
1206 Converts a EFI shell list of structures to the coresponding UEFI Shell 2.0 type of list.
1208 if OldStyleFileList is NULL then ASSERT()
1210 this function will convert a SHELL_FILE_ARG based list into a callee allocated
1211 EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via
1212 the ShellCloseFileMetaArg function.
1214 @param FileList the EFI shell list type
1216 @retval the resultant head of the double linked new format list;
1220 InternalShellConvertFileListType (
1221 LIST_ENTRY
*FileList
1224 LIST_ENTRY
*ListHead
;
1225 SHELL_FILE_ARG
*OldInfo
;
1227 EFI_SHELL_FILE_INFO_NO_CONST
*NewInfo
;
1230 // ASSERT that FileList is not NULL
1232 ASSERT(FileList
!= NULL
);
1235 // Allocate our list head and initialize the list
1237 ListHead
= AllocateZeroPool(sizeof(LIST_ENTRY
));
1238 ASSERT (ListHead
!= NULL
);
1239 ListHead
= InitializeListHead (ListHead
);
1242 // enumerate through each member of the old list and copy
1244 for (Link
= FileList
->ForwardLink
; Link
!= FileList
; Link
= Link
->ForwardLink
) {
1245 OldInfo
= CR (Link
, SHELL_FILE_ARG
, Link
, SHELL_FILE_ARG_SIGNATURE
);
1248 // make sure the old list was valid
1250 ASSERT(OldInfo
!= NULL
);
1251 ASSERT(OldInfo
->Info
!= NULL
);
1252 ASSERT(OldInfo
->FullName
!= NULL
);
1253 ASSERT(OldInfo
->FileName
!= NULL
);
1256 // allocate a new EFI_SHELL_FILE_INFO object
1258 NewInfo
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1261 // copy the simple items
1263 NewInfo
->Handle
= OldInfo
->Handle
;
1264 NewInfo
->Status
= OldInfo
->Status
;
1266 // old shell checks for 0 not NULL
1267 OldInfo
->Handle
= 0;
1270 // allocate new space to copy strings and structure
1272 NewInfo
->FullName
= AllocateZeroPool(StrSize(OldInfo
->FullName
));
1273 NewInfo
->FileName
= AllocateZeroPool(StrSize(OldInfo
->FileName
));
1274 NewInfo
->Info
= AllocateZeroPool((UINTN
)OldInfo
->Info
->Size
);
1277 // make sure all the memory allocations were sucessful
1279 ASSERT(NewInfo
->FullName
!= NULL
);
1280 ASSERT(NewInfo
->FileName
!= NULL
);
1281 ASSERT(NewInfo
->Info
!= NULL
);
1284 // Copt the strings and structure
1286 StrCpy(NewInfo
->FullName
, OldInfo
->FullName
);
1287 StrCpy(NewInfo
->FileName
, OldInfo
->FileName
);
1288 gBS
->CopyMem (NewInfo
->Info
, OldInfo
->Info
, (UINTN
)OldInfo
->Info
->Size
);
1291 // add that to the list
1293 InsertTailList(ListHead
, (LIST_ENTRY
*)NewInfo
);
1298 Opens a group of files based on a path.
1300 This function uses the Arg to open all the matching files. Each matched
1301 file has a SHELL_FILE_ARG structure to record the file information. These
1302 structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG
1303 structures from ListHead to access each file. This function supports wildcards
1304 and will process '?' and '*' as such. the list must be freed with a call to
1305 ShellCloseFileMetaArg().
1307 This function will fail if called sequentially without freeing the list in the middle.
1309 @param Arg pointer to path string
1310 @param OpenMode mode to open files with
1311 @param ListHead head of linked list of results
1313 @retval EFI_SUCCESS the operation was sucessful and the list head
1314 contains the list of opened files
1315 #retval EFI_UNSUPPORTED a previous ShellOpenFileMetaArg must be closed first.
1316 *ListHead is set to NULL.
1317 @return != EFI_SUCCESS the operation failed
1319 @sa InternalShellConvertFileListType
1323 ShellOpenFileMetaArg (
1326 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1330 LIST_ENTRY
*EmptyNode
;
1331 LIST_ENTRY
*mOldStyleFileList
;
1334 // ASSERT that Arg and ListHead are not NULL
1336 ASSERT(Arg
!= NULL
);
1337 ASSERT(ListHead
!= NULL
);
1340 // Check for UEFI Shell 2.0 protocols
1342 if (mEfiShellProtocol
!= NULL
) {
1343 return (mEfiShellProtocol
->OpenFileList(Arg
,
1349 // ASSERT that we must have EFI shell
1351 ASSERT(mEfiShellEnvironment2
!= NULL
);
1354 // allocate memory for old list head
1356 mOldStyleFileList
= (LIST_ENTRY
*)AllocatePool(sizeof(LIST_ENTRY
));
1357 ASSERT(mOldStyleFileList
!= NULL
);
1360 // make sure the list head is initialized
1362 InitializeListHead((LIST_ENTRY
*)mOldStyleFileList
);
1365 // Get the EFI Shell list of files
1367 Status
= mEfiShellEnvironment2
->FileMetaArg(Arg
, mOldStyleFileList
);
1368 if (EFI_ERROR(Status
)) {
1374 // Convert that to equivalent of UEFI Shell 2.0 structure
1376 EmptyNode
= InternalShellConvertFileListType(mOldStyleFileList
);
1379 // Free the EFI Shell version that was converted.
1381 ASSERT_EFI_ERROR(mEfiShellEnvironment2
->FreeFileList(mOldStyleFileList
));
1382 FreePool(mOldStyleFileList
);
1383 mOldStyleFileList
= NULL
;
1386 // remove the empty head of the list
1388 *ListHead
= (EFI_SHELL_FILE_INFO
*)RemoveEntryList(EmptyNode
);
1389 FreePool(EmptyNode
);
1394 Free the linked list returned from ShellOpenFileMetaArg
1396 if ListHead is NULL then ASSERT()
1398 @param ListHead the pointer to free
1400 @retval EFI_SUCCESS the operation was sucessful
1404 ShellCloseFileMetaArg (
1405 IN OUT EFI_SHELL_FILE_INFO
**ListHead
1411 // ASSERT that ListHead is not NULL
1413 ASSERT(ListHead
!= NULL
);
1416 // Check for UEFI Shell 2.0 protocols
1418 if (mEfiShellProtocol
!= NULL
) {
1419 return (mEfiShellProtocol
->FreeFileList(ListHead
));
1422 // Since this is EFI Shell version we need to free our internally made copy
1425 for (Node
= GetFirstNode((LIST_ENTRY
*)*ListHead
) ; IsListEmpty((LIST_ENTRY
*)*ListHead
) == FALSE
; Node
= GetFirstNode((LIST_ENTRY
*)*ListHead
)) {
1426 RemoveEntryList(Node
);
1427 ((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
->Close(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Handle
);
1428 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FullName
);
1429 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->FileName
);
1430 FreePool(((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
)->Info
);
1431 FreePool((EFI_SHELL_FILE_INFO_NO_CONST
*)Node
);
1442 UINTN OriginalPosition
;
1443 } SHELL_PARAM_PACKAGE
;
1446 Checks the list of valid arguments and returns TRUE if the item was found. If the
1447 return value is TRUE then the type parameter is set also.
1449 if CheckList is NULL then ASSERT();
1450 if Name is NULL then ASSERT();
1451 if Type is NULL then ASSERT();
1453 @param Type pointer to type of parameter if it was found
1454 @param Name pointer to Name of parameter found
1455 @param CheckList List to check against
1457 @retval TRUE the Parameter was found. Type is valid.
1458 @retval FALSE the Parameter was not found. Type is not valid.
1462 InternalIsOnCheckList (
1463 IN CONST CHAR16
*Name
,
1464 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1468 SHELL_PARAM_ITEM
*TempListItem
;
1471 // ASSERT that all 3 pointer parameters aren't NULL
1473 ASSERT(CheckList
!= NULL
);
1474 ASSERT(Type
!= NULL
);
1475 ASSERT(Name
!= NULL
);
1478 // question mark and page break mode are always supported
1480 if ((StrCmp(Name
, L
"-?") == 0) ||
1481 (StrCmp(Name
, L
"-b") == 0)
1487 // Enumerate through the list
1489 for (TempListItem
= (SHELL_PARAM_ITEM
*)CheckList
; TempListItem
->Name
!= NULL
; TempListItem
++) {
1491 // If the Name matches set the type and return TRUE
1493 if (StrCmp(Name
, TempListItem
->Name
) == 0) {
1494 *Type
= TempListItem
->Type
;
1501 Checks the string for indicators of "flag" status. this is a leading '/', '-', or '+'
1503 @param Name pointer to Name of parameter found
1505 @retval TRUE the Parameter is a flag.
1506 @retval FALSE the Parameter not a flag
1511 IN CONST CHAR16
*Name
1515 // ASSERT that Name isn't NULL
1517 ASSERT(Name
!= NULL
);
1520 // If the Name has a / or - as the first character return TRUE
1522 if ((Name
[0] == L
'/') ||
1523 (Name
[0] == L
'-') ||
1532 Checks the command line arguments passed against the list of valid ones.
1534 If no initialization is required, then return RETURN_SUCCESS.
1536 @param CheckList pointer to list of parameters to check
1537 @param CheckPackage pointer to pointer to list checked values
1538 @param ProblemParam optional pointer to pointer to unicode string for
1539 the paramater that caused failure. If used then the
1540 caller is responsible for freeing the memory.
1541 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1542 @param Argc Count of parameters in Argv
1543 @param Argv pointer to array of parameters
1545 @retval EFI_SUCCESS The operation completed sucessfully.
1546 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1547 @retval EFI_INVALID_PARAMETER A parameter was invalid
1548 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1549 duplicated. the duplicated command line argument
1550 was returned in ProblemParam if provided.
1551 @retval EFI_NOT_FOUND a argument required a value that was missing.
1552 the invalid command line argument was returned in
1553 ProblemParam if provided.
1557 InternalCommandLineParse (
1558 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1559 OUT LIST_ENTRY
**CheckPackage
,
1560 OUT CHAR16
**ProblemParam OPTIONAL
,
1561 IN BOOLEAN AutoPageBreak
,
1562 IN CONST CHAR16
**Argv
,
1568 ParamType CurrentItemType
;
1569 SHELL_PARAM_PACKAGE
*CurrentItemPackage
;
1570 BOOLEAN GetItemValue
;
1572 CurrentItemPackage
= NULL
;
1577 ASSERT(CheckList
!= NULL
);
1578 ASSERT(Argv
!= NULL
);
1581 GetItemValue
= FALSE
;
1584 // If there is only 1 item we dont need to do anything
1587 *CheckPackage
= NULL
;
1588 return (EFI_SUCCESS
);
1592 // initialize the linked list
1594 *CheckPackage
= (LIST_ENTRY
*)AllocateZeroPool(sizeof(LIST_ENTRY
));
1595 InitializeListHead(*CheckPackage
);
1598 // loop through each of the arguments
1600 for (LoopCounter
= 0 ; LoopCounter
< Argc
; ++LoopCounter
) {
1601 if (Argv
[LoopCounter
] == NULL
) {
1603 // do nothing for NULL argv
1605 } else if (GetItemValue
== TRUE
) {
1606 ASSERT(CurrentItemPackage
!= NULL
);
1608 // get the item VALUE for the previous flag
1610 GetItemValue
= FALSE
;
1611 CurrentItemPackage
->Value
= AllocateZeroPool(StrSize(Argv
[LoopCounter
]));
1612 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1613 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1614 InsertTailList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1615 } else if (InternalIsFlag(Argv
[LoopCounter
]) == FALSE
) {
1617 // add this one as a non-flag
1619 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1620 ASSERT(CurrentItemPackage
!= NULL
);
1621 CurrentItemPackage
->Name
= NULL
;
1622 CurrentItemPackage
->Type
= TypePosition
;
1623 CurrentItemPackage
->Value
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1624 ASSERT(CurrentItemPackage
->Value
!= NULL
);
1625 StrCpy(CurrentItemPackage
->Value
, Argv
[LoopCounter
]);
1626 CurrentItemPackage
->OriginalPosition
= Count
++;
1627 InsertTailList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1628 } else if (InternalIsOnCheckList(Argv
[LoopCounter
], CheckList
, &CurrentItemType
) == TRUE
) {
1632 CurrentItemPackage
= AllocatePool(sizeof(SHELL_PARAM_PACKAGE
));
1633 ASSERT(CurrentItemPackage
!= NULL
);
1634 CurrentItemPackage
->Name
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1635 ASSERT(CurrentItemPackage
->Name
!= NULL
);
1636 StrCpy(CurrentItemPackage
->Name
, Argv
[LoopCounter
]);
1637 CurrentItemPackage
->Type
= CurrentItemType
;
1638 CurrentItemPackage
->OriginalPosition
= (UINTN
)(-1);
1641 // Does this flag require a value
1643 if (CurrentItemPackage
->Type
== TypeValue
) {
1645 // trigger the next loop to populate the value of this item
1647 GetItemValue
= TRUE
;
1650 // this item has no value expected; we are done
1652 CurrentItemPackage
->Value
= NULL
;
1653 InsertTailList(*CheckPackage
, (LIST_ENTRY
*)CurrentItemPackage
);
1655 } else if (ProblemParam
) {
1657 // this was a non-recognised flag... error!
1659 *ProblemParam
= AllocatePool(StrSize(Argv
[LoopCounter
]));
1660 ASSERT(*ProblemParam
!= NULL
);
1661 StrCpy(*ProblemParam
, Argv
[LoopCounter
]);
1662 ShellCommandLineFreeVarList(*CheckPackage
);
1663 *CheckPackage
= NULL
;
1664 return (EFI_VOLUME_CORRUPTED
);
1666 ShellCommandLineFreeVarList(*CheckPackage
);
1667 *CheckPackage
= NULL
;
1668 return (EFI_VOLUME_CORRUPTED
);
1672 // support for AutoPageBreak
1674 if (AutoPageBreak
&& ShellCommandLineGetFlag(*CheckPackage
, L
"-b")) {
1675 ShellSetPageBreakMode(TRUE
);
1677 return (EFI_SUCCESS
);
1681 Checks the command line arguments passed against the list of valid ones.
1682 Optionally removes NULL values first.
1684 If no initialization is required, then return RETURN_SUCCESS.
1686 @param CheckList pointer to list of parameters to check
1687 @param CheckPackage pointer to pointer to list checked values
1688 @param ProblemParam optional pointer to pointer to unicode string for
1689 the paramater that caused failure.
1690 @param AutoPageBreak will automatically set PageBreakEnabled for "b" parameter
1692 @retval EFI_SUCCESS The operation completed sucessfully.
1693 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1694 @retval EFI_INVALID_PARAMETER A parameter was invalid
1695 @retval EFI_VOLUME_CORRUPTED the command line was corrupt. an argument was
1696 duplicated. the duplicated command line argument
1697 was returned in ProblemParam if provided.
1698 @retval EFI_DEVICE_ERROR the commands contained 2 opposing arguments. one
1699 of the command line arguments was returned in
1700 ProblemParam if provided.
1701 @retval EFI_NOT_FOUND a argument required a value that was missing.
1702 the invalid command line argument was returned in
1703 ProblemParam if provided.
1707 ShellCommandLineParse (
1708 IN CONST SHELL_PARAM_ITEM
*CheckList
,
1709 OUT LIST_ENTRY
**CheckPackage
,
1710 OUT CHAR16
**ProblemParam OPTIONAL
,
1711 IN BOOLEAN AutoPageBreak
1715 // ASSERT that CheckList and CheckPackage aren't NULL
1717 ASSERT(CheckList
!= NULL
);
1718 ASSERT(CheckPackage
!= NULL
);
1721 // Check for UEFI Shell 2.0 protocols
1723 if (mEfiShellParametersProtocol
!= NULL
) {
1724 return (InternalCommandLineParse(CheckList
,
1728 mEfiShellParametersProtocol
->Argv
,
1729 mEfiShellParametersProtocol
->Argc
));
1733 // ASSERT That EFI Shell is not required
1735 ASSERT (mEfiShellInterface
!= NULL
);
1736 return (InternalCommandLineParse(CheckList
,
1740 mEfiShellInterface
->Argv
,
1741 mEfiShellInterface
->Argc
));
1745 Frees shell variable list that was returned from ShellCommandLineParse.
1747 This function will free all the memory that was used for the CheckPackage
1748 list of postprocessed shell arguments.
1750 this function has no return value.
1752 if CheckPackage is NULL, then return
1754 @param CheckPackage the list to de-allocate
1758 ShellCommandLineFreeVarList (
1759 IN LIST_ENTRY
*CheckPackage
1765 // check for CheckPackage == NULL
1767 if (CheckPackage
== NULL
) {
1772 // for each node in the list
1774 for (Node
= GetFirstNode(CheckPackage
); Node
!= CheckPackage
; Node
= GetFirstNode(CheckPackage
)) {
1776 // Remove it from the list
1778 RemoveEntryList(Node
);
1781 // if it has a name free the name
1783 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1784 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Name
);
1788 // if it has a value free the value
1790 if (((SHELL_PARAM_PACKAGE
*)Node
)->Value
!= NULL
) {
1791 FreePool(((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1795 // free the node structure
1797 FreePool((SHELL_PARAM_PACKAGE
*)Node
);
1800 // free the list head node
1802 FreePool(CheckPackage
);
1805 Checks for presence of a flag parameter
1807 flag arguments are in the form of "-<Key>" or "/<Key>", but do not have a value following the key
1809 if CheckPackage is NULL then return FALSE.
1810 if KeyString is NULL then ASSERT()
1812 @param CheckPackage The package of parsed command line arguments
1813 @param KeyString the Key of the command line argument to check for
1815 @retval TRUE the flag is on the command line
1816 @retval FALSE the flag is not on the command line
1820 ShellCommandLineGetFlag (
1821 IN CONST LIST_ENTRY
*CheckPackage
,
1822 IN CHAR16
*KeyString
1828 // ASSERT that both CheckPackage and KeyString aren't NULL
1830 ASSERT(KeyString
!= NULL
);
1833 // return FALSE for no package
1835 if (CheckPackage
== NULL
) {
1840 // enumerate through the list of parametrs
1842 for (Node
= GetFirstNode(CheckPackage
) ; Node
!= CheckPackage
; Node
= GetNextNode(CheckPackage
, Node
) ) {
1844 // If the Name matches, return TRUE (and there may be NULL name)
1846 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1847 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1855 returns value from command line argument
1857 value parameters are in the form of "-<Key> value" or "/<Key> value"
1859 if CheckPackage is NULL, then return NULL;
1861 @param CheckPackage The package of parsed command line arguments
1862 @param KeyString the Key of the command line argument to check for
1864 @retval NULL the flag is not on the command line
1865 @return !=NULL pointer to unicode string of the value
1869 ShellCommandLineGetValue (
1870 IN CONST LIST_ENTRY
*CheckPackage
,
1871 IN CHAR16
*KeyString
1877 // check for CheckPackage == NULL
1879 if (CheckPackage
== NULL
) {
1884 // enumerate through the list of parametrs
1886 for (Node
= GetFirstNode(CheckPackage
) ; Node
!= NULL
; Node
= GetNextNode(CheckPackage
, Node
) ) {
1888 // If the Name matches, return the value (name can be NULL)
1890 if (((SHELL_PARAM_PACKAGE
*)Node
)->Name
!= NULL
) {
1891 if (StrCmp(KeyString
, ((SHELL_PARAM_PACKAGE
*)Node
)->Name
) == 0) {
1892 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);
1899 returns raw value from command line argument
1901 raw value parameters are in the form of "value" in a specific position in the list
1903 if CheckPackage is NULL, then return NULL;
1905 @param CheckPackage The package of parsed command line arguments
1906 @param Position the position of the value
1908 @retval NULL the flag is not on the command line
1909 @return !=NULL pointer to unicode string of the value
1913 ShellCommandLineGetRawValue (
1914 IN CONST LIST_ENTRY
*CheckPackage
,
1921 // check for CheckPackage == NULL
1923 if (CheckPackage
== NULL
) {
1928 // enumerate through the list of parametrs
1930 for (Node
= GetFirstNode(CheckPackage
) ; Node
!= NULL
; Node
= GetNextNode(CheckPackage
, Node
) ) {
1932 // If the position matches, return the value
1934 if (((SHELL_PARAM_PACKAGE
*)Node
)->OriginalPosition
== Position
) {
1935 return (((SHELL_PARAM_PACKAGE
*)Node
)->Value
);