X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FBaseFileHandleLib%2FBaseFileHandleLib.c;h=8c4cc03fc1845c7091607d10986f51a304a28d6b;hp=023210e8e0f0dc484a24b830d9f35e2d130d7a72;hb=d54744cd6c544e544c02fd75b20cff73fd865258;hpb=b1f95a06ca1a66ad15b3d8843fb4c7563c5350d2 diff --git a/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c b/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c index 023210e8e0..8c4cc03fc1 100644 --- a/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c +++ b/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c @@ -1,7 +1,7 @@ /** @file Provides interface to EFI_FILE_HANDLE functionality. -Copyright (c) 2006 - 2009, Intel Corporation +Copyright (c) 2006 - 2009, 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 @@ -22,6 +22,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include +#include +#include #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes) #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN) @@ -46,7 +49,6 @@ FileHandleGetInfo ( IN EFI_FILE_HANDLE FileHandle ) { - EFI_GUID FileInfoGuid; EFI_FILE_INFO *pFileInfo; UINTN FileInfoSize; EFI_STATUS Status; @@ -59,11 +61,10 @@ FileHandleGetInfo ( // // Get the required size to allocate // - FileInfoGuid = gEfiFileInfoGuid; FileInfoSize = 0; pFileInfo = NULL; Status = FileHandle->GetInfo(FileHandle, - &FileInfoGuid, + &gEfiFileInfoGuid, &FileInfoSize, pFileInfo); // @@ -76,7 +77,7 @@ FileHandleGetInfo ( // now get the information // Status = FileHandle->GetInfo(FileHandle, - &FileInfoGuid, + &gEfiFileInfoGuid, &FileInfoSize, pFileInfo); // @@ -114,7 +115,6 @@ FileHandleSetInfo ( IN CONST EFI_FILE_INFO *FileInfo ) { - EFI_GUID FileInfoGuid; // // ASSERT if the FileHandle or FileInfo is NULL @@ -122,12 +122,11 @@ FileHandleSetInfo ( ASSERT (FileHandle != NULL); ASSERT (FileInfo != NULL); - FileInfoGuid = gEfiFileInfoGuid; // // Set the info // return (FileHandle->SetInfo(FileHandle, - &FileInfoGuid, + &gEfiFileInfoGuid, (UINTN)FileInfo->Size, (EFI_FILE_INFO*)FileInfo)); } @@ -541,14 +540,6 @@ FileHandleFindNextFile( ASSERT (DirHandle != NULL); ASSERT (Buffer != NULL); ASSERT (NoFile != NULL); - - // - // verify that DirHandle is a directory - // - Status = FileHandleIsDirectory(DirHandle); - if (EFI_ERROR(Status)) { - return (Status); - } // // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile @@ -648,8 +639,8 @@ FileHandleGetSize ( if Destination's current length (including NULL terminator) is already more then CurrentSize, then ASSERT() - @param[in][out] Destination The String to append onto - @param[in][out] CurrentSize on call the number of bytes in Destination. On + @param[in,out] Destination The String to append onto + @param[in,out] CurrentSize on call the number of bytes in Destination. On return possibly the new size (still in bytes). if NULL then allocate whatever is needed. @param[in] Source The String to append from @@ -668,6 +659,7 @@ StrnCatGrowLeft ( ){ UINTN DestinationStartSize; UINTN NewSize; + UINTN CopySize; // // ASSERTs @@ -697,7 +689,7 @@ StrnCatGrowLeft ( // Append all of Source? // if (Count == 0) { - Count = StrLen(Source); + Count = StrSize(Source); } // @@ -705,17 +697,18 @@ StrnCatGrowLeft ( // if (CurrentSize != NULL) { NewSize = *CurrentSize; - while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) { - NewSize += 2 * Count * sizeof(CHAR16); + while (NewSize < (DestinationStartSize + Count)) { + NewSize += 2 * Count; } *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination); *CurrentSize = NewSize; } else { - *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16)); + *Destination = AllocateZeroPool(Count+sizeof(CHAR16)); } - *Destination = CopyMem(*Destination+StrLen(Source), *Destination, StrSize(*Destination)); - *Destination = CopyMem(*Destination, Source, StrLen(Source)); + CopySize = StrSize(*Destination); + CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize); + CopyMem(*Destination, Source, Count-2); return (*Destination); } @@ -747,7 +740,6 @@ FileHandleGetFileName ( EFI_FILE_INFO *FileInfo; Size = 0; - *FullFileName = NULL; // // Check our parameters @@ -756,6 +748,8 @@ FileHandleGetFileName ( return (EFI_INVALID_PARAMETER); } + *FullFileName = NULL; + Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0); if (!EFI_ERROR(Status)) { // @@ -771,10 +765,15 @@ FileHandleGetFileName ( // We got info... do we have a name? if yes preceed the current path with it... // if (StrLen (FileInfo->FileName) == 0) { - *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0); + if (*FullFileName == NULL) { + *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0); + } FreePool(FileInfo); break; } else { + if (*FullFileName == NULL) { + *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0); + } *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0); *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0); FreePool(FileInfo); @@ -798,26 +797,70 @@ FileHandleGetFileName ( } if (EFI_ERROR(Status) && *FullFileName != NULL) { - FreePool(FullFileName); + FreePool(*FullFileName); } return (Status); } +/** + Function to read a single line from a file. The \n is not included in the returned + buffer. The returned buffer must be callee freed. + + If the position upon start is 0, then the Ascii Boolean will be set. This should be + maintained and not changed for all operations with the same file. + + @param[in] Handle FileHandle to read from. + @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE); + + @return The line of text from the file. + + @sa FileHandleReadLine +**/ +CHAR16* +EFIAPI +FileHandleReturnLine( + IN EFI_FILE_HANDLE Handle, + IN OUT BOOLEAN *Ascii + ) +{ + CHAR16 *RetVal; + UINTN Size; + EFI_STATUS Status; + + Size = 0; + RetVal = NULL; + + Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); + if (Status == EFI_BUFFER_TOO_SMALL) { + RetVal = AllocatePool(Size); + Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); + } + ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status) && (RetVal != NULL)) { + FreePool(RetVal); + RetVal = NULL; + } + return (RetVal); +} + /** Function to read a single line (up to but not including the \n) from a file. + If the position upon start is 0, then the Ascii Boolean will be set. This should be + maintained and not changed for all operations with the same file. + @param[in] Handle FileHandle to read from - @param[in][out] Buffer pointer to buffer to read into - @param[in][out] Size pointer to number of bytes in buffer - @param[in[ Truncate if TRUE then allows for truncation of the line to fit. + @param[in,out] Buffer pointer to buffer to read into + @param[in,out] Size pointer to number of bytes in buffer + @param[in] Truncate if TRUE then allows for truncation of the line to fit. if FALSE will reset the position to the begining of the line if the buffer is not large enough. + @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE); @retval EFI_SUCCESS the operation was sucessful. the line is stored in Buffer. @retval EFI_INVALID_PARAMETER Handle was NULL. - @retval EFI_INVALID_PARAMETER Buffer was NULL. @retval EFI_INVALID_PARAMETER Size was NULL. @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line. Size was updated to minimum space required. @@ -827,31 +870,54 @@ EFI_STATUS EFIAPI FileHandleReadLine( IN EFI_FILE_HANDLE Handle, - IN OUT VOID *Buffer, + IN OUT CHAR16 *Buffer, IN OUT UINTN *Size, - IN BOOLEAN Truncate + IN BOOLEAN Truncate, + IN OUT BOOLEAN *Ascii ){ EFI_STATUS Status; CHAR16 CharBuffer; UINTN CharSize; UINTN CountSoFar; - UINT64 Position; + UINT64 OriginalFilePosition; if (Handle == NULL - ||Buffer == NULL ||Size == NULL ){ return (EFI_INVALID_PARAMETER); } - FileHandleGetPosition(Handle, &Position); + FileHandleGetPosition(Handle, &OriginalFilePosition); + if (OriginalFilePosition == 0) { + CharSize = sizeof(CHAR16); + Status = FileHandleRead(Handle, &CharSize, &CharBuffer); + ASSERT_EFI_ERROR(Status); + if (CharBuffer == UnicodeFileTag) { + *Ascii = FALSE; + } else { + *Ascii = TRUE; + FileHandleSetPosition(Handle, OriginalFilePosition); + } + } for (CountSoFar = 0;;CountSoFar++){ - CharSize = sizeof(CharBuffer); + CharBuffer = 0; + if (*Ascii) { + CharSize = sizeof(CHAR8); + } else { + CharSize = sizeof(CHAR16); + } Status = FileHandleRead(Handle, &CharSize, &CharBuffer); + if (OriginalFilePosition == 0 && *Ascii == FALSE && CountSoFar == 0) { + // + // we need to skip the unicode tag + // + continue; + } if ( EFI_ERROR(Status) || CharSize == 0 - || CharBuffer == '\n' + || (CharBuffer == L'\n' && *Ascii == FALSE) + || (CharBuffer == '\n' && *Ascii != FALSE ) ){ break; } @@ -859,8 +925,9 @@ FileHandleReadLine( // if we have space save it... // if ((CountSoFar+1)*sizeof(CHAR16) < *Size){ + ASSERT(Buffer != NULL); ((CHAR16*)Buffer)[CountSoFar] = CharBuffer; - ((CHAR16*)Buffer)[CountSoFar+1] = '\0'; + ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL; } } @@ -870,13 +937,16 @@ FileHandleReadLine( if ((CountSoFar+1)*sizeof(CHAR16) > *Size){ *Size = (CountSoFar+1)*sizeof(CHAR16); if (Truncate == FALSE) { - FileHandleSetPosition(Handle, Position); + FileHandleSetPosition(Handle, OriginalFilePosition); } else { - DEBUG((DEBUG_WARN, "The line was truncated in ReadLine")); + DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine")); } return (EFI_BUFFER_TOO_SMALL); } - *Size = (CountSoFar+1)*sizeof(CHAR16); + while(Buffer[StrLen(Buffer)-1] == L'\r') { + Buffer[StrLen(Buffer)-1] = CHAR_NULL; + } + return (Status); } @@ -917,3 +987,99 @@ FileHandleWriteLine( Size = StrLen(L"\r\n"); return FileHandleWrite(Handle, &Size, L"\r\n"); } + +/** + function to take a formatted argument and print it to a file. + + @param[in] Handle the file handle for the file to write to + @param[in] Format the format argument (see printlib for format specifier) + @param[in] ... the variable arguments for the format + + @retval EFI_SUCCESS the operation was sucessful + @return other a return value from FileHandleWriteLine + + @sa FileHandleWriteLine +**/ +EFI_STATUS +EFIAPI +FileHandlePrintLine( + IN EFI_FILE_HANDLE Handle, + IN CONST CHAR16 *Format, + ... + ) +{ + VA_LIST Marker; + CHAR16 *Buffer; + EFI_STATUS Status; + + VA_START (Marker, Format); + + // + // Get a buffer to print into + // + Buffer = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize)); + ASSERT (Buffer != NULL); + + // + // Print into our buffer + // + UnicodeVSPrint (Buffer, PcdGet16 (PcdShellPrintBufferSize), Format, Marker); + + // + // Print buffer into file + // + Status = FileHandleWriteLine(Handle, Buffer); + + // + // Cleanup and return + // + FreePool(Buffer); + return (Status); +} + +/** + Function to determine if a FILE_HANDLE is at the end of the file. + + This will NOT work on directories. + + If Handle is NULL, then ASSERT. + + @param[in] Handle the file handle + + @retval TRUE the position is at the end of the file + @retval FALSE the position is not at the end of the file +**/ +BOOLEAN +EFIAPI +FileHandleEof( + IN EFI_FILE_HANDLE Handle + ) +{ + EFI_FILE_INFO *Info; + UINT64 Pos; + BOOLEAN RetVal; + + // + // ASSERT if Handle is NULL + // + ASSERT(Handle != NULL); + + FileHandleGetPosition(Handle, &Pos); + Info = FileHandleGetInfo (Handle); + ASSERT(Info != NULL); + FileHandleSetPosition(Handle, Pos); + + if (Info == NULL) { + return (FALSE); + } + + if (Pos == Info->FileSize) { + RetVal = TRUE; + } else { + RetVal = FALSE; + } + + FreePool (Info); + + return (RetVal); +} \ No newline at end of file