summaryrefslogtreecommitdiff
path: root/core/file.c
blob: 9ad1fd879bb8f87b26537712f5e3a558143377ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2026, Apollo Telephone Laboratories.
 * Provided under the BSD-3 clause.
 */

#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)
{
    EFI_LOADED_IMAGE *LoadedImage = NULL;
    EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *IoVolume;
    EFI_GUID LipGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
    EFI_GUID FsGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
    EFI_FILE_HANDLE Volume;
    EFI_STATUS Status;

    Status = FwiHandleProtocol(
        ImageHandle,
        &LipGuid,
        (VOID **)&LoadedImage
    );

    if (EFI_ERROR(Status)) {
        Print(L"fatal: could not get loaded image\n");
        return Status;
    }

    Status = FwiHandleProtocol(
        LoadedImage->DeviceHandle,
        &FsGuid,
        (VOID *)&IoVolume
    );

    if (EFI_ERROR(Status)) {
        Print(L"fatal: could not I/O volume\n");
        return Status;
    }

    Status = uefi_call_wrapper(
        IoVolume->OpenVolume,
        2,
        IoVolume,
        &Volume
    );

    if (EFI_ERROR(Status)) {
        Print(L"fatal: failed to get volume\n");
        return Status;
    }

    *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;
}