diff --git a/Include/Acidanthera/Library/OcFileLib.h b/Include/Acidanthera/Library/OcFileLib.h index e81a1a3b..5cdf5241 100755 --- a/Include/Acidanthera/Library/OcFileLib.h +++ b/Include/Acidanthera/Library/OcFileLib.h @@ -186,6 +186,22 @@ SetFileData ( IN UINT32 Size ); +/** + Read all bytes from EFI_FILE_PROTOCOL and return a buffer. + + @param[in] File A pointer to the file protocol. + @param[out] Buffer A pointer for the returned buffer. + @param[out] BufferSize A pointer for the size of the returned buffer. + + @retval EFI_SUCCESS on success. +**/ +EFI_STATUS +AllocateCopyFileData ( + IN EFI_FILE_PROTOCOL *File, + OUT UINT8 **Buffer, + OUT UINT32 *BufferSize + ); + /** Get file information of specified type. diff --git a/Library/OcFileLib/FileProtocol.c b/Library/OcFileLib/FileProtocol.c index 69be8594..eca90cf1 100644 --- a/Library/OcFileLib/FileProtocol.c +++ b/Library/OcFileLib/FileProtocol.c @@ -246,3 +246,37 @@ SetFileData ( return Status; } + +EFI_STATUS +AllocateCopyFileData ( + IN EFI_FILE_PROTOCOL *File, + OUT UINT8 **Buffer, + OUT UINT32 *BufferSize + ) +{ + EFI_STATUS Status; + UINT8 *FileBuffer; + UINT32 ReadSize; + + // + // Get full file data. + // + Status = GetFileSize (File, &ReadSize); + if (EFI_ERROR (Status)) { + return Status; + } + + FileBuffer = AllocatePool (ReadSize); + if (FileBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = GetFileData (File, 0, ReadSize, FileBuffer); + if (EFI_ERROR (Status)) { + FreePool (FileBuffer); + return Status; + } + + *Buffer = FileBuffer; + *BufferSize = ReadSize; + return EFI_SUCCESS; +}