X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellLib%2FUefiShellLib.c;h=f04adbb63ffe650a9a0b77b06d5e324a57dbcc4b;hp=536db3c80599ff21e7798035e789b061c120c31a;hb=9becf2f0759eab29afcb088ee5c49f522d1d6619;hpb=ac55b925548f3b33f2bc93e603ecffe4a6cb191a diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Library/UefiShellLib/UefiShellLib.c index 536db3c805..f04adbb63f 100644 --- a/ShellPkg/Library/UefiShellLib/UefiShellLib.c +++ b/ShellPkg/Library/UefiShellLib/UefiShellLib.c @@ -3,7 +3,7 @@ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
Copyright 2016 Dell Inc. - Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+ 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 @@ -18,8 +18,6 @@ #include #include -#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN) - // // globals... // @@ -181,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 // @@ -240,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; @@ -322,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); } @@ -371,6 +388,7 @@ ShellLibDestructor ( EFI_STATUS EFIAPI ShellInitialize ( + VOID ) { EFI_STATUS Status; @@ -454,7 +472,6 @@ ShellSetFileInfo ( @param FilePath on input the device path to the file. On output the remaining device path. - @param DeviceHandle pointer to the system device handle. @param FileHandle pointer to the file handle. @param OpenMode the mode to open the file with. @param Attributes the file's file attributes. @@ -480,7 +497,6 @@ EFI_STATUS EFIAPI ShellOpenFileByDevicePath( IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath, - OUT EFI_HANDLE *DeviceHandle, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, IN UINT64 Attributes @@ -488,13 +504,9 @@ ShellOpenFileByDevicePath( { CHAR16 *FileName; EFI_STATUS Status; - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol; - EFI_FILE_PROTOCOL *Handle1; - EFI_FILE_PROTOCOL *Handle2; - CHAR16 *FnafPathName; - UINTN PathLen; + EFI_FILE_PROTOCOL *File; - if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) { + if (FilePath == NULL || FileHandle == NULL) { return (EFI_INVALID_PARAMETER); } @@ -518,117 +530,15 @@ ShellOpenFileByDevicePath( // // use old shell method. // - Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, - FilePath, - DeviceHandle); - if (EFI_ERROR (Status)) { - return Status; - } - Status = gBS->OpenProtocol(*DeviceHandle, - &gEfiSimpleFileSystemProtocolGuid, - (VOID**)&EfiSimpleFileSystemProtocol, - gImageHandle, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (EFI_ERROR (Status)) { - return Status; - } - Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1); + Status = EfiOpenFileByDevicePath (FilePath, &File, OpenMode, Attributes); if (EFI_ERROR (Status)) { - FileHandle = NULL; return Status; } - // - // go down directories one node at a time. - // - while (!IsDevicePathEnd (*FilePath)) { - // - // For file system access each node should be a file path component - // - if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH || - DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP - ) { - FileHandle = NULL; - return (EFI_INVALID_PARAMETER); - } - // - // Open this file path node - // - 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, - FnafPathName, - OpenMode &~EFI_FILE_MODE_CREATE, - 0 - ); - - // - // see if the error was that it needs to be created - // - if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) { - Status = Handle2->Open ( - Handle2, - &Handle1, - FnafPathName, - OpenMode, - Attributes - ); - } - - // - // Free the alignment buffer - // - FreePool(FnafPathName); - - // - // Close the last node - // - Handle2->Close (Handle2); - - if (EFI_ERROR(Status)) { - return (Status); - } - - // - // Get the next node - // - *FilePath = NextDevicePathNode (*FilePath); - } - // // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also! // - *FileHandle = (VOID*)Handle1; + *FileHandle = (VOID*)File; return (EFI_SUCCESS); } @@ -672,7 +582,6 @@ ShellOpenFileByName( IN UINT64 Attributes ) { - EFI_HANDLE DeviceHandle; EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_STATUS Status; EFI_FILE_INFO *FileInfo; @@ -702,7 +611,7 @@ ShellOpenFileByName( // Create the directory to create the file in // FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName); - if (FileName == NULL) { + if (FileNameCopy == NULL) { return (EFI_OUT_OF_RESOURCES); } PathCleanUpDirectories (FileNameCopy); @@ -756,7 +665,6 @@ ShellOpenFileByName( FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName); if (FilePath != NULL) { return (ShellOpenFileByDevicePath(&FilePath, - &DeviceHandle, FileHandle, OpenMode, Attributes)); @@ -2140,22 +2048,22 @@ InternalCommandLineParse ( } CurrentItemPackage->Value = NewValue; if (ValueSize == 0) { - StrCpyS( CurrentItemPackage->Value, - CurrentValueSize/sizeof(CHAR16), + StrCpyS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), Argv[LoopCounter] ); } else { - StrCatS( CurrentItemPackage->Value, - CurrentValueSize/sizeof(CHAR16), + StrCatS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), L" " ); - StrCatS( CurrentItemPackage->Value, - CurrentValueSize/sizeof(CHAR16), + StrCatS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), Argv[LoopCounter] ); } ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); - + GetItemValue--; if (GetItemValue == 0) { InsertHeadList(*CheckPackage, &CurrentItemPackage->Link); @@ -2714,10 +2622,10 @@ InternalPrintTo ( return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String)); } if (mEfiShellInterface != NULL) { - if (mEfiShellInterface->RedirArgc == 0) { + if (mEfiShellInterface->RedirArgc == 0) { // // Divide in half for old shell. Must be string length not size. - // + // Size /=2; // Divide in half only when no redirection. } return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut, &Size, (VOID*)String)); @@ -2828,7 +2736,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 ^ // @@ -2851,10 +2759,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: // @@ -3155,7 +3063,7 @@ ShellHexStrToUintn( if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) { return ((UINTN)RetVal); } - + return ((UINTN)(-1)); } @@ -3294,7 +3202,7 @@ StrnCatGrow ( if (*Destination == NULL) { return (NULL); } - + StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count); return *Destination; } @@ -3618,29 +3526,36 @@ InternalShellIsHexOrDecimalNumber ( ) { 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 // @@ -3657,7 +3572,7 @@ 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; } @@ -3755,7 +3670,7 @@ InternalShellHexCharToUintn ( return Char - L'0'; } - return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A'); + return (10 + InternalShellCharToUpper (Char) - L'A'); } /** @@ -3919,7 +3834,7 @@ InternalShellStrDecimalToUint64 ( Result = 0; // - // Stop upon space if requested + // Stop upon space if requested // (if the whole value was 0) // if (StopAtSpace && *String == L' ') { @@ -4230,7 +4145,7 @@ ShellFileHandleReadLine( @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 + @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. @@ -4244,33 +4159,33 @@ ShellPrintHelp ( IN BOOLEAN PrintCommandText ) { - EFI_STATUS Status; - CHAR16 *OutText; - - OutText = NULL; - + EFI_STATUS Status; + CHAR16 *OutText; + + OutText = NULL; + // // Get the string to print based // - Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText); - + 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; - } - + 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 // @@ -4279,17 +4194,17 @@ ShellPrintHelp ( } else { ShellPrintEx(-1, -1, L"%N%s\r\n", OutText); } - + SHELL_FREE_NON_NULL(OutText); - return EFI_SUCCESS; + 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 @@ -4316,26 +4231,26 @@ ShellDeleteFileByName( { 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 + @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. @@ -4347,7 +4262,7 @@ InternalShellStripQuotes ( ) { CHAR16 *Walker; - + if (OriginalString == NULL || CleanString == NULL) { return EFI_INVALID_PARAMETER; }