summaryrefslogtreecommitdiff
path: root/loader/main_efi.c
blob: 37802cb1b4356cdb65d70aa1aa30ac0897b171a1 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <loader/efi/shim.h>

#include <kernel/entry.h>

#include <efi.h>
#include <efidebug.h>
#include <efilib.h>

#include "../lib/elf.h"

EFI_STATUS alloc_page(EFI_MEMORY_TYPE type,
        UINTN size,
        EFI_PHYSICAL_ADDRESS *base);
EFI_STATUS free_page(EFI_PHYSICAL_ADDRESS base, UINTN size);

EFI_STATUS open_image(struct efi_loader_image *image);
EFI_STATUS allocate_image(struct efi_loader_image *image);
EFI_STATUS load_image(struct efi_loader_image *image);

static EFI_STATUS enter_shim(void);

static struct efi_loader_image shim_image =
{   
    .path = L"\\adasoft\\sophia\\efi.os"
};

static struct efi_loader_interface loader_interface =
{
    NULL,
    NULL,
    &alloc_page,
    &free_page,
    &open_image,
    &allocate_image,
    &load_image
};

EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
{
    InitializeLib(image_handle, system_table);
    loader_interface.image_handle = image_handle;
    loader_interface.system_table = system_table;

    EFI_STATUS status;

    if (EFI_ERROR((status = open_image(&shim_image))))
    {
        Print(L"error opening shim image: %r\r\n");
    }

    if (EFI_ERROR((status = allocate_image(&shim_image))))
    {
        Print(L"error allocating shim image: %r\r\n");
    }

    if (EFI_ERROR((status = load_image(&shim_image))))
    {
        Print(L"error loading shim image: %r\r\n");
    }

    if (EFI_ERROR((status = enter_shim())))
    {
        Print(L"error running shim image: %r\r\n");
    }

    if (shim_image.root)
    {
        shim_image.root->Close(shim_image.root);
    }
    if (shim_image.file)
    {
        shim_image.file->Close(shim_image.file);
    }

    if (shim_image.buffer_base && shim_image.buffer_size)
    {
        free_page(shim_image.buffer_base,
                EFI_SIZE_TO_PAGES(shim_image.buffer_size));
    }

    return status;
}

EFI_STATUS alloc_page(EFI_MEMORY_TYPE type,
        UINTN size,
        EFI_PHYSICAL_ADDRESS *base)
{
    return gBS->AllocatePages(AllocateAnyPages,
            type,
            EFI_SIZE_TO_PAGES(size),
            base);
}

EFI_STATUS free_page(EFI_PHYSICAL_ADDRESS base, UINTN size)
{
    return gBS->FreePages(base, size);
}


EFI_STATUS open_image(struct efi_loader_image *image)
{
    EFI_FILE_PROTOCOL *root;

    EFI_LOADED_IMAGE_PROTOCOL *loader_image;

    EFI_STATUS status;

    status = gBS->OpenProtocol(loader_interface.image_handle,
            &LoadedImageProtocol,
            (void  *)&loader_image,
            loader_interface.image_handle,
            NULL,
            EFI_OPEN_PROTOCOL_GET_PROTOCOL);


    if (EFI_ERROR(status) ||
            !(root = LibOpenRoot(loader_image->DeviceHandle)))
    {
        Print(L"failed opening root device\r\n");
        return EFI_NOT_FOUND;
    }

    Print(L"opening image %s\r\n", image->path);

    status = root->Open(root,
            &image->file,
            image->path,
            EFI_FILE_MODE_READ,
            0);

    if (!EFI_ERROR(status))
    {
        image->root = root;
    }
    else
    {
        Print(L"failed opening image: %r\r\n", status);
        return status;
    }

    Elf64_Ehdr ehdr;
    UINTN ehdr_size = sizeof(ehdr);

    Elf64_Phdr *phdrs = NULL;
    UINTN phdrs_size = 0;

    if (!EFI_ERROR((status = image->file->Read(image->file,
                        &ehdr_size, 
                        &ehdr))))
    {
        if (elf_validate(&ehdr, ET_EXEC, EM_X86_64) ||
                elf_validate(&ehdr, ET_DYN, EM_X86_64))
        {
            phdrs_size = ehdr.e_phentsize * ehdr.e_phnum;
            phdrs = AllocatePool(phdrs_size);
            ASSERT(phdrs);
         }
         else
         {
            FreePool(phdrs);
            Print(L"invalid image format detected\r\n");
            return EFI_INVALID_PARAMETER;
         }
    }
    else
    {
        Print(L"failed reading shim image headers\r\n");
        return status;
    }

    if (!EFI_ERROR((status = image->file->Read(image->file,
                        &phdrs_size,
                        phdrs))))
    {
        image->buffer_size = elf_size(&ehdr,  phdrs);
        image->system_table = gST;
        Print(L"elf image %d bytes\r\n", image->buffer_size);
    }
    else
    {
        Print(L"failed reading shim image segments\r\n");
    }

    FreePool(phdrs);

    return status;
}

EFI_STATUS allocate_image(struct efi_loader_image *image)
{
    Print(L"allocating image %s\r\n", image->path);
    EFI_STATUS status;

    status = alloc_page(SystemMemoryType,
            image->buffer_size,
            &image->buffer_base);

    if (!EFI_ERROR(status))
    {
        gBS->SetMem((void *)image->buffer_base, image->buffer_size, 0);
    }

    return status;
}

static EFI_STATUS read_image(struct efi_loader_image *image, 
        VOID **buffer,
        UINTN offset,
        UINTN length)
{
    Print(L"reading %d bytes from image at 0x%8.0x %s\r\n", length, offset, image->path);
    EFI_STATUS status;

    ASSERT(buffer);

    if (!EFI_ERROR((status = image->file->SetPosition(image->file, offset))))
    {
        *buffer = (void *)(image->buffer_base + offset);
        status = image->file->Read(image->file, &length, *buffer);
    }
    else
    {
        Print(L"failed seeking shim image\r\n");
    } 

    return status;
}

EFI_STATUS load_image(struct efi_loader_image *image)
{
    EFI_STATUS status;
    Elf64_Ehdr *ehdr;
    Elf64_Phdr *phdrs;

    if (!EFI_ERROR((status = read_image(image,
                        (void **)&ehdr,
                        0,
                        sizeof(*ehdr)))) &&
            !EFI_ERROR((status = read_image(image,
                        (void **)&phdrs,
                        ehdr->e_phoff,
                        ehdr->e_phnum * ehdr->e_phentsize))))
    {
        for (UINTN i = 0; i < ehdr->e_phnum && !EFI_ERROR(status); i++)
        {
            if (PT_LOAD != phdrs[i].p_type)
            {
                continue;
            }

            void *placement;
            status = read_image(image,
                    &placement,
                    phdrs[i].p_offset,
                    phdrs[i].p_filesz);

            if (!EFI_ERROR(status))
            {
                Print(L"loaded segment 0x%16.0x of %d bytes "
                        L"(file size %d bytes)\r\n",
                        placement,
                        phdrs[i].p_memsz,
                        phdrs[i].p_filesz);
            }
        }
    }

    return status;
}

static EFI_STATUS enter_shim(void)
{
    EFI_STATUS status;
    Elf64_Ehdr *ehdr = (Elf64_Ehdr *)shim_image.buffer_base;
    kc_entry_func entry;

    entry = (kc_entry_func)(shim_image.buffer_base + ehdr->e_entry);
    status = entry(&loader_interface);
    return status;
}