X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShellParametersProtocol.c;h=ab435a126313da06da9eaecfe3e8c478e3617430;hb=7798fb83de4b2505a8e17457256b113a05d8475c;hp=b594d3434f7ad098a5616e17640cb56831247af5;hpb=8be0ba36fc347bac7199e7dbb8d373ea5e4bf0af;p=mirror_edk2.git diff --git a/ShellPkg/Application/Shell/ShellParametersProtocol.c b/ShellPkg/Application/Shell/ShellParametersProtocol.c index b594d3434f..ab435a1263 100644 --- a/ShellPkg/Application/Shell/ShellParametersProtocol.c +++ b/ShellPkg/Application/Shell/ShellParametersProtocol.c @@ -2,7 +2,8 @@ Member functions of EFI_SHELL_PARAMETERS_PROTOCOL and functions for creation, manipulation, and initialization of EFI_SHELL_PARAMETERS_PROTOCOL. - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2013 Hewlett-Packard Development Company, L.P. + Copyright (c) 2009 - 2014, 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,8 +14,7 @@ **/ -#include "ShellParametersProtocol.h" -#include "ConsoleWrappers.h" +#include "Shell.h" /** return the next parameter from a command line string; @@ -26,9 +26,9 @@ Temp Parameter must be large enough to hold the parameter before calling this function. - @param[in,out] Walker pointer to string of command line. Adjusted to + @param[in, out] Walker pointer to string of command line. Adjusted to reminaing command line on return - @param[in,out] TempParameter pointer to string of command line item extracted. + @param[in, out] TempParameter pointer to string of command line item extracted. **/ VOID @@ -69,14 +69,8 @@ GetNextParameter( if ((*Walker)[0] == L'\"') { NextDelim = NULL; for (TempLoc = *Walker + 1 ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) { - if (*TempLoc == L'^' && *(TempLoc+1) == L'^') { + if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') { TempLoc++; - } else if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') { - TempLoc++; - } else if (*TempLoc == L'^' && *(TempLoc+1) == L'|') { - TempLoc++; - } else if (*TempLoc == L'^') { - *TempLoc = L' '; } else if (*TempLoc == L'\"') { NextDelim = TempLoc; break; @@ -100,10 +94,7 @@ GetNextParameter( *Walker = NULL; } for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) { - if ((*TempLoc == L'^' && *(TempLoc+1) == L'^') - || (*TempLoc == L'^' && *(TempLoc+1) == L'|') - || (*TempLoc == L'^' && *(TempLoc+1) == L'\"') - ){ + if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') { CopyMem(TempLoc, TempLoc+1, StrSize(TempLoc) - sizeof(TempLoc[0])); } } @@ -127,8 +118,6 @@ GetNextParameter( for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) { if (*NextDelim == L'^' && *(NextDelim+1) == L'^') { CopyMem(NextDelim, NextDelim+1, StrSize(NextDelim) - sizeof(NextDelim[0])); - } else if (*NextDelim == L'^') { - *NextDelim = L' '; } } while ((*TempParameter)[StrLen(*TempParameter)-1] == L' ') { @@ -142,15 +131,15 @@ GetNextParameter( } /** - function to populate Argc and Argv. + Function to populate Argc and Argv. This function parses the CommandLine and divides it into standard C style Argc/Argv parameters for inclusion in EFI_SHELL_PARAMETERS_PROTOCOL. this supports space delimited and quote surrounded parameter definition. - @param[in] CommandLine String of command line to parse - @param[in,out] Argv pointer to array of strings; one for each parameter - @param[in,out] Argc pointer to number of strings in Argv array + @param[in] CommandLine String of command line to parse + @param[in, out] Argv pointer to array of strings; one for each parameter + @param[in, out] Argc pointer to number of strings in Argv array @return EFI_SUCCESS the operation was sucessful @return EFI_OUT_OF_RESOURCES a memory allocation failed. @@ -203,6 +192,7 @@ ParseCommandLineToArgs( // (*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*)); if (*Argv == NULL) { + SHELL_FREE_NON_NULL(TempParameter); return (EFI_OUT_OF_RESOURCES); } @@ -218,6 +208,7 @@ ParseCommandLineToArgs( (*Argc)++; } ASSERT(Count >= (*Argc)); + SHELL_FREE_NON_NULL(TempParameter); return (EFI_SUCCESS); } @@ -226,9 +217,9 @@ ParseCommandLineToArgs( installs it on our handle and if there is an existing version of the protocol that one is cached for removal later. - @param[in,out] NewShellParameters on a successful return, a pointer to pointer + @param[in, out] NewShellParameters on a successful return, a pointer to pointer to the newly installed interface. - @param[in,out] RootShellInstance on a successful return, pointer to boolean. + @param[in, out] RootShellInstance on a successful return, pointer to boolean. TRUE if this is the root shell instance. @retval EFI_SUCCESS the operation completed successfully. @@ -300,10 +291,10 @@ CreatePopulateInstallShellParametersProtocol ( // // Build the full command line // - Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, &FullCommandLine); + Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine); if (Status == EFI_BUFFER_TOO_SMALL) { FullCommandLine = AllocateZeroPool(Size + LoadedImage->LoadOptionsSize); - Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, &FullCommandLine); + Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine); } if (Status == EFI_NOT_FOUND) { // @@ -323,9 +314,7 @@ CreatePopulateInstallShellParametersProtocol ( FullCommandLine = AllocateZeroPool(Size); } if (FullCommandLine != NULL) { - if (LoadedImage->LoadOptionsSize != 0){ - StrCpy(FullCommandLine, LoadedImage->LoadOptions); - } + CopyMem (FullCommandLine, LoadedImage->LoadOptions, LoadedImage->LoadOptionsSize); // // Populate Argc and Argv // @@ -419,6 +408,187 @@ CleanUpShellParametersProtocol ( return (Status); } +/** + Determin if a file name represents a unicode file. + + @param[in] FileName Pointer to the filename to open. + + @retval EFI_SUCCESS The file is a unicode file. + @return An error upon failure. +**/ +EFI_STATUS +EFIAPI +IsUnicodeFile( + IN CONST CHAR16 *FileName + ) +{ + SHELL_FILE_HANDLE Handle; + EFI_STATUS Status; + UINT64 OriginalFilePosition; + UINTN CharSize; + CHAR16 CharBuffer; + + Status = gEfiShellProtocol->OpenFileByName(FileName, &Handle, EFI_FILE_MODE_READ); + if (EFI_ERROR(Status)) { + return (Status); + } + gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition); + gEfiShellProtocol->SetFilePosition(Handle, 0); + CharSize = sizeof(CHAR16); + Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); + if (EFI_ERROR(Status) || CharBuffer != gUnicodeFileTag) { + Status = EFI_BUFFER_TOO_SMALL; + } + gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); + gEfiShellProtocol->CloseFile(Handle); + return (Status); +} + +/** + Strips out quotes sections of a string. + + All of the characters between quotes is replaced with spaces. + + @param[in, out] TheString A pointer to the string to update. +**/ +VOID +EFIAPI +StripQuotes ( + IN OUT CHAR16 *TheString + ) +{ + BOOLEAN RemoveNow; + + for (RemoveNow = FALSE ; TheString != NULL && *TheString != CHAR_NULL ; TheString++) { + if (*TheString == L'^' && *(TheString + 1) == L'\"') { + TheString++; + } else if (*TheString == L'\"') { + RemoveNow = (BOOLEAN)!RemoveNow; + } else if (RemoveNow) { + *TheString = L' '; + } + } +} + +/** + Calcualte the 32-bit CRC in a EFI table using the service provided by the + gRuntime service. + + @param Hdr Pointer to an EFI standard header + +**/ +VOID +CalculateEfiHdrCrc ( + IN OUT EFI_TABLE_HEADER *Hdr + ) +{ + UINT32 Crc; + + Hdr->CRC32 = 0; + + // + // If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then + // Crc will come back as zero if we set it to zero here + // + Crc = 0; + gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc); + Hdr->CRC32 = Crc; +} + +/** + Fix a string to only have the file name, removing starting at the first space of whatever is quoted. + + @param[in] FileName The filename to start with. + + @retval NULL FileName was invalid. + @return The modified FileName. +**/ +CHAR16* +EFIAPI +FixFileName ( + IN CHAR16 *FileName + ) +{ + CHAR16 *Copy; + CHAR16 *TempLocation; + + if (FileName == NULL) { + return (NULL); + } + + if (FileName[0] == L'\"') { + Copy = FileName+1; + if ((TempLocation = StrStr(Copy , L"\"")) != NULL) { + TempLocation[0] = CHAR_NULL; + } + } else { + Copy = FileName; + while(Copy[0] == L' ') { + Copy++; + } + if ((TempLocation = StrStr(Copy , L" ")) != NULL) { + TempLocation[0] = CHAR_NULL; + } + } + + if (Copy[0] == CHAR_NULL) { + return (NULL); + } + + return (Copy); +} + +/** + Fix a string to only have the environment variable name, removing starting at the first space of whatever is quoted and removing the leading and trailing %. + + @param[in] FileName The filename to start with. + + @retval NULL FileName was invalid. + @return The modified FileName. +**/ +CHAR16* +EFIAPI +FixVarName ( + IN CHAR16 *FileName + ) +{ + CHAR16 *Copy; + CHAR16 *TempLocation; + + Copy = FileName; + + if (FileName[0] == L'%') { + Copy = FileName+1; + if ((TempLocation = StrStr(Copy , L"%")) != NULL) { + TempLocation[0] = CHAR_NULL; + } + } + + return (FixFileName(Copy)); +} + +/** + Remove the unicode file tag from the begining of the file buffer since that will not be + used by StdIn. +**/ +EFI_STATUS +EFIAPI +RemoveFileTag( + IN SHELL_FILE_HANDLE *Handle + ) +{ + UINTN CharSize; + CHAR16 CharBuffer; + + CharSize = sizeof(CHAR16); + CharBuffer = 0; + gEfiShellProtocol->ReadFile(*Handle, &CharSize, &CharBuffer); + if (CharBuffer != gUnicodeFileTag) { + gEfiShellProtocol->SetFilePosition(*Handle, 0); + } + return (EFI_SUCCESS); +} + /** Funcion will replace the current StdIn and StdOut in the ShellParameters protocol structure by parsing NewCommandLine. The current values are returned to the @@ -426,12 +596,12 @@ CleanUpShellParametersProtocol ( This will also update the system table. - @param[in,out] ShellParameters Pointer to parameter structure to modify. - @param[in] NewCommandLine The new command line to parse and use. - @param[out] OldStdIn Pointer to old StdIn. - @param[out] OldStdOut Pointer to old StdOut. - @param[out] OldStdErr Pointer to old StdErr. - @param[out] SystemTableInfo Pointer to old system table information. + @param[in, out] ShellParameters Pointer to parameter structure to modify. + @param[in] NewCommandLine The new command line to parse and use. + @param[out] OldStdIn Pointer to old StdIn. + @param[out] OldStdOut Pointer to old StdOut. + @param[out] OldStdErr Pointer to old StdErr. + @param[out] SystemTableInfo Pointer to old system table information. @retval EFI_SUCCESS Operation was sucessful, Argv and Argc are valid. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. @@ -440,7 +610,7 @@ EFI_STATUS EFIAPI UpdateStdInStdOutStdErr( IN OUT EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters, - IN CONST CHAR16 *NewCommandLine, + IN CHAR16 *NewCommandLine, OUT SHELL_FILE_HANDLE *OldStdIn, OUT SHELL_FILE_HANDLE *OldStdOut, OUT SHELL_FILE_HANDLE *OldStdErr, @@ -466,6 +636,7 @@ UpdateStdInStdOutStdErr( UINTN Size; CHAR16 TagBuffer[2]; SPLIT_LIST *Split; + CHAR16 *FirstLocation; OutUnicode = TRUE; InUnicode = TRUE; @@ -479,6 +650,7 @@ UpdateStdInStdOutStdErr( ErrAppend = FALSE; OutAppend = FALSE; CommandLineCopy = NULL; + FirstLocation = NULL; if (ShellParameters == NULL || SystemTableInfo == NULL || OldStdIn == NULL || OldStdOut == NULL || OldStdErr == NULL) { return (EFI_INVALID_PARAMETER); @@ -488,8 +660,8 @@ UpdateStdInStdOutStdErr( SystemTableInfo->ConInHandle = gST->ConsoleInHandle; SystemTableInfo->ConOut = gST->ConOut; SystemTableInfo->ConOutHandle = gST->ConsoleOutHandle; - SystemTableInfo->ConErr = gST->StdErr; - SystemTableInfo->ConErrHandle = gST->StandardErrorHandle; + SystemTableInfo->ErrOut = gST->StdErr; + SystemTableInfo->ErrOutHandle = gST->StandardErrorHandle; *OldStdIn = ShellParameters->StdIn; *OldStdOut = ShellParameters->StdOut; *OldStdErr = ShellParameters->StdErr; @@ -499,8 +671,14 @@ UpdateStdInStdOutStdErr( } CommandLineCopy = StrnCatGrow(&CommandLineCopy, NULL, NewCommandLine, 0); + if (CommandLineCopy == NULL) { + return (EFI_OUT_OF_RESOURCES); + } Status = EFI_SUCCESS; Split = NULL; + FirstLocation = CommandLineCopy + StrLen(CommandLineCopy); + + StripQuotes(CommandLineCopy); if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) { Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link); @@ -513,41 +691,78 @@ UpdateStdInStdOutStdErr( } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>>v ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 12, L' '); StdErrVarName = CommandLineWalker += 6; ErrAppend = TRUE; + if (StrStr(CommandLineWalker, L" 2>>v ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>v ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 12, L' '); StdOutVarName = CommandLineWalker += 6; OutAppend = TRUE; + if (StrStr(CommandLineWalker, L" 1>>v ") != NULL) { + Status = EFI_NOT_FOUND; + } } else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>v ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); StdOutVarName = CommandLineWalker += 5; OutAppend = TRUE; + if (StrStr(CommandLineWalker, L" >>v ") != NULL) { + Status = EFI_NOT_FOUND; + } } else if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >v ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 8, L' '); StdOutVarName = CommandLineWalker += 4; OutAppend = FALSE; + if (StrStr(CommandLineWalker, L" >v ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>>a ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 12, L' '); StdOutFileName = CommandLineWalker += 6; OutAppend = TRUE; OutUnicode = FALSE; + if (StrStr(CommandLineWalker, L" 1>>a ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>> ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdOutFileName = CommandLineWalker += 5; OutAppend = TRUE; } + if (StrStr(CommandLineWalker, L" 1>> ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >> ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 8, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdOutFileName = CommandLineWalker += 4; OutAppend = TRUE; } + if (StrStr(CommandLineWalker, L" >> ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >>a ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { @@ -555,8 +770,13 @@ UpdateStdInStdOutStdErr( OutAppend = TRUE; OutUnicode = FALSE; } + if (StrStr(CommandLineWalker, L" >>a ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>a ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { @@ -564,8 +784,13 @@ UpdateStdInStdOutStdErr( OutAppend = FALSE; OutUnicode = FALSE; } + if (StrStr(CommandLineWalker, L" 1>a ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" >a ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 8, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { @@ -573,33 +798,53 @@ UpdateStdInStdOutStdErr( OutAppend = FALSE; OutUnicode = FALSE; } + if (StrStr(CommandLineWalker, L" >a ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>> ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdErrFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdErrFileName = CommandLineWalker += 5; ErrAppend = TRUE; } + if (StrStr(CommandLineWalker, L" 2>> ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>v ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdErrVarName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdErrVarName = CommandLineWalker += 5; ErrAppend = FALSE; } + if (StrStr(CommandLineWalker, L" 2>v ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1>v ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdOutVarName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdOutVarName = CommandLineWalker += 5; OutAppend = FALSE; } + if (StrStr(CommandLineWalker, L" 1>v ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2>a ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 10, L' '); if (StdErrFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { @@ -607,100 +852,184 @@ UpdateStdInStdOutStdErr( ErrAppend = FALSE; ErrUnicode = FALSE; } + if (StrStr(CommandLineWalker, L" 2>a ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 2> ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 8, L' '); if (StdErrFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdErrFileName = CommandLineWalker += 4; ErrAppend = FALSE; } + if (StrStr(CommandLineWalker, L" 2> ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" 1> ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 8, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdOutFileName = CommandLineWalker += 4; OutAppend = FALSE; } + if (StrStr(CommandLineWalker, L" 1> ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" > ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 6, L' '); if (StdOutFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdOutFileName = CommandLineWalker += 3; OutAppend = FALSE; } + if (StrStr(CommandLineWalker, L" > ") != NULL) { + Status = EFI_NOT_FOUND; + } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" < ")) != NULL) { + FirstLocation = MIN(CommandLineWalker, FirstLocation); + SetMem16(CommandLineWalker, 6, L' '); if (StdInFileName != NULL) { Status = EFI_INVALID_PARAMETER; } else { StdInFileName = CommandLineWalker += 3; - OutAppend = FALSE; + } + if (StrStr(CommandLineWalker, L" < ") != NULL) { + Status = EFI_NOT_FOUND; } } if (!EFI_ERROR(Status) && (CommandLineWalker = StrStr(CommandLineCopy, L" SplitStdIn != NULL && (StdInVarName != NULL || StdInFileName != NULL)) ||(Split != NULL && Split->SplitStdOut != NULL && (StdOutVarName != NULL || StdOutFileName != NULL)) + // + // Check that nothing is trying to be output to 2 locations. + // ||(StdErrFileName != NULL && StdErrVarName != NULL) ||(StdOutFileName != NULL && StdOutVarName != NULL) ||(StdInFileName != NULL && StdInVarName != NULL) + // + // Check for no volatile environment variables + // ||(StdErrVarName != NULL && !IsVolatileEnv(StdErrVarName)) ||(StdOutVarName != NULL && !IsVolatileEnv(StdOutVarName)) + // + // Cant redirect during a reconnect operation. + // ||(StrStr(NewCommandLine, L"connect -r") != NULL && (StdOutVarName != NULL || StdOutFileName != NULL || StdErrFileName != NULL || StdErrVarName != NULL)) + // + // Check that filetypes (Unicode/Ascii) do not change during an append + // + ||(StdOutFileName != NULL && OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && EFI_ERROR(IsUnicodeFile(StdOutFileName)))) + ||(StdErrFileName != NULL && ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && EFI_ERROR(IsUnicodeFile(StdErrFileName)))) + ||(StdOutFileName != NULL && !OutUnicode && OutAppend && (!EFI_ERROR(ShellFileExists(StdOutFileName)) && !EFI_ERROR(IsUnicodeFile(StdOutFileName)))) + ||(StdErrFileName != NULL && !ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && !EFI_ERROR(IsUnicodeFile(StdErrFileName)))) ){ Status = EFI_INVALID_PARAMETER; - } else { + ShellParameters->StdIn = *OldStdIn; + ShellParameters->StdOut = *OldStdOut; + ShellParameters->StdErr = *OldStdErr; + } else if (!EFI_ERROR(Status)){ // // Open the Std and we should not have conflicts here... // @@ -716,13 +1045,12 @@ UpdateStdInStdOutStdErr( ShellInfoObject.NewEfiShellProtocol->DeleteFileByName(StdErrFileName); } Status = ShellOpenFileByName(StdErrFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0); - ASSERT(TempHandle != NULL); if (!ErrAppend && ErrUnicode && !EFI_ERROR(Status)) { // - // Write out the UnicodeFileTag + // Write out the gUnicodeFileTag // Size = sizeof(CHAR16); - TagBuffer[0] = UnicodeFileTag; + TagBuffer[0] = gUnicodeFileTag; TagBuffer[1] = CHAR_NULL; ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer); } @@ -732,7 +1060,7 @@ UpdateStdInStdOutStdErr( } if (!EFI_ERROR(Status)) { ShellParameters->StdErr = TempHandle; - gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle); + gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr); } } @@ -750,12 +1078,14 @@ UpdateStdInStdOutStdErr( if (TempHandle == NULL) { Status = EFI_INVALID_PARAMETER; } else { - if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) { + if (StrStr(StdOutFileName, L"NUL")==StdOutFileName) { + //no-op + } else if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) { // - // Write out the UnicodeFileTag + // Write out the gUnicodeFileTag // Size = sizeof(CHAR16); - TagBuffer[0] = UnicodeFileTag; + TagBuffer[0] = gUnicodeFileTag; TagBuffer[1] = CHAR_NULL; ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer); } else if (OutAppend) { @@ -773,7 +1103,7 @@ UpdateStdInStdOutStdErr( } if (!EFI_ERROR(Status)) { ShellParameters->StdOut = TempHandle; - gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle); + gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut); } } } @@ -790,12 +1120,8 @@ UpdateStdInStdOutStdErr( } TempHandle = CreateFileInterfaceEnv(StdOutVarName); ASSERT(TempHandle != NULL); - if (!OutUnicode) { - TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); - ASSERT(TempHandle != NULL); - } ShellParameters->StdOut = TempHandle; - gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle); + gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut); } // @@ -810,12 +1136,8 @@ UpdateStdInStdOutStdErr( } TempHandle = CreateFileInterfaceEnv(StdErrVarName); ASSERT(TempHandle != NULL); - if (!ErrUnicode) { - TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); - ASSERT(TempHandle != NULL); - } ShellParameters->StdErr = TempHandle; - gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle); + gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr); } // @@ -823,16 +1145,19 @@ UpdateStdInStdOutStdErr( // if (!EFI_ERROR(Status) && StdInVarName != NULL) { TempHandle = CreateFileInterfaceEnv(StdInVarName); - if (!InUnicode) { - TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); - } - Size = 0; - ASSERT(TempHandle != NULL); - if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) { - Status = EFI_INVALID_PARAMETER; + if (TempHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; } else { - ShellParameters->StdIn = TempHandle; - gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle); + if (!InUnicode) { + TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); + } + Size = 0; + if (TempHandle == NULL || ((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) { + Status = EFI_INVALID_PARAMETER; + } else { + ShellParameters->StdIn = TempHandle; + gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle); + } } } @@ -845,7 +1170,15 @@ UpdateStdInStdOutStdErr( &TempHandle, EFI_FILE_MODE_READ, 0); - if (!InUnicode && !EFI_ERROR(Status)) { + if (InUnicode) { + // + // Chop off the 0xFEFF if it's there... + // + RemoveFileTag(&TempHandle); + } else if (!EFI_ERROR(Status)) { + // + // Create the ASCII->Unicode conversion layer + // TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); } if (!EFI_ERROR(Status)) { @@ -857,9 +1190,18 @@ UpdateStdInStdOutStdErr( } FreePool(CommandLineCopy); + CalculateEfiHdrCrc(&gST->Hdr); + if (gST->ConIn == NULL ||gST->ConOut == NULL) { - return (EFI_OUT_OF_RESOURCES); + Status = EFI_OUT_OF_RESOURCES; } + + if (Status == EFI_NOT_FOUND) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle); + } else if (EFI_ERROR(Status)) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle); + } + return (Status); } @@ -867,11 +1209,11 @@ UpdateStdInStdOutStdErr( Funcion will replace the current StdIn and StdOut in the ShellParameters protocol structure with StdIn and StdOut. The current values are de-allocated. - @param[in,out] ShellParameters Pointer to parameter structure to modify. - @param[in] OldStdIn Pointer to old StdIn. - @param[in] OldStdOut Pointer to old StdOut. - @param[in] OldStdErr Pointer to old StdErr. - @param[in] SystemTableInfo Pointer to old system table information. + @param[in, out] ShellParameters Pointer to parameter structure to modify. + @param[in] OldStdIn Pointer to old StdIn. + @param[in] OldStdOut Pointer to old StdOut. + @param[in] OldStdErr Pointer to old StdErr. + @param[in] SystemTableInfo Pointer to old system table information. **/ EFI_STATUS EFIAPI @@ -924,12 +1266,14 @@ RestoreStdInStdOutStdErr ( gST->ConOut = SystemTableInfo->ConOut; gST->ConsoleOutHandle = SystemTableInfo->ConOutHandle; } - if (gST->StdErr != SystemTableInfo->ConErr) { + if (gST->StdErr != SystemTableInfo->ErrOut) { CloseSimpleTextOutOnFile(gST->StdErr); - gST->StdErr = SystemTableInfo->ConErr; - gST->StandardErrorHandle = SystemTableInfo->ConErrHandle; + gST->StdErr = SystemTableInfo->ErrOut; + gST->StandardErrorHandle = SystemTableInfo->ErrOutHandle; } + CalculateEfiHdrCrc(&gST->Hdr); + return (EFI_SUCCESS); } /** @@ -939,10 +1283,10 @@ RestoreStdInStdOutStdErr ( If OldArgv or OldArgc is NULL then that value is not returned. - @param[in,out] ShellParameters Pointer to parameter structure to modify. - @param[in] NewCommandLine The new command line to parse and use. - @param[out] OldArgv Pointer to old list of parameters. - @param[out] OldArgc Pointer to old number of items in Argv list. + @param[in, out] ShellParameters Pointer to parameter structure to modify. + @param[in] NewCommandLine The new command line to parse and use. + @param[out] OldArgv Pointer to old list of parameters. + @param[out] OldArgc Pointer to old number of items in Argv list. @retval EFI_SUCCESS Operation was sucessful, Argv and Argc are valid. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. @@ -973,9 +1317,9 @@ UpdateArgcArgv( structure with Argv and Argc. The current values are de-allocated and the OldArgv must not be deallocated by the caller. - @param[in,out] ShellParameters pointer to parameter structure to modify - @param[in] OldArgv pointer to old list of parameters - @param[in] OldArgc pointer to old number of items in Argv list + @param[in, out] ShellParameters pointer to parameter structure to modify + @param[in] OldArgv pointer to old list of parameters + @param[in] OldArgc pointer to old number of items in Argv list **/ VOID EFIAPI