summaryrefslogtreecommitdiff
path: root/core/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/file.c')
-rw-r--r--core/file.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/core/file.c b/core/file.c
index 717e506..9ad1fd8 100644
--- a/core/file.c
+++ b/core/file.c
@@ -6,6 +6,18 @@
#include <sboot/file.h>
#include <sboot/fwi.h>
+static UINT64
+FileSize(EFI_FILE_HANDLE FileHandle)
+{
+ EFI_FILE_INFO *FileInfo;
+ UINT64 Size;
+
+ FileInfo = LibFileInfo(FileHandle);
+ Size = FileInfo->FileSize;
+ FreePool(FileInfo);
+ return Size;
+}
+
EFI_STATUS
FileGetVolume(IN EFI_HANDLE ImageHandle, OUT EFI_FILE_HANDLE *Handle)
{
@@ -53,3 +65,63 @@ FileGetVolume(IN EFI_HANDLE ImageHandle, OUT EFI_FILE_HANDLE *Handle)
*Handle = Volume;
return EFI_SUCCESS;
}
+
+EFI_STATUS
+FileReadToBuffer(IN CHAR16 *FileName, OUT VOID **Buffer)
+{
+ EFI_FILE_HANDLE Handle;
+ EFI_STATUS Status;
+ UINT64 ReadSize;
+ UINT8 *DataBuffer;
+
+ Status = uefi_call_wrapper(
+ gBootVolume->Open,
+ 5,
+ gBootVolume,
+ &Handle,
+ FileName,
+ EFI_FILE_MODE_READ,
+ EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM
+ );
+
+ if (EFI_ERROR(Status)) {
+ Print(L"fatal: could not open %s\n", FileName);
+ return Status;
+ }
+
+ ReadSize = FileSize(Handle);
+ Status = uefi_call_wrapper(
+ gBS->AllocatePool,
+ 3,
+ EfiLoaderData,
+ ReadSize,
+ &DataBuffer
+ );
+
+ if (EFI_ERROR(Status)) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ Status = uefi_call_wrapper(
+ Handle->Read,
+ 3,
+ Handle,
+ &ReadSize,
+ DataBuffer
+ );
+
+ if (EFI_ERROR(Status)) {
+ Print(L"fatal: could not read %s\n", FileName);
+ FreePool(DataBuffer);
+ return Status;
+ }
+
+ uefi_call_wrapper(
+ Handle->Close,
+ 1,
+ Handle
+ );
+
+ *Buffer = DataBuffer;
+ return EFI_SUCCESS;
+}