X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;ds=sidebyside;f=ShellPkg%2FLibrary%2FUefiShellLib%2FUefiShellLib.c;h=2e6301d70a6dce59ebcbef1628d9070a8e547e6c;hb=a8577b3a65e2aad01b1c69338ab1c63273d073f6;hp=1929349b2038aa2a7f4f197eb532ef442d7459b7;hpb=12ea46947dbe411cc278a8893dd9df4587d9c075;p=mirror_edk2.git diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Library/UefiShellLib/UefiShellLib.c index 1929349b20..2e6301d70a 100644 --- a/ShellPkg/Library/UefiShellLib/UefiShellLib.c +++ b/ShellPkg/Library/UefiShellLib/UefiShellLib.c @@ -1,7 +1,7 @@ /** @file Provides interface to shell functionality for shell commands and applications. - Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2015, 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 @@ -15,6 +15,7 @@ #include "UefiShellLib.h" #include #include +#include #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN) @@ -486,6 +487,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 +554,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 +595,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 +672,7 @@ ShellOpenFileByName( EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_STATUS Status; EFI_FILE_INFO *FileInfo; + CHAR16 *FileNameCopy; // // ASSERT if FileName is NULL @@ -651,11 +684,32 @@ 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 (FileName == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + PathCleanUpDirectories (FileNameCopy); + if (PathRemoveLastItem (FileNameCopy)) { + 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, @@ -1144,7 +1198,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 +1226,7 @@ ShellExecute ( OUT EFI_STATUS *Status OPTIONAL ) { + EFI_STATUS CmdStatus; // // Check for UEFI Shell 2.0 protocols // @@ -1190,16 +1245,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 @@ -1385,26 +1453,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 +1488,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 +1517,7 @@ ShellOpenFileMetaArg ( { EFI_STATUS Status; LIST_ENTRY mOldStyleFileList; + CHAR16 *CleanFilePathStr; // // ASSERT that Arg and ListHead are not NULL @@ -1452,6 +1525,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 +1539,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 +1554,11 @@ ShellOpenFileMetaArg ( } if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) { FreePool(*ListHead); + FreePool(CleanFilePathStr); *ListHead = NULL; return (EFI_NOT_FOUND); } + FreePool(CleanFilePathStr); return (Status); } @@ -1491,15 +1574,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 +1605,11 @@ ShellOpenFileMetaArg ( *ListHead = NULL; Status = EFI_NOT_FOUND; } + FreePool(CleanFilePathStr); return (Status); } + FreePool(CleanFilePathStr); return (EFI_UNSUPPORTED); } /** @@ -1567,6 +1654,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; } @@ -1625,8 +1713,8 @@ ShellFindFilePath ( if (TestPath == NULL) { return (NULL); } - StrCpy(TestPath, Path); - StrCat(TestPath, FileName); + StrnCpy(TestPath, Path, Size/sizeof(CHAR16) - 1); + StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0); if (!EFI_ERROR(Status)){ if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) { @@ -1658,12 +1746,12 @@ ShellFindFilePath ( *TempChar = CHAR_NULL; } if (TestPath[StrLen(TestPath)-1] != L'\\') { - StrCat(TestPath, L"\\"); + StrnCat(TestPath, L"\\", Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); } if (FileName[0] == L'\\') { FileName++; } - StrCat(TestPath, FileName); + StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); if (StrStr(Walker, L";") != NULL) { Walker = StrStr(Walker, L";") + 1; } else { @@ -1732,9 +1820,9 @@ ShellFindFilePathEx ( return (NULL); } for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){ - StrCpy(TestPath, FileName); + StrnCpy(TestPath, FileName, Size/sizeof(CHAR16) - 1); if (ExtensionWalker != NULL) { - StrCat(TestPath, ExtensionWalker); + StrnCat(TestPath, ExtensionWalker, Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); } TempChar = StrStr(TestPath, L";"); if (TempChar != NULL) { @@ -1838,6 +1926,7 @@ 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. @@ -1846,7 +1935,8 @@ BOOLEAN EFIAPI InternalIsFlag ( IN CONST CHAR16 *Name, - IN BOOLEAN AlwaysAllowNumbers + IN CONST BOOLEAN AlwaysAllowNumbers, + IN CONST BOOLEAN TimeNumbers ) { // @@ -1857,7 +1947,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); } @@ -1917,6 +2007,7 @@ InternalCommandLineParse ( UINTN ValueSize; UINTN Count; CONST CHAR16 *TempPointer; + UINTN CurrentValueSize; CurrentItemPackage = NULL; GetItemValue = 0; @@ -1972,13 +2063,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 +2081,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,36 +2101,37 @@ 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, 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); + CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16); + CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value); ASSERT(CurrentItemPackage->Value != NULL); if (ValueSize == 0) { - StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]); + StrnCpy(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1); } else { - StrCat(CurrentItemPackage->Value, L" "); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); + StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); + StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); } ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); } else { // // the parameter has spaces. must be quoted. // - CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16), CurrentItemPackage->Value); + CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16); + CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value); ASSERT(CurrentItemPackage->Value != NULL); if (ValueSize == 0) { - StrCpy(CurrentItemPackage->Value, L"\""); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); - StrCat(CurrentItemPackage->Value, L"\""); + StrnCpy(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1); + StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); + StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); } else { - StrCat(CurrentItemPackage->Value, L" "); - StrCat(CurrentItemPackage->Value, L"\""); - StrCat(CurrentItemPackage->Value, Argv[LoopCounter]); - StrCat(CurrentItemPackage->Value, L"\""); + StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); + StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); + StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); + StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); } ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); } @@ -2047,7 +2139,7 @@ InternalCommandLineParse ( 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 +2159,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 +2172,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; @@ -2551,7 +2639,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,7 +2654,7 @@ ShellCopySearchAndReplace( FreePool(Replace); return (EFI_BUFFER_TOO_SMALL); } - StrCat(NewString, Replace); + StrnCat(NewString, Replace, NewSize/sizeof(CHAR16) - 1 - StrLen(NewString)); } else { Size = StrSize(NewString); if (Size + sizeof(CHAR16) > NewSize) { @@ -3025,9 +3113,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 +3159,7 @@ ShellStrToUintn( Hex = FALSE; - if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) { + if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE, FALSE)) { Hex = TRUE; } @@ -3142,11 +3256,13 @@ 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)); } @@ -3220,7 +3336,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 +3355,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': @@ -3265,9 +3389,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': @@ -3299,10 +3429,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 +3462,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 +3489,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 +3508,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,6 +3576,7 @@ 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. @@ -3436,7 +3586,8 @@ EFIAPI InternalShellIsHexOrDecimalNumber ( IN CONST CHAR16 *String, IN CONST BOOLEAN ForceHex, - IN CONST BOOLEAN StopAtSpace + IN CONST BOOLEAN StopAtSpace, + IN CONST BOOLEAN TimeNumbers ) { BOOLEAN Hex; @@ -3480,6 +3631,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); @@ -3803,10 +3957,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 +3976,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); } @@ -4026,3 +4180,146 @@ 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 +EFIAPI +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; +} +