X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellLib%2FUefiShellLib.c;h=e53985e2d71406a45970fd618927f36e280dc0a9;hp=1929349b2038aa2a7f4f197eb532ef442d7459b7;hb=ee33344c595709ef7642c52d27a16f21e692b856;hpb=06c355b42fd112da4a439ba225aa666767d02838 diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Library/UefiShellLib/UefiShellLib.c index 1929349b20..e53985e2d7 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 - 2012, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ Copyright 2016 Dell Inc. + Copyright (c) 2006 - 2018, 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. @@ -88,7 +89,6 @@ ShellIsDecimalDigitCharacter ( @retval EFI_OUT_OF_RESOURCES Memory allocation failed. **/ EFI_STATUS -EFIAPI ShellFindSE2 ( IN EFI_HANDLE ImageHandle ) @@ -172,7 +172,6 @@ ShellFindSE2 ( @retval EFI_SUCCESS The operationw as successful. **/ EFI_STATUS -EFIAPI ShellLibConstructorWorker ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable @@ -180,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 // @@ -239,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; @@ -293,6 +301,7 @@ ShellLibConstructor ( gEfiShellParametersProtocol = NULL; mEfiShellInterface = NULL; mEfiShellEnvironment2Handle = NULL; + mUnicodeCollationProtocol = NULL; // // verify that auto initialize is not set false @@ -320,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); } @@ -369,8 +388,11 @@ ShellLibDestructor ( EFI_STATUS EFIAPI ShellInitialize ( + VOID ) { + EFI_STATUS Status; + // // if auto initialize is not false then skip // @@ -381,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 @@ -486,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); @@ -551,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 ); @@ -569,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 // @@ -640,6 +694,8 @@ ShellOpenFileByName( EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_STATUS Status; EFI_FILE_INFO *FileInfo; + CHAR16 *FileNameCopy; + EFI_STATUS Status2; // // ASSERT if FileName is NULL @@ -651,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); } @@ -1144,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. @@ -1172,6 +1268,7 @@ ShellExecute ( OUT EFI_STATUS *Status OPTIONAL ) { + EFI_STATUS CmdStatus; // // Check for UEFI Shell 2.0 protocols // @@ -1190,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 @@ -1207,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 @@ -1326,7 +1438,6 @@ typedef struct { @retval the resultant head of the double linked new format list; **/ LIST_ENTRY* -EFIAPI InternalShellConvertFileListType ( IN LIST_ENTRY *FileList, IN OUT LIST_ENTRY *ListHead @@ -1385,26 +1496,30 @@ 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 // 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); + + // + // Free the previously converted stuff + // ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead)); ListHead = NULL; break; } - // - // 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); - // // add that to the list // @@ -1416,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(). @@ -1445,6 +1560,7 @@ ShellOpenFileMetaArg ( { EFI_STATUS Status; LIST_ENTRY mOldStyleFileList; + CHAR16 *CleanFilePathStr; // // ASSERT that Arg and ListHead are not NULL @@ -1452,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 // @@ -1459,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)) { @@ -1473,9 +1597,11 @@ ShellOpenFileMetaArg ( } if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) { FreePool(*ListHead); + FreePool(CleanFilePathStr); *ListHead = NULL; return (EFI_NOT_FOUND); } + FreePool(CleanFilePathStr); return (Status); } @@ -1491,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)); @@ -1520,9 +1648,11 @@ ShellOpenFileMetaArg ( *ListHead = NULL; Status = EFI_NOT_FOUND; } + FreePool(CleanFilePathStr); return (Status); } + FreePool(CleanFilePathStr); return (EFI_UNSUPPORTED); } /** @@ -1567,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; } @@ -1619,14 +1750,15 @@ ShellFindFilePath ( Path = ShellGetEnvironmentVariable(L"cwd"); if (Path != NULL) { - Size = StrSize(Path); + Size = StrSize(Path) + sizeof(CHAR16); Size += StrSize(FileName); TestPath = AllocateZeroPool(Size); 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) { @@ -1658,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 { @@ -1732,9 +1864,9 @@ ShellFindFilePathEx ( 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) { @@ -1775,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, @@ -1838,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 ) { // @@ -1857,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); } @@ -1899,7 +2031,6 @@ InternalIsFlag ( ProblemParam if provided. **/ EFI_STATUS -EFIAPI InternalCommandLineParse ( IN CONST SHELL_PARAM_ITEM *CheckList, OUT LIST_ENTRY **CheckPackage, @@ -1917,6 +2048,8 @@ InternalCommandLineParse ( UINTN ValueSize; UINTN Count; CONST CHAR16 *TempPointer; + UINTN CurrentValueSize; + CHAR16 *NewValue; CurrentItemPackage = NULL; GetItemValue = 0; @@ -1972,13 +2105,12 @@ InternalCommandLineParse ( *CheckPackage = NULL; return (EFI_OUT_OF_RESOURCES); } - CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter])); + CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]); if (CurrentItemPackage->Name == NULL) { ShellCommandLineFreeVarList(*CheckPackage); *CheckPackage = NULL; return (EFI_OUT_OF_RESOURCES); } - StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]); CurrentItemPackage->Type = CurrentItemType; CurrentItemPackage->OriginalPosition = (UINTN)(-1); CurrentItemPackage->Value = NULL; @@ -1991,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; @@ -2010,44 +2143,42 @@ 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 // - if (StrStr(Argv[LoopCounter], L" ") == NULL) { - CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value); - ASSERT(CurrentItemPackage->Value != NULL); - if (ValueSize == 0) { - StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]); - } else { - StrCat(CurrentItemPackage->Value, L" "); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); - } - ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); + 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) { + StrCpyS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + Argv[LoopCounter] + ); } else { - // - // the parameter has spaces. must be quoted. - // - CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16), CurrentItemPackage->Value); - ASSERT(CurrentItemPackage->Value != NULL); - if (ValueSize == 0) { - StrCpy(CurrentItemPackage->Value, L"\""); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); - StrCat(CurrentItemPackage->Value, L"\""); - } else { - StrCat(CurrentItemPackage->Value, L" "); - StrCat(CurrentItemPackage->Value, L"\""); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); - StrCat(CurrentItemPackage->Value, L"\""); - } - ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); + 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 // @@ -2067,13 +2198,12 @@ InternalCommandLineParse ( } CurrentItemPackage->Name = NULL; CurrentItemPackage->Type = TypePosition; - CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer)); + CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer); if (CurrentItemPackage->Value == NULL) { ShellCommandLineFreeVarList(*CheckPackage); *CheckPackage = NULL; return (EFI_OUT_OF_RESOURCES); } - StrCpy(CurrentItemPackage->Value, TempPointer); CurrentItemPackage->OriginalPosition = Count++; InsertHeadList(*CheckPackage, &CurrentItemPackage->Link); } else { @@ -2081,10 +2211,7 @@ InternalCommandLineParse ( // this was a non-recognised flag... error! // if (ProblemParam != NULL) { - *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter])); - if (*ProblemParam != NULL) { - StrCpy(*ProblemParam, Argv[LoopCounter]); - } + *ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]); } ShellCommandLineFreeVarList(*CheckPackage); *CheckPackage = NULL; @@ -2443,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. @@ -2551,7 +2678,7 @@ ShellCopySearchAndReplace( 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 @@ -2566,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++; } } @@ -2592,7 +2719,6 @@ ShellCopySearchAndReplace( @retval !EFI_SUCCESS The operation failed. **/ EFI_STATUS -EFIAPI InternalPrintTo ( IN CONST CHAR16 *String ) @@ -2648,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, @@ -2721,7 +2846,7 @@ 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 ^ // @@ -2744,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: // @@ -2877,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); @@ -3025,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 @@ -3045,7 +3197,7 @@ ShellStrToUintn( Hex = FALSE; - if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) { + if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) { Hex = TRUE; } @@ -3142,13 +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); - *CurrentSize = NewSize; } else { - *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16)); + NewSize = (Count+1)*sizeof(CHAR16); + *Destination = AllocateZeroPool(NewSize); } // @@ -3157,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. @@ -3220,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; @@ -3237,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': @@ -3256,7 +3421,8 @@ ShellPromptForResponse ( break; } } - break; case ShellPromptResponseTypeYesNoAllCancel: + break; + case ShellPromptResponseTypeYesNoAllCancel: if (Prompt != NULL) { ShellPrintEx(-1, -1, L"%s", Prompt); } @@ -3265,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': @@ -3299,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; @@ -3326,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': @@ -3347,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; @@ -3360,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); @@ -3427,42 +3622,50 @@ 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; + BOOLEAN LeadingZero; + + if (String == NULL) { + return FALSE; + } // // chop off a single negative sign // - if (String != NULL && *String == L'-') { + if (*String == L'-') { String++; } - if (String == NULL) { - return (FALSE); + if (*String == CHAR_NULL) { + return FALSE; } // // chop leading zeroes // - while(String != NULL && *String == L'0'){ + LeadingZero = FALSE; + while(*String == L'0'){ String++; + LeadingZero = TRUE; } // // allow '0x' or '0X', but not 'x' or 'X' // - if (String != NULL && (*String == L'x' || *String == L'X')) { - if (*(String-1) != L'0') { + if (*String == L'x' || *String == L'X') { + if (!LeadingZero) { // // we got an x without a preceeding 0 // @@ -3479,7 +3682,10 @@ InternalShellIsHexOrDecimalNumber ( // // loop through the remaining characters and use the lib function // - for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){ + for ( ; *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){ + if (TimeNumbers && (String[0] == L':')) { + continue; + } if (Hex) { if (!ShellIsHexaDecimalDigitCharacter(*String)) { return (FALSE); @@ -3541,7 +3747,6 @@ ShellFileExists( **/ CHAR16 -EFIAPI InternalShellCharToUpper ( IN CHAR16 Char ) @@ -3567,7 +3772,6 @@ InternalShellCharToUpper ( **/ UINTN -EFIAPI InternalShellHexCharToUintn ( IN CHAR16 Char ) @@ -3576,13 +3780,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: @@ -3610,7 +3814,6 @@ InternalShellHexCharToUintn ( @retval EFI_DEVICE_ERROR An overflow occured. **/ EFI_STATUS -EFIAPI InternalShellStrHexToUint64 ( IN CONST CHAR16 *String, OUT UINT64 *Value, @@ -3650,16 +3853,16 @@ 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(). + // 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))) { @@ -3712,7 +3915,6 @@ InternalShellStrHexToUint64 ( @retval EFI_DEVICE_ERROR An overflow occured. **/ EFI_STATUS -EFIAPI InternalShellStrDecimalToUint64 ( IN CONST CHAR16 *String, OUT UINT64 *Value, @@ -3742,15 +3944,18 @@ 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(). + // to the range defined by UINT64, then return EFI_DEVICE_ERROR. // if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) { @@ -3803,10 +4008,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 { @@ -3822,7 +4027,7 @@ 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); } @@ -3911,6 +4116,9 @@ ShellFileHandleReturnLine( Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); } + if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) { + Status = EFI_SUCCESS; + } if (EFI_ERROR(Status) && (RetVal != NULL)) { FreePool(RetVal); RetVal = NULL; @@ -3924,9 +4132,20 @@ 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. + 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. - @param[in, out] Size The pointer to number of bytes in Buffer. + @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. @@ -3938,6 +4157,7 @@ ShellFileHandleReturnLine( @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. @@ -3983,19 +4203,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; } // @@ -4026,3 +4249,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; +} +