X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellLib%2FUefiShellLib.c;h=7f6389f655783a7a68156490c2118b9c0e8cf5d7;hp=e824895cdb45881c15daa4994676fa6eb32ec0d7;hb=68b07ebae5d7c4124ff7b2a8d35e2f8b6cda18b8;hpb=5d46f17ba795ecba56ed73538961ecc013c98376 diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Library/UefiShellLib/UefiShellLib.c index e824895cdb..7f6389f655 100644 --- a/ShellPkg/Library/UefiShellLib/UefiShellLib.c +++ b/ShellPkg/Library/UefiShellLib/UefiShellLib.c @@ -1,7 +1,9 @@ /** @file Provides interface to shell functionality for shell commands and applications. - Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ Copyright 2016 Dell Inc. + Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -13,10 +15,8 @@ **/ #include "UefiShellLib.h" -#include #include - -#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN) +#include // // globals... @@ -34,6 +34,7 @@ EFI_SHELL_PROTOCOL *gEfiShellProtocol; EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol; EFI_HANDLE mEfiShellEnvironment2Handle; FILE_HANDLE_FUNCTION_MAP FileFunctionMap; +EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationProtocol; /** Check if a Unicode character is a hexadecimal character. @@ -84,9 +85,10 @@ ShellIsDecimalDigitCharacter ( Helper function to find ShellEnvironment2 for constructor. @param[in] ImageHandle A copy of the calling image's handle. + + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. **/ EFI_STATUS -EFIAPI ShellFindSE2 ( IN EFI_HANDLE ImageHandle ) @@ -123,7 +125,9 @@ ShellFindSE2 ( // if (Status == EFI_BUFFER_TOO_SMALL) { Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize); - ASSERT(Buffer != NULL); + if (Buffer == NULL) { + return (EFI_OUT_OF_RESOURCES); + } Status = gBS->LocateHandle (ByProtocol, &gEfiShellEnvironment2Guid, NULL, // ignored for ByProtocol @@ -159,7 +163,7 @@ ShellFindSE2 ( } /** - Function to do most of the work of the constructor. Allows for calling + Function to do most of the work of the constructor. Allows for calling multiple times without complete re-initialization. @param[in] ImageHandle A copy of the ImageHandle. @@ -168,7 +172,6 @@ ShellFindSE2 ( @retval EFI_SUCCESS The operationw as successful. **/ EFI_STATUS -EFIAPI ShellLibConstructorWorker ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable @@ -176,43 +179,48 @@ ShellLibConstructorWorker ( { EFI_STATUS Status; - // - // UEFI 2.0 shell interfaces (used preferentially) - // - Status = gBS->OpenProtocol( - ImageHandle, - &gEfiShellProtocolGuid, - (VOID **)&gEfiShellProtocol, - ImageHandle, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); - if (EFI_ERROR(Status)) { + if (gEfiShellProtocol == NULL) { // - // Search for the shell protocol + // UEFI 2.0 shell interfaces (used preferentially) // - Status = gBS->LocateProtocol( + Status = gBS->OpenProtocol ( + ImageHandle, &gEfiShellProtocolGuid, + (VOID **)&gEfiShellProtocol, + ImageHandle, NULL, - (VOID **)&gEfiShellProtocol - ); - if (EFI_ERROR(Status)) { - gEfiShellProtocol = NULL; + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + // + // Search for the shell protocol + // + Status = gBS->LocateProtocol ( + &gEfiShellProtocolGuid, + NULL, + (VOID **)&gEfiShellProtocol + ); + if (EFI_ERROR (Status)) { + gEfiShellProtocol = NULL; + } } } - Status = gBS->OpenProtocol( - ImageHandle, - &gEfiShellParametersProtocolGuid, - (VOID **)&gEfiShellParametersProtocol, - ImageHandle, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); - if (EFI_ERROR(Status)) { - gEfiShellParametersProtocol = NULL; + + if (gEfiShellParametersProtocol == NULL) { + Status = gBS->OpenProtocol ( + ImageHandle, + &gEfiShellParametersProtocolGuid, + (VOID **)&gEfiShellParametersProtocol, + ImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + gEfiShellParametersProtocol = NULL; + } } - if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) { + if (gEfiShellProtocol == NULL) { // // Moved to seperate function due to complexity // @@ -235,10 +243,14 @@ ShellLibConstructorWorker ( } // - // only success getting 2 of either the old or new, but no 1/2 and 1/2 + // Getting either EDK Shell's ShellEnvironment2 and ShellInterface protocol + // or UEFI Shell's Shell protocol. + // When ShellLib is linked to a driver producing DynamicCommand protocol, + // ShellParameters protocol is set by DynamicCommand.Handler(). // - if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) || - (gEfiShellProtocol != NULL && gEfiShellParametersProtocol != NULL) ) { + if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) || + (gEfiShellProtocol != NULL) + ) { if (gEfiShellProtocol != NULL) { FileFunctionMap.GetFileInfo = gEfiShellProtocol->GetFileInfo; FileFunctionMap.SetFileInfo = gEfiShellProtocol->SetFileInfo; @@ -289,6 +301,7 @@ ShellLibConstructor ( gEfiShellParametersProtocol = NULL; mEfiShellInterface = NULL; mEfiShellEnvironment2Handle = NULL; + mUnicodeCollationProtocol = NULL; // // verify that auto initialize is not set false @@ -316,35 +329,45 @@ ShellLibDestructor ( IN EFI_SYSTEM_TABLE *SystemTable ) { + EFI_STATUS Status; + if (mEfiShellEnvironment2 != NULL) { - gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle, + Status = gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle, &gEfiShellEnvironment2Guid, ImageHandle, NULL); - mEfiShellEnvironment2 = NULL; + if (!EFI_ERROR (Status)) { + mEfiShellEnvironment2 = NULL; + mEfiShellEnvironment2Handle = NULL; + } } if (mEfiShellInterface != NULL) { - gBS->CloseProtocol(ImageHandle, + Status = gBS->CloseProtocol(ImageHandle, &gEfiShellInterfaceGuid, ImageHandle, NULL); - mEfiShellInterface = NULL; + if (!EFI_ERROR (Status)) { + mEfiShellInterface = NULL; + } } if (gEfiShellProtocol != NULL) { - gBS->CloseProtocol(ImageHandle, + Status = gBS->CloseProtocol(ImageHandle, &gEfiShellProtocolGuid, ImageHandle, NULL); - gEfiShellProtocol = NULL; + if (!EFI_ERROR (Status)) { + gEfiShellProtocol = NULL; + } } if (gEfiShellParametersProtocol != NULL) { - gBS->CloseProtocol(ImageHandle, + Status = gBS->CloseProtocol(ImageHandle, &gEfiShellParametersProtocolGuid, ImageHandle, NULL); - gEfiShellParametersProtocol = NULL; + if (!EFI_ERROR (Status)) { + gEfiShellParametersProtocol = NULL; + } } - mEfiShellEnvironment2Handle = NULL; return (EFI_SUCCESS); } @@ -365,8 +388,11 @@ ShellLibDestructor ( EFI_STATUS EFIAPI ShellInitialize ( + VOID ) { + EFI_STATUS Status; + // // if auto initialize is not false then skip // @@ -377,7 +403,8 @@ ShellInitialize ( // // deinit the current stuff // - ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST)); + Status = ShellLibDestructor (gImageHandle, gST); + ASSERT_EFI_ERROR (Status); // // init the new stuff @@ -417,20 +444,20 @@ ShellGetFileInfo ( @param[in] FileInfo The information to set. - @retval EFI_SUCCESS The information was set. + @retval EFI_SUCCESS The information was set. @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid. @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo. - @retval EFI_NO_MEDIA The device has no medium. - @retval EFI_DEVICE_ERROR The device reported an error. - @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. - @retval EFI_WRITE_PROTECTED The file or medium is write protected. + @retval EFI_NO_MEDIA The device has no medium. + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_WRITE_PROTECTED The file or medium is write protected. @retval EFI_ACCESS_DENIED The file was opened read only. @retval EFI_VOLUME_FULL The volume is full. **/ EFI_STATUS EFIAPI ShellSetFileInfo ( - IN SHELL_FILE_HANDLE FileHandle, + IN SHELL_FILE_HANDLE FileHandle, IN EFI_FILE_INFO *FileInfo ) { @@ -443,38 +470,38 @@ ShellSetFileInfo ( This function opens a file with the open mode according to the file path. The Attributes is valid only for EFI_FILE_MODE_CREATE. - @param FilePath on input the device path to the file. On output + @param FilePath on input the device path to the file. On output the remaining device path. - @param DeviceHandle pointer to the system device handle. - @param FileHandle pointer to the file handle. - @param OpenMode the mode to open the file with. - @param Attributes the file's file attributes. - - @retval EFI_SUCCESS The information was set. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. - @retval EFI_UNSUPPORTED Could not open the file path. - @retval EFI_NOT_FOUND The specified file could not be found on the + @param DeviceHandle pointer to the system device handle. + @param FileHandle pointer to the file handle. + @param OpenMode the mode to open the file with. + @param Attributes the file's file attributes. + + @retval EFI_SUCCESS The information was set. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_UNSUPPORTED Could not open the file path. + @retval EFI_NOT_FOUND The specified file could not be found on the device or the file system could not be found on the device. - @retval EFI_NO_MEDIA The device has no medium. - @retval EFI_MEDIA_CHANGED The device has a different medium in it or the + @retval EFI_NO_MEDIA The device has no medium. + @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no longer supported. - @retval EFI_DEVICE_ERROR The device reported an error. - @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. - @retval EFI_WRITE_PROTECTED The file or medium is write protected. - @retval EFI_ACCESS_DENIED The file was opened read only. - @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_WRITE_PROTECTED The file or medium is write protected. + @retval EFI_ACCESS_DENIED The file was opened read only. + @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. - @retval EFI_VOLUME_FULL The volume is full. + @retval EFI_VOLUME_FULL The volume is full. **/ EFI_STATUS EFIAPI ShellOpenFileByDevicePath( - IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath, - OUT EFI_HANDLE *DeviceHandle, + IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath, + OUT EFI_HANDLE *DeviceHandle, OUT SHELL_FILE_HANDLE *FileHandle, - IN UINT64 OpenMode, - IN UINT64 Attributes + IN UINT64 OpenMode, + IN UINT64 Attributes ) { CHAR16 *FileName; @@ -482,6 +509,8 @@ ShellOpenFileByDevicePath( EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol; EFI_FILE_PROTOCOL *Handle1; EFI_FILE_PROTOCOL *Handle2; + CHAR16 *FnafPathName; + UINTN PathLen; if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) { return (EFI_INVALID_PARAMETER); @@ -547,13 +576,36 @@ ShellOpenFileByDevicePath( Handle2 = Handle1; Handle1 = NULL; + // + // File Name Alignment Fix (FNAF) + // Handle2->Open may be incapable of handling a unaligned CHAR16 data. + // The structure pointed to by FilePath may be not CHAR16 aligned. + // This code copies the potentially unaligned PathName data from the + // FilePath structure to the aligned FnafPathName for use in the + // calls to Handl2->Open. + // + + // + // Determine length of PathName, in bytes. + // + PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH; + + // + // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment + // Copy bytes from possibly unaligned location to aligned location + // + FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName); + if (FnafPathName == NULL) { + return EFI_OUT_OF_RESOURCES; + } + // // Try to test opening an existing file // Status = Handle2->Open ( Handle2, &Handle1, - ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName, + FnafPathName, OpenMode &~EFI_FILE_MODE_CREATE, 0 ); @@ -565,11 +617,17 @@ ShellOpenFileByDevicePath( Status = Handle2->Open ( Handle2, &Handle1, - ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName, + FnafPathName, OpenMode, Attributes ); } + + // + // Free the alignment buffer + // + FreePool(FnafPathName); + // // Close the last node // @@ -601,41 +659,43 @@ ShellOpenFileByDevicePath( if FileName is NULL then ASSERT() - @param FileName pointer to file name - @param FileHandle pointer to the file handle. - @param OpenMode the mode to open the file with. - @param Attributes the file's file attributes. + @param FileName pointer to file name + @param FileHandle pointer to the file handle. + @param OpenMode the mode to open the file with. + @param Attributes the file's file attributes. - @retval EFI_SUCCESS The information was set. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. - @retval EFI_UNSUPPORTED Could not open the file path. - @retval EFI_NOT_FOUND The specified file could not be found on the + @retval EFI_SUCCESS The information was set. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_UNSUPPORTED Could not open the file path. + @retval EFI_NOT_FOUND The specified file could not be found on the device or the file system could not be found on the device. - @retval EFI_NO_MEDIA The device has no medium. - @retval EFI_MEDIA_CHANGED The device has a different medium in it or the + @retval EFI_NO_MEDIA The device has no medium. + @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no longer supported. - @retval EFI_DEVICE_ERROR The device reported an error. - @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. - @retval EFI_WRITE_PROTECTED The file or medium is write protected. - @retval EFI_ACCESS_DENIED The file was opened read only. - @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_WRITE_PROTECTED The file or medium is write protected. + @retval EFI_ACCESS_DENIED The file was opened read only. + @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. - @retval EFI_VOLUME_FULL The volume is full. + @retval EFI_VOLUME_FULL The volume is full. **/ EFI_STATUS EFIAPI ShellOpenFileByName( - IN CONST CHAR16 *FileName, + IN CONST CHAR16 *FileName, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, - IN UINT64 Attributes + IN UINT64 Attributes ) { EFI_HANDLE DeviceHandle; EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_STATUS Status; EFI_FILE_INFO *FileInfo; + CHAR16 *FileNameCopy; + EFI_STATUS Status2; // // ASSERT if FileName is NULL @@ -647,21 +707,61 @@ ShellOpenFileByName( } if (gEfiShellProtocol != NULL) { - if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) { - return ShellCreateDirectory(FileName, FileHandle); + if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE) { + + // + // Create only a directory + // + if ((Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) { + return ShellCreateDirectory(FileName, FileHandle); + } + + // + // Create the directory to create the file in + // + FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName); + if (FileNameCopy == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + PathCleanUpDirectories (FileNameCopy); + if (PathRemoveLastItem (FileNameCopy)) { + if (!EFI_ERROR(ShellCreateDirectory (FileNameCopy, FileHandle))) { + ShellCloseFile (FileHandle); + } + } + SHELL_FREE_NON_NULL (FileNameCopy); } + // - // Use UEFI Shell 2.0 method + // Use UEFI Shell 2.0 method to create the file // Status = gEfiShellProtocol->OpenFileByName(FileName, FileHandle, OpenMode); - if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){ + if (EFI_ERROR(Status)) { + return Status; + } + + if (mUnicodeCollationProtocol == NULL) { + Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&mUnicodeCollationProtocol); + if (EFI_ERROR (Status)) { + gEfiShellProtocol->CloseFile (*FileHandle); + return Status; + } + } + + if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NUL") != 0) && + (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NULL") != 0) && + !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){ FileInfo = FileFunctionMap.GetFileInfo(*FileHandle); ASSERT(FileInfo != NULL); FileInfo->Attribute = Attributes; - Status = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo); + Status2 = FileFunctionMap.SetFileInfo(*FileHandle, FileInfo); FreePool(FileInfo); + if (EFI_ERROR (Status2)) { + gEfiShellProtocol->CloseFile(*FileHandle); + } + Status = Status2; } return (Status); } @@ -688,25 +788,25 @@ ShellOpenFileByName( otherwise, the Filehandle is NULL. If the directory already existed, this function opens the existing directory. - @param DirectoryName pointer to directory name - @param FileHandle pointer to the file handle. + @param DirectoryName pointer to directory name + @param FileHandle pointer to the file handle. - @retval EFI_SUCCESS The information was set. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. - @retval EFI_UNSUPPORTED Could not open the file path. - @retval EFI_NOT_FOUND The specified file could not be found on the + @retval EFI_SUCCESS The information was set. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_UNSUPPORTED Could not open the file path. + @retval EFI_NOT_FOUND The specified file could not be found on the device or the file system could not be found on the device. - @retval EFI_NO_MEDIA The device has no medium. - @retval EFI_MEDIA_CHANGED The device has a different medium in it or the + @retval EFI_NO_MEDIA The device has no medium. + @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no longer supported. - @retval EFI_DEVICE_ERROR The device reported an error. - @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. - @retval EFI_WRITE_PROTECTED The file or medium is write protected. - @retval EFI_ACCESS_DENIED The file was opened read only. - @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_WRITE_PROTECTED The file or medium is write protected. + @retval EFI_ACCESS_DENIED The file was opened read only. + @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. - @retval EFI_VOLUME_FULL The volume is full. + @retval EFI_VOLUME_FULL The volume is full. @sa ShellOpenFileByName **/ EFI_STATUS @@ -754,11 +854,11 @@ ShellCreateDirectory( the number of bytes written. @param Buffer the buffer to put read data into. - @retval EFI_SUCCESS Data was read. - @retval EFI_NO_MEDIA The device has no media. - @retval EFI_DEVICE_ERROR The device reported an error. - @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. - @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required + @retval EFI_SUCCESS Data was read. + @retval EFI_NO_MEDIA The device has no media. + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required size. **/ @@ -789,14 +889,14 @@ ShellReadFile( the number of bytes written. @param Buffer the buffer containing data to write is stored. - @retval EFI_SUCCESS Data was written. - @retval EFI_UNSUPPORTED Writes to an open directory are not supported. - @retval EFI_NO_MEDIA The device has no media. - @retval EFI_DEVICE_ERROR The device reported an error. - @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. - @retval EFI_WRITE_PROTECTED The device is write-protected. - @retval EFI_ACCESS_DENIED The file was open for read only. - @retval EFI_VOLUME_FULL The volume is full. + @retval EFI_SUCCESS Data was written. + @retval EFI_UNSUPPORTED Writes to an open directory are not supported. + @retval EFI_NO_MEDIA The device has no media. + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_WRITE_PROTECTED The device is write-protected. + @retval EFI_ACCESS_DENIED The file was open for read only. + @retval EFI_VOLUME_FULL The volume is full. **/ EFI_STATUS EFIAPI @@ -841,12 +941,12 @@ ShellCloseFile ( @retval EFI_SUCCESS the file was closed sucessfully @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not deleted - @retval INVALID_PARAMETER One of the parameters has an invalid value. + @retval INVALID_PARAMETER One of the parameters has an invalid value. **/ EFI_STATUS EFIAPI ShellDeleteFile ( - IN SHELL_FILE_HANDLE *FileHandle + IN SHELL_FILE_HANDLE *FileHandle ) { return (FileFunctionMap.DeleteFile(*FileHandle)); @@ -874,8 +974,8 @@ ShellDeleteFile ( EFI_STATUS EFIAPI ShellSetFilePosition ( - IN SHELL_FILE_HANDLE FileHandle, - IN UINT64 Position + IN SHELL_FILE_HANDLE FileHandle, + IN UINT64 Position ) { return (FileFunctionMap.SetFilePosition(FileHandle, Position)); @@ -928,15 +1028,19 @@ ShellFlushFile ( return (FileFunctionMap.FlushFile(FileHandle)); } -/** - Retrieves the first file from a directory +/** Retrieve first entry from a directory. - This function opens a directory and gets the first file's info in the - directory. Caller can use ShellFindNextFile() to get other files. When - complete the caller is responsible for calling FreePool() on Buffer. + This function takes an open directory handle and gets information from the + first entry in the directory. A buffer is allocated to contain + the information and a pointer to the buffer is returned in *Buffer. The + caller can use ShellFindNextFile() to get subsequent directory entries. - @param DirHandle The file handle of the directory to search - @param Buffer Pointer to buffer for file's information + The buffer will be freed by ShellFindNextFile() when the last directory + entry is read. Otherwise, the caller must free the buffer, using FreePool, + when finished with it. + + @param[in] DirHandle The file handle of the directory to search. + @param[out] Buffer The pointer to the buffer for the file's information. @retval EFI_SUCCESS Found the first file. @retval EFI_NOT_FOUND Cannot find the directory. @@ -958,19 +1062,18 @@ ShellFindFirstFile ( // return (FileHandleFindFirstFile(DirHandle, Buffer)); } -/** - Retrieves the next file in a directory. +/** Retrieve next entries from a directory. - To use this function, caller must call the ShellFindFirstFile() to get the - first file, and then use this function get other files. This function can be - called for several times to get each file's information in the directory. If - the call of ShellFindNextFile() got the last file in the directory, the next - call of this function has no file to get. *NoFile will be set to TRUE and the - Buffer memory will be automatically freed. + To use this function, the caller must first call the ShellFindFirstFile() + function to get the first directory entry. Subsequent directory entries are + retrieved by using the ShellFindNextFile() function. This function can + be called several times to get each entry from the directory. If the call of + ShellFindNextFile() retrieved the last directory entry, the next call of + this function will set *NoFile to TRUE and free the buffer. - @param DirHandle the file handle of the directory - @param Buffer pointer to buffer for file's information - @param NoFile pointer to boolean when last file is found + @param[in] DirHandle The file handle of the directory. + @param[out] Buffer The pointer to buffer for file's information. + @param[out] NoFile The pointer to boolean when last file is found. @retval EFI_SUCCESS Found the next file, or reached last file @retval EFI_NO_MEDIA The device has no media. @@ -1137,7 +1240,7 @@ ShellSetEnvironmentVariable ( The CommandLine is executed from the current working directory on the current device. - The EnvironmentVariables and Status parameters are ignored in a pre-UEFI Shell 2.0 + The EnvironmentVariables pararemeter is ignored in a pre-UEFI Shell 2.0 environment. The values pointed to by the parameters will be unchanged by the ShellExecute() function. The Output parameter has no effect in a UEFI Shell 2.0 environment. @@ -1165,6 +1268,7 @@ ShellExecute ( OUT EFI_STATUS *Status OPTIONAL ) { + EFI_STATUS CmdStatus; // // Check for UEFI Shell 2.0 protocols // @@ -1183,16 +1287,29 @@ ShellExecute ( // if (mEfiShellEnvironment2 != NULL) { // - // Call EFI Shell version (not using EnvironmentVariables or Status parameters) + // Call EFI Shell version. // Due to oddity in the EFI shell we want to dereference the ParentHandle here // - return (mEfiShellEnvironment2->Execute(*ParentHandle, + CmdStatus = (mEfiShellEnvironment2->Execute(*ParentHandle, CommandLine, Output)); + // + // No Status output parameter so just use the returned status + // + if (Status != NULL) { + *Status = CmdStatus; + } + // + // If there was an error, we can't tell if it was from the command or from + // the Execute() function, so we'll just assume the shell ran successfully + // and the error came from the command. + // + return EFI_SUCCESS; } return (EFI_UNSUPPORTED); } + /** Retreives the current directory path @@ -1200,6 +1317,8 @@ ShellExecute ( name. If the DeviceName is not NULL, it returns the current directory name on specified drive. + Note that the current directory string should exclude the tailing backslash character. + @param DeviceName the name of the drive to get directory on @retval NULL the directory does not exist @@ -1313,13 +1432,12 @@ typedef struct { EFI_SHELL_FILE_INFO based list. it is up to the caller to free the memory via the ShellCloseFileMetaArg function. - @param[in] FileList the EFI shell list type - @param[in,out] ListHead the list to add to + @param[in] FileList the EFI shell list type + @param[in, out] ListHead the list to add to @retval the resultant head of the double linked new format list; **/ LIST_ENTRY* -EFIAPI InternalShellConvertFileListType ( IN LIST_ENTRY *FileList, IN OUT LIST_ENTRY *ListHead @@ -1360,8 +1478,9 @@ InternalShellConvertFileListType ( // allocate a new EFI_SHELL_FILE_INFO object // NewInfo = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); - ASSERT(NewInfo != NULL); if (NewInfo == NULL) { + ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead)); + ListHead = NULL; break; } @@ -1377,23 +1496,29 @@ InternalShellConvertFileListType ( // // allocate new space to copy strings and structure // - NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName)); - NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName)); - NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size); + NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName); + NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName); + NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info); // // make sure all the memory allocations were sucessful // - ASSERT(NewInfo->FullName != NULL); - ASSERT(NewInfo->FileName != NULL); - ASSERT(NewInfo->Info != NULL); + if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) { + // + // Free the partially allocated new node + // + SHELL_FREE_NON_NULL(NewInfo->FullName); + SHELL_FREE_NON_NULL(NewInfo->FileName); + SHELL_FREE_NON_NULL(NewInfo->Info); + SHELL_FREE_NON_NULL(NewInfo); - // - // Copt the strings and structure - // - StrCpy(NewInfo->FullName, OldInfo->FullName); - StrCpy(NewInfo->FileName, OldInfo->FileName); - gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size); + // + // Free the previously converted stuff + // + ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead)); + ListHead = NULL; + break; + } // // add that to the list @@ -1406,8 +1531,8 @@ InternalShellConvertFileListType ( Opens a group of files based on a path. This function uses the Arg to open all the matching files. Each matched - file has a SHELL_FILE_ARG structure to record the file information. These - structures are placed on the list ListHead. Users can get the SHELL_FILE_ARG + file has a SHELL_FILE_INFO structure to record the file information. These + structures are placed on the list ListHead. Users can get the SHELL_FILE_INFO structures from ListHead to access each file. This function supports wildcards and will process '?' and '*' as such. the list must be freed with a call to ShellCloseFileMetaArg(). @@ -1435,6 +1560,7 @@ ShellOpenFileMetaArg ( { EFI_STATUS Status; LIST_ENTRY mOldStyleFileList; + CHAR16 *CleanFilePathStr; // // ASSERT that Arg and ListHead are not NULL @@ -1442,6 +1568,13 @@ ShellOpenFileMetaArg ( ASSERT(Arg != NULL); ASSERT(ListHead != NULL); + CleanFilePathStr = NULL; + + Status = InternalShellStripQuotes (Arg, &CleanFilePathStr); + if (EFI_ERROR (Status)) { + return Status; + } + // // Check for UEFI Shell 2.0 protocols // @@ -1449,11 +1582,12 @@ ShellOpenFileMetaArg ( if (*ListHead == NULL) { *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); if (*ListHead == NULL) { + FreePool(CleanFilePathStr); return (EFI_OUT_OF_RESOURCES); } InitializeListHead(&((*ListHead)->Link)); } - Status = gEfiShellProtocol->OpenFileList(Arg, + Status = gEfiShellProtocol->OpenFileList(CleanFilePathStr, OpenMode, ListHead); if (EFI_ERROR(Status)) { @@ -1463,9 +1597,11 @@ ShellOpenFileMetaArg ( } if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) { FreePool(*ListHead); + FreePool(CleanFilePathStr); *ListHead = NULL; return (EFI_NOT_FOUND); } + FreePool(CleanFilePathStr); return (Status); } @@ -1481,15 +1617,17 @@ ShellOpenFileMetaArg ( // // Get the EFI Shell list of files // - Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList); + Status = mEfiShellEnvironment2->FileMetaArg(CleanFilePathStr, &mOldStyleFileList); if (EFI_ERROR(Status)) { *ListHead = NULL; + FreePool(CleanFilePathStr); return (Status); } if (*ListHead == NULL) { *ListHead = (EFI_SHELL_FILE_INFO *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); if (*ListHead == NULL) { + FreePool(CleanFilePathStr); return (EFI_OUT_OF_RESOURCES); } InitializeListHead(&((*ListHead)->Link)); @@ -1510,9 +1648,11 @@ ShellOpenFileMetaArg ( *ListHead = NULL; Status = EFI_NOT_FOUND; } + FreePool(CleanFilePathStr); return (Status); } + FreePool(CleanFilePathStr); return (EFI_UNSUPPORTED); } /** @@ -1557,6 +1697,7 @@ ShellCloseFileMetaArg ( FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Info); FreePool((EFI_SHELL_FILE_INFO_NO_CONST*)Node); } + SHELL_FREE_NON_NULL(*ListHead); return EFI_SUCCESS; } @@ -1609,15 +1750,15 @@ ShellFindFilePath ( Path = ShellGetEnvironmentVariable(L"cwd"); if (Path != NULL) { - Size = StrSize(Path); + Size = StrSize(Path) + sizeof(CHAR16); Size += StrSize(FileName); TestPath = AllocateZeroPool(Size); - ASSERT(TestPath != NULL); if (TestPath == NULL) { return (NULL); } - StrCpy(TestPath, Path); - StrCat(TestPath, FileName); + StrCpyS(TestPath, Size/sizeof(CHAR16), Path); + StrCatS(TestPath, Size/sizeof(CHAR16), L"\\"); + StrCatS(TestPath, Size/sizeof(CHAR16), FileName); Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0); if (!EFI_ERROR(Status)){ if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) { @@ -1649,12 +1790,12 @@ ShellFindFilePath ( *TempChar = CHAR_NULL; } if (TestPath[StrLen(TestPath)-1] != L'\\') { - StrCat(TestPath, L"\\"); + StrCatS(TestPath, Size/sizeof(CHAR16), L"\\"); } if (FileName[0] == L'\\') { FileName++; } - StrCat(TestPath, FileName); + StrCatS(TestPath, Size/sizeof(CHAR16), FileName); if (StrStr(Walker, L";") != NULL) { Walker = StrStr(Walker, L";") + 1; } else { @@ -1719,14 +1860,13 @@ ShellFindFilePathEx ( Size = StrSize(FileName); Size += StrSize(FileExtension); TestPath = AllocateZeroPool(Size); - ASSERT(TestPath != NULL); if (TestPath == NULL) { return (NULL); } for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){ - StrCpy(TestPath, FileName); + StrCpyS(TestPath, Size/sizeof(CHAR16), FileName); if (ExtensionWalker != NULL) { - StrCat(TestPath, ExtensionWalker); + StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker); } TempChar = StrStr(TestPath, L";"); if (TempChar != NULL) { @@ -1767,7 +1907,6 @@ typedef struct { @retval FALSE the Parameter was not found. Type is not valid. **/ BOOLEAN -EFIAPI InternalIsOnCheckList ( IN CONST CHAR16 *Name, IN CONST SHELL_PARAM_ITEM *CheckList, @@ -1802,7 +1941,7 @@ InternalIsOnCheckList ( // If the Type is TypeStart only check the first characters of the passed in param // If it matches set the type and return TRUE // - if (TempListItem->Type == TypeStart) { + if (TempListItem->Type == TypeStart) { if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) { *Type = TempListItem->Type; return (TRUE); @@ -1830,15 +1969,16 @@ InternalIsOnCheckList ( @param[in] Name pointer to Name of parameter found @param[in] AlwaysAllowNumbers TRUE to allow numbers, FALSE to not. + @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise. @retval TRUE the Parameter is a flag. @retval FALSE the Parameter not a flag. **/ BOOLEAN -EFIAPI InternalIsFlag ( IN CONST CHAR16 *Name, - IN BOOLEAN AlwaysAllowNumbers + IN CONST BOOLEAN AlwaysAllowNumbers, + IN CONST BOOLEAN TimeNumbers ) { // @@ -1849,7 +1989,7 @@ InternalIsFlag ( // // If we accept numbers then dont return TRUE. (they will be values) // - if (((Name[0] == L'-' || Name[0] == L'+') && ShellIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers) { + if (((Name[0] == L'-' || Name[0] == L'+') && InternalShellIsHexOrDecimalNumber(Name+1, FALSE, FALSE, TimeNumbers)) && AlwaysAllowNumbers) { return (FALSE); } @@ -1891,7 +2031,6 @@ InternalIsFlag ( ProblemParam if provided. **/ EFI_STATUS -EFIAPI InternalCommandLineParse ( IN CONST SHELL_PARAM_ITEM *CheckList, OUT LIST_ENTRY **CheckPackage, @@ -1909,6 +2048,8 @@ InternalCommandLineParse ( UINTN ValueSize; UINTN Count; CONST CHAR16 *TempPointer; + UINTN CurrentValueSize; + CHAR16 *NewValue; CurrentItemPackage = NULL; GetItemValue = 0; @@ -1933,6 +2074,10 @@ InternalCommandLineParse ( // initialize the linked list // *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY)); + if (*CheckPackage == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + InitializeListHead(*CheckPackage); // @@ -1955,10 +2100,17 @@ InternalCommandLineParse ( // this is a flag // CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE)); - ASSERT(CurrentItemPackage != NULL); - CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter])); - ASSERT(CurrentItemPackage->Name != NULL); - StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]); + if (CurrentItemPackage == NULL) { + ShellCommandLineFreeVarList(*CheckPackage); + *CheckPackage = NULL; + return (EFI_OUT_OF_RESOURCES); + } + CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]); + if (CurrentItemPackage->Name == NULL) { + ShellCommandLineFreeVarList(*CheckPackage); + *CheckPackage = NULL; + return (EFI_OUT_OF_RESOURCES); + } CurrentItemPackage->Type = CurrentItemType; CurrentItemPackage->OriginalPosition = (UINTN)(-1); CurrentItemPackage->Value = NULL; @@ -1971,6 +2123,7 @@ InternalCommandLineParse ( // possibly trigger the next loop(s) to populate the value of this item // case TypeValue: + case TypeTimeValue: GetItemValue = 1; ValueSize = 0; break; @@ -1990,43 +2143,67 @@ InternalCommandLineParse ( ASSERT(GetItemValue == 0); break; } - } else if (GetItemValue != 0 && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers)) { - ASSERT(CurrentItemPackage != NULL); + } else if (GetItemValue != 0 && CurrentItemPackage != NULL && !InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, (BOOLEAN)(CurrentItemPackage->Type == TypeTimeValue))) { // // get the item VALUE for a previous flag // - CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value); - ASSERT(CurrentItemPackage->Value != NULL); + CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16); + NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value); + if (NewValue == NULL) { + SHELL_FREE_NON_NULL (CurrentItemPackage->Value); + SHELL_FREE_NON_NULL (CurrentItemPackage); + ShellCommandLineFreeVarList (*CheckPackage); + *CheckPackage = NULL; + return EFI_OUT_OF_RESOURCES; + } + CurrentItemPackage->Value = NewValue; if (ValueSize == 0) { - StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]); + StrCpyS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + Argv[LoopCounter] + ); } else { - StrCat(CurrentItemPackage->Value, L" "); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); + StrCatS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + L" " + ); + StrCatS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + Argv[LoopCounter] + ); } ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); + GetItemValue--; if (GetItemValue == 0) { InsertHeadList(*CheckPackage, &CurrentItemPackage->Link); } - } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers) ){ //|| ProblemParam == NULL) { + } else if (!InternalIsFlag(Argv[LoopCounter], AlwaysAllowNumbers, FALSE)){ // // add this one as a non-flag // TempPointer = Argv[LoopCounter]; - if ((*TempPointer == L'^' && *(TempPointer+1) == L'-') + if ((*TempPointer == L'^' && *(TempPointer+1) == L'-') || (*TempPointer == L'^' && *(TempPointer+1) == L'/') || (*TempPointer == L'^' && *(TempPointer+1) == L'+') ){ TempPointer++; } CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE)); - ASSERT(CurrentItemPackage != NULL); + if (CurrentItemPackage == NULL) { + ShellCommandLineFreeVarList(*CheckPackage); + *CheckPackage = NULL; + return (EFI_OUT_OF_RESOURCES); + } CurrentItemPackage->Name = NULL; CurrentItemPackage->Type = TypePosition; - CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer)); - ASSERT(CurrentItemPackage->Value != NULL); - StrCpy(CurrentItemPackage->Value, TempPointer); + CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer); + if (CurrentItemPackage->Value == NULL) { + ShellCommandLineFreeVarList(*CheckPackage); + *CheckPackage = NULL; + return (EFI_OUT_OF_RESOURCES); + } CurrentItemPackage->OriginalPosition = Count++; InsertHeadList(*CheckPackage, &CurrentItemPackage->Link); } else { @@ -2034,9 +2211,7 @@ InternalCommandLineParse ( // this was a non-recognised flag... error! // if (ProblemParam != NULL) { - *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter])); - ASSERT(*ProblemParam != NULL); - StrCpy(*ProblemParam, Argv[LoopCounter]); + *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]); } ShellCommandLineFreeVarList(*CheckPackage); *CheckPackage = NULL; @@ -2210,14 +2385,9 @@ ShellCommandLineGetFlag ( CHAR16 *TempString; // - // ASSERT that both CheckPackage and KeyString aren't NULL - // - ASSERT(KeyString != NULL); - - // - // return FALSE for no package + // return FALSE for no package or KeyString is NULL // - if (CheckPackage == NULL) { + if (CheckPackage == NULL || KeyString == NULL) { return (FALSE); } @@ -2279,9 +2449,9 @@ ShellCommandLineGetValue ( CHAR16 *TempString; // - // check for CheckPackage == NULL + // return NULL for no package or KeyString is NULL // - if (CheckPackage == NULL) { + if (CheckPackage == NULL || KeyString == NULL) { return (NULL); } @@ -2400,7 +2570,7 @@ ShellCommandLineGetCount( } /** - Determins if a parameter is duplicated. + Determines if a parameter is duplicated. If Param is not NULL then it will point to a callee allocated string buffer with the parameter value if a duplicate is found. @@ -2453,14 +2623,14 @@ ShellCommandLineCheckDuplicate ( If the string would grow bigger than NewSize it will halt and return error. - @param[in] SourceString The string with source buffer. - @param[in,out] NewString The string with resultant buffer. - @param[in] NewSize The size in bytes of NewString. - @param[in] FindTarget The string to look for. - @param[in] ReplaceWith The string to replace FindTarget with. - @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^' - immediately before it. - @param[in] ParameterReplacing If TRUE will add "" around items with spaces. + @param[in] SourceString The string with source buffer. + @param[in, out] NewString The string with resultant buffer. + @param[in] NewSize The size in bytes of NewString. + @param[in] FindTarget The string to look for. + @param[in] ReplaceWith The string to replace FindTarget with. + @param[in] SkipPreCarrot If TRUE will skip a FindTarget that has a '^' + immediately before it. + @param[in] ParameterReplacing If TRUE will add "" around items with spaces. @retval EFI_INVALID_PARAMETER SourceString was NULL. @retval EFI_INVALID_PARAMETER NewString was NULL. @@ -2501,12 +2671,14 @@ ShellCopySearchAndReplace( Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0); } else { Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16)); - UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith); + if (Replace != NULL) { + UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith); + } } if (Replace == NULL) { return (EFI_OUT_OF_RESOURCES); } - NewString = SetMem16(NewString, NewSize, CHAR_NULL); + NewString = ZeroMem(NewString, NewSize); while (*SourceString != CHAR_NULL) { // // if we find the FindTarget and either Skip == FALSE or Skip and we @@ -2521,14 +2693,14 @@ ShellCopySearchAndReplace( FreePool(Replace); return (EFI_BUFFER_TOO_SMALL); } - StrCat(NewString, Replace); + StrCatS(NewString, NewSize/sizeof(CHAR16), Replace); } else { Size = StrSize(NewString); if (Size + sizeof(CHAR16) > NewSize) { FreePool(Replace); return (EFI_BUFFER_TOO_SMALL); } - StrnCat(NewString, SourceString, 1); + StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1); SourceString++; } } @@ -2547,7 +2719,6 @@ ShellCopySearchAndReplace( @retval !EFI_SUCCESS The operation failed. **/ EFI_STATUS -EFIAPI InternalPrintTo ( IN CONST CHAR16 *String ) @@ -2561,10 +2732,12 @@ InternalPrintTo ( return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String)); } if (mEfiShellInterface != NULL) { + if (mEfiShellInterface->RedirArgc == 0) { // // Divide in half for old shell. Must be string length not size. - // - Size /= 2; + // + Size /=2; // Divide in half only when no redirection. + } return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String)); } ASSERT(FALSE); @@ -2601,7 +2774,6 @@ InternalPrintTo ( @return EFI_DEVICE_ERROR The console device reported an error. **/ EFI_STATUS -EFIAPI InternalShellPrintWorker( IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, @@ -2674,7 +2846,12 @@ InternalShellPrintWorker( // update the attribute // if (ResumeLocation != NULL) { - if (*(ResumeLocation-1) == L'^') { + if ((ResumeLocation != mPostReplaceFormat2) && (*(ResumeLocation-1) == L'^')) { + // + // Move cursor back 1 position to overwrite the ^ + // + gST->ConOut->SetCursorPosition(gST->ConOut, gST->ConOut->Mode->CursorColumn - 1, gST->ConOut->Mode->CursorRow); + // // Print a simple '%' symbol // @@ -2692,10 +2869,10 @@ InternalShellPrintWorker( gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))); break; case (L'B'): - gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))); + gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_LIGHTBLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))); break; case (L'V'): - gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))); + gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_LIGHTGREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))); break; default: // @@ -2825,13 +3002,14 @@ ShellPrintHiiEx( CHAR16 *HiiFormatString; EFI_STATUS RetVal; + RetVal = EFI_DEVICE_ERROR; + VA_START (Marker, HiiFormatHandle); HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language); - ASSERT(HiiFormatString != NULL); - - RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker); - - SHELL_FREE_NON_NULL(HiiFormatString); + if (HiiFormatString != NULL) { + RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker); + SHELL_FREE_NON_NULL (HiiFormatString); + } VA_END(Marker); return (RetVal); @@ -2842,9 +3020,10 @@ ShellPrintHiiEx( @param[in] DirName Path to directory to test. - @retval EFI_SUCCESS The Path represents a directory - @retval EFI_NOT_FOUND The Path does not represent a directory - @return other The path failed to open + @retval EFI_SUCCESS The Path represents a directory + @retval EFI_NOT_FOUND The Path does not represent a directory + @retval EFI_OUT_OF_RESOURCES A memory allocation failed. + @return The path failed to open **/ EFI_STATUS EFIAPI @@ -2869,6 +3048,10 @@ ShellIsDirectory( // if (gEfiShellProtocol != NULL) { TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0); + if (TempLocation == NULL) { + ShellCloseFile(&Handle); + return (EFI_OUT_OF_RESOURCES); + } TempLocation2 = StrStr(TempLocation, L":"); if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) { *(TempLocation2+1) = CHAR_NULL; @@ -2968,9 +3151,35 @@ ShellIsFileInPath( return (Status); } +/** + Function return the number converted from a hex representation of a number. + + Note: this function cannot be used when (UINTN)(-1), (0xFFFFFFFF) may be a valid + result. Use ShellConvertStringToUint64 instead. + + @param[in] String String representation of a number. + + @return The unsigned integer result of the conversion. + @retval (UINTN)(-1) An error occured. +**/ +UINTN +EFIAPI +ShellHexStrToUintn( + IN CONST CHAR16 *String + ) +{ + UINT64 RetVal; + + if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) { + return ((UINTN)RetVal); + } + + return ((UINTN)(-1)); +} + /** Function to determine whether a string is decimal or hex representation of a number - and return the number converted from the string. + and return the number converted from the string. Spaces are always skipped. @param[in] String String representation of a number @@ -2988,7 +3197,7 @@ ShellStrToUintn( Hex = FALSE; - if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) { + if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) { Hex = TRUE; } @@ -3020,8 +3229,8 @@ ShellStrToUintn( if Destination's current length (including NULL terminator) is already more then CurrentSize, then ASSERT() - @param[in,out] Destination The String to append onto - @param[in,out] CurrentSize on call the number of bytes in Destination. On + @param[in, out] Destination The String to append onto + @param[in, out] CurrentSize on call the number of bytes in Destination. On return possibly the new size (still in bytes). if NULL then allocate whatever is needed. @param[in] Source The String to append from @@ -3085,15 +3294,16 @@ StrnCatGrow ( // if (CurrentSize != NULL) { NewSize = *CurrentSize; - while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) { - NewSize += 2 * Count * sizeof(CHAR16); + if (NewSize < DestinationStartSize + (Count * sizeof(CHAR16))) { + while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) { + NewSize += 2 * Count * sizeof(CHAR16); + } + *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination); + *CurrentSize = NewSize; } - *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination); - ASSERT(*Destination != NULL); - *CurrentSize = NewSize; } else { - *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16)); - ASSERT(*Destination != NULL); + NewSize = (Count+1)*sizeof(CHAR16); + *Destination = AllocateZeroPool(NewSize); } // @@ -3102,14 +3312,16 @@ StrnCatGrow ( if (*Destination == NULL) { return (NULL); } - return StrnCat(*Destination, Source, Count); + + StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count); + return *Destination; } /** Prompt the user and return the resultant answer to the requestor. This function will display the requested question on the shell prompt and then - wait for an apropriate answer to be input from the console. + wait for an appropriate answer to be input from the console. if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE. @@ -3165,7 +3377,9 @@ ShellPromptForResponse ( // gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex); Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + break; + } ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar); if (Key.UnicodeChar == L'Q' || Key.UnicodeChar ==L'q') { *Resp = ShellPromptResponseQuit; @@ -3182,9 +3396,15 @@ ShellPromptForResponse ( // *Resp = ShellPromptResponseMax; while (*Resp == ShellPromptResponseMax) { + if (ShellGetExecutionBreakFlag()) { + Status = EFI_ABORTED; + break; + } gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex); Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + break; + } ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar); switch (Key.UnicodeChar) { case L'Y': @@ -3201,7 +3421,8 @@ ShellPromptForResponse ( break; } } - break; case ShellPromptResponseTypeYesNoAllCancel: + break; + case ShellPromptResponseTypeYesNoAllCancel: if (Prompt != NULL) { ShellPrintEx(-1, -1, L"%s", Prompt); } @@ -3210,10 +3431,20 @@ ShellPromptForResponse ( // *Resp = ShellPromptResponseMax; while (*Resp == ShellPromptResponseMax) { + if (ShellGetExecutionBreakFlag()) { + Status = EFI_ABORTED; + break; + } gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex); Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); - ASSERT_EFI_ERROR(Status); - ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar); + if (EFI_ERROR(Status)) { + break; + } + + if (Key.UnicodeChar <= 127 && Key.UnicodeChar >= 32) { + ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar); + } + switch (Key.UnicodeChar) { case L'Y': case L'y': @@ -3244,10 +3475,16 @@ ShellPromptForResponse ( // *Resp = ShellPromptResponseMax; while (*Resp == ShellPromptResponseMax) { + if (ShellGetExecutionBreakFlag()) { + Status = EFI_ABORTED; + break; + } gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex); if (Type == ShellPromptResponseTypeEnterContinue) { Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + break; + } ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar); if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) { *Resp = ShellPromptResponseContinue; @@ -3271,9 +3508,15 @@ ShellPromptForResponse ( // *Resp = ShellPromptResponseMax; while (*Resp == ShellPromptResponseMax) { + if (ShellGetExecutionBreakFlag()) { + Status = EFI_ABORTED; + break; + } gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex); Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + break; + } ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar); switch (Key.UnicodeChar) { case L'Y': @@ -3292,9 +3535,15 @@ ShellPromptForResponse ( ShellPrintEx(-1, -1, L"%s", Prompt); } while(1) { + if (ShellGetExecutionBreakFlag()) { + Status = EFI_ABORTED; + break; + } gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex); Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); - ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status)) { + break; + } ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar); if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) { break; @@ -3305,6 +3554,7 @@ ShellPromptForResponse ( break; // // This is the location to add new prompt types. + // If your new type loops remember to add ExecutionBreak support. // default: ASSERT(FALSE); @@ -3372,16 +3622,17 @@ ShellPromptForResponseHii ( @param[in] String The string to evaluate. @param[in] ForceHex TRUE - always assume hex. @param[in] StopAtSpace TRUE to halt upon finding a space, FALSE to keep going. + @param[in] TimeNumbers TRUE to allow numbers with ":", FALSE otherwise. @retval TRUE It is all numeric (dec/hex) characters. @retval FALSE There is a non-numeric character. **/ BOOLEAN -EFIAPI InternalShellIsHexOrDecimalNumber ( IN CONST CHAR16 *String, IN CONST BOOLEAN ForceHex, - IN CONST BOOLEAN StopAtSpace + IN CONST BOOLEAN StopAtSpace, + IN CONST BOOLEAN TimeNumbers ) { BOOLEAN Hex; @@ -3392,7 +3643,7 @@ InternalShellIsHexOrDecimalNumber ( if (String != NULL && *String == L'-') { String++; } - + if (String == NULL) { return (FALSE); } @@ -3425,6 +3676,9 @@ InternalShellIsHexOrDecimalNumber ( // loop through the remaining characters and use the lib function // for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){ + if (TimeNumbers && (String[0] == L':')) { + continue; + } if (Hex) { if (!ShellIsHexaDecimalDigitCharacter(*String)) { return (FALSE); @@ -3471,7 +3725,7 @@ ShellFileExists( } /** - Convert a Unicode character to upper case only if + Convert a Unicode character to upper case only if it maps to a valid small-case ASCII character. This internal function only deal with Unicode character @@ -3486,7 +3740,6 @@ ShellFileExists( **/ CHAR16 -EFIAPI InternalShellCharToUpper ( IN CHAR16 Char ) @@ -3503,7 +3756,7 @@ InternalShellCharToUpper ( This internal function only deal with Unicode character which maps to a valid hexadecimal ASII character, i.e. - L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other + L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other Unicode character, the value returned does not make sense. @param Char The character to convert. @@ -3512,7 +3765,6 @@ InternalShellCharToUpper ( **/ UINTN -EFIAPI InternalShellHexCharToUintn ( IN CHAR16 Char ) @@ -3521,13 +3773,13 @@ InternalShellHexCharToUintn ( return Char - L'0'; } - return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A'); + return (10 + InternalShellCharToUpper (Char) - L'A'); } /** Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64. - This function returns a value of type UINTN by interpreting the contents + This function returns a value of type UINT64 by interpreting the contents of the Unicode string specified by String as a hexadecimal number. The format of the input Unicode string String is: @@ -3555,7 +3807,6 @@ InternalShellHexCharToUintn ( @retval EFI_DEVICE_ERROR An overflow occured. **/ EFI_STATUS -EFIAPI InternalShellStrHexToUint64 ( IN CONST CHAR16 *String, OUT UINT64 *Value, @@ -3567,9 +3818,9 @@ InternalShellStrHexToUint64 ( if (String == NULL || StrSize(String) == 0 || Value == NULL) { return (EFI_INVALID_PARAMETER); } - + // - // Ignore the pad spaces (space or tab) + // Ignore the pad spaces (space or tab) // while ((*String == L' ') || (*String == L'\t')) { String++; @@ -3593,18 +3844,18 @@ InternalShellStrHexToUint64 ( } Result = 0; - + // - // Skip spaces if requested + // there is a space where there should't be // - while (StopAtSpace && *String == L' ') { - String++; + if (*String == L' ') { + return (EFI_INVALID_PARAMETER); } - + while (ShellIsHexaDecimalDigitCharacter (*String)) { // - // If the Hex Number represented by String overflows according - // to the range defined by UINTN, then ASSERT(). + // If the Hex Number represented by String overflows according + // to the range defined by UINT64, then return EFI_DEVICE_ERROR. // if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) { // if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) { @@ -3616,10 +3867,10 @@ InternalShellStrHexToUint64 ( String++; // - // Skip spaces if requested + // stop at spaces if requested // - while (StopAtSpace && *String == L' ') { - String++; + if (StopAtSpace && *String == L' ') { + break; } } @@ -3657,7 +3908,6 @@ InternalShellStrHexToUint64 ( @retval EFI_DEVICE_ERROR An overflow occured. **/ EFI_STATUS -EFIAPI InternalShellStrDecimalToUint64 ( IN CONST CHAR16 *String, OUT UINT64 *Value, @@ -3687,17 +3937,20 @@ InternalShellStrDecimalToUint64 ( Result = 0; // - // Skip spaces if requested + // Stop upon space if requested + // (if the whole value was 0) // - while (StopAtSpace && *String == L' ') { - String++; + if (StopAtSpace && *String == L' ') { + *Value = Result; + return (EFI_SUCCESS); } + while (ShellIsDecimalDigitCharacter (*String)) { // - // If the number represented by String overflows according - // to the range defined by UINT64, then ASSERT(). + // If the number represented by String overflows according + // to the range defined by UINT64, then return EFI_DEVICE_ERROR. // - + if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) { return (EFI_DEVICE_ERROR); } @@ -3714,7 +3967,7 @@ InternalShellStrDecimalToUint64 ( } *Value = Result; - + return (EFI_SUCCESS); } @@ -3727,7 +3980,7 @@ InternalShellStrDecimalToUint64 ( @param[out] Value Upon a successful return the value of the conversion. @param[in] ForceHex TRUE - always assume hex. @param[in] StopAtSpace FALSE to skip spaces. - + @retval EFI_SUCCESS The conversion was successful. @retval EFI_INVALID_PARAMETER String contained an invalid character. @retval EFI_NOT_FOUND String was a number, but Value was NULL. @@ -3748,10 +4001,10 @@ ShellConvertStringToUint64( Hex = ForceHex; - if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) { + if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) { if (!Hex) { Hex = TRUE; - if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) { + if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace, FALSE)) { return (EFI_INVALID_PARAMETER); } } else { @@ -3767,9 +4020,9 @@ ShellConvertStringToUint64( // // make sure we have something left that is numeric. // - if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) { + if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace, FALSE)) { return (EFI_INVALID_PARAMETER); - } + } // // do the conversion. @@ -3824,11 +4077,12 @@ ShellIsHexOrDecimalNumber ( If the position upon start is 0, then the Ascii Boolean will be set. This should be maintained and not changed for all operations with the same file. - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). + @param[in] Handle SHELL_FILE_HANDLE to read from. + @param[in, out] Ascii Boolean value for indicating whether the file is + Ascii (TRUE) or UCS2 (FALSE). - @return The line of text from the file. + @return The line of text from the file. + @retval NULL There was not enough memory available. @sa ShellFileHandleReadLine **/ @@ -3849,9 +4103,15 @@ ShellFileHandleReturnLine( Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); if (Status == EFI_BUFFER_TOO_SMALL) { RetVal = AllocateZeroPool(Size); + if (RetVal == NULL) { + return (NULL); + } Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); + + } + if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) { + Status = EFI_SUCCESS; } - ASSERT_EFI_ERROR(Status); if (EFI_ERROR(Status) && (RetVal != NULL)) { FreePool(RetVal); RetVal = NULL; @@ -3865,20 +4125,32 @@ ShellFileHandleReturnLine( If the position upon start is 0, then the Ascii Boolean will be set. This should be maintained and not changed for all operations with the same file. - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Buffer The pointer to buffer to read into. - @param[in,out] Size The pointer to number of bytes in Buffer. - @param[in] Truncate If the buffer is large enough, this has no effect. - If the buffer is is too small and Truncate is TRUE, - the line will be truncated. - If the buffer is is too small and Truncate is FALSE, - then no read will occur. - - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). + NOTE: LINES THAT ARE RETURNED BY THIS FUNCTION ARE UCS2, EVEN IF THE FILE BEING READ + IS IN ASCII FORMAT. + + @param[in] Handle SHELL_FILE_HANDLE to read from. + @param[in, out] Buffer The pointer to buffer to read into. If this function + returns EFI_SUCCESS, then on output Buffer will + contain a UCS2 string, even if the file being + read is ASCII. + @param[in, out] Size On input, pointer to number of bytes in Buffer. + On output, unchanged unless Buffer is too small + to contain the next line of the file. In that + case Size is set to the number of bytes needed + to hold the next line of the file (as a UCS2 + string, even if it is an ASCII file). + @param[in] Truncate If the buffer is large enough, this has no effect. + If the buffer is is too small and Truncate is TRUE, + the line will be truncated. + If the buffer is is too small and Truncate is FALSE, + then no read will occur. + + @param[in, out] Ascii Boolean value for indicating whether the file is + Ascii (TRUE) or UCS2 (FALSE). @retval EFI_SUCCESS The operation was successful. The line is stored in Buffer. + @retval EFI_END_OF_FILE There are no more lines in the file. @retval EFI_INVALID_PARAMETER Handle was NULL. @retval EFI_INVALID_PARAMETER Size was NULL. @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line. @@ -3924,19 +4196,22 @@ ShellFileHandleReadLine( } } + if (*Ascii) { + CharSize = sizeof(CHAR8); + } else { + CharSize = sizeof(CHAR16); + } for (CountSoFar = 0;;CountSoFar++){ CharBuffer = 0; - if (*Ascii) { - CharSize = sizeof(CHAR8); - } else { - CharSize = sizeof(CHAR16); - } Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); if ( EFI_ERROR(Status) || CharSize == 0 || (CharBuffer == L'\n' && !(*Ascii)) || (CharBuffer == '\n' && *Ascii) ){ + if (CharSize == 0) { + Status = EFI_END_OF_FILE; + } break; } // @@ -3967,3 +4242,145 @@ ShellFileHandleReadLine( return (Status); } + +/** + Function to print help file / man page content in the spec from the UEFI Shell protocol GetHelpText function. + + @param[in] CommandToGetHelpOn Pointer to a string containing the command name of help file to be printed. + @param[in] SectionToGetHelpOn Pointer to the section specifier(s). + @param[in] PrintCommandText If TRUE, prints the command followed by the help content, otherwise prints + the help content only. + @retval EFI_DEVICE_ERROR The help data format was incorrect. + @retval EFI_NOT_FOUND The help data could not be found. + @retval EFI_SUCCESS The operation was successful. +**/ +EFI_STATUS +EFIAPI +ShellPrintHelp ( + IN CONST CHAR16 *CommandToGetHelpOn, + IN CONST CHAR16 *SectionToGetHelpOn, + IN BOOLEAN PrintCommandText + ) +{ + EFI_STATUS Status; + CHAR16 *OutText; + + OutText = NULL; + + // + // Get the string to print based + // + Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText); + + // + // make sure we got a valid string + // + if (EFI_ERROR(Status)){ + return Status; + } + if (OutText == NULL || StrLen(OutText) == 0) { + return EFI_NOT_FOUND; + } + + // + // Chop off trailing stuff we dont need + // + while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') { + OutText[StrLen(OutText)-1] = CHAR_NULL; + } + + // + // Print this out to the console + // + if (PrintCommandText) { + ShellPrintEx(-1, -1, L"%H%-14s%N- %s\r\n", CommandToGetHelpOn, OutText); + } else { + ShellPrintEx(-1, -1, L"%N%s\r\n", OutText); + } + + SHELL_FREE_NON_NULL(OutText); + + return EFI_SUCCESS; +} + +/** + Function to delete a file by name + + @param[in] FileName Pointer to file name to delete. + + @retval EFI_SUCCESS the file was deleted sucessfully + @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not + deleted + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_NOT_FOUND The specified file could not be found on the + device or the file system could not be found + on the device. + @retval EFI_NO_MEDIA The device has no medium. + @retval EFI_MEDIA_CHANGED The device has a different medium in it or the + medium is no longer supported. + @retval EFI_DEVICE_ERROR The device reported an error. + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. + @retval EFI_WRITE_PROTECTED The file or medium is write protected. + @retval EFI_ACCESS_DENIED The file was opened read only. + @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the + file. + @retval other The file failed to open +**/ +EFI_STATUS +EFIAPI +ShellDeleteFileByName( + IN CONST CHAR16 *FileName + ) +{ + EFI_STATUS Status; + SHELL_FILE_HANDLE FileHandle; + + Status = ShellFileExists(FileName); + + if (Status == EFI_SUCCESS){ + Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0); + if (Status == EFI_SUCCESS){ + Status = ShellDeleteFile(&FileHandle); + } + } + + return(Status); + +} + +/** + Cleans off all the quotes in the string. + + @param[in] OriginalString pointer to the string to be cleaned. + @param[out] CleanString The new string with all quotes removed. + Memory allocated in the function and free + by caller. + + @retval EFI_SUCCESS The operation was successful. +**/ +EFI_STATUS +InternalShellStripQuotes ( + IN CONST CHAR16 *OriginalString, + OUT CHAR16 **CleanString + ) +{ + CHAR16 *Walker; + + if (OriginalString == NULL || CleanString == NULL) { + return EFI_INVALID_PARAMETER; + } + + *CleanString = AllocateCopyPool (StrSize (OriginalString), OriginalString); + if (*CleanString == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (Walker = *CleanString; Walker != NULL && *Walker != CHAR_NULL ; Walker++) { + if (*Walker == L'\"') { + CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0])); + } + } + + return EFI_SUCCESS; +} +