-
Notifications
You must be signed in to change notification settings - Fork 6
/
loader.c
551 lines (481 loc) · 11.9 KB
/
loader.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include "loader.h"
#include "clib.h"
#include "compiler.h"
#include "io.h"
#include "log.h"
static efi_status_t reserve(
struct loader *loader,
const char *name,
uint64_t begin,
uint64_t end)
{
if (loader->reserves == loader->reserve_capacity) {
efi_status_t status = EFI_SUCCESS;
size_t new_size = 2 * loader->reserves;
struct reserve *new_reserve = NULL;
struct reserve *old_reserve = loader->reserve;
if (new_size == 0)
new_size = 16;
status = loader->system->boot->allocate_pool(
EFI_LOADER_DATA,
new_size * sizeof(struct reserve),
(void **)&new_reserve);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to allocate buffer for memory reserve\r\n");
return status;
}
memcpy(
new_reserve,
old_reserve,
loader->reserves * sizeof(struct reserve));
loader->reserve = new_reserve;
loader->reserve_capacity = new_size;
if (old_reserve != NULL) {
status = loader->system->boot->free_pool(
(void *)old_reserve);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to free buffer for memory reserve\r\n");
return status;
}
}
}
memset(&loader->reserve[loader->reserves], 0, sizeof(struct reserve));
loader->reserve[loader->reserves].name = name;
loader->reserve[loader->reserves].begin = begin;
loader->reserve[loader->reserves].end = end;
++loader->reserves;
return EFI_SUCCESS;
}
static efi_status_t get_loader_image(
efi_handle_t loader,
struct efi_system_table *system,
struct efi_loaded_image_protocol **image)
{
struct efi_guid guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
return system->boot->open_protocol(
loader,
&guid,
(void **)image,
loader,
NULL,
EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
}
static efi_status_t get_rootfs(
efi_handle_t loader,
struct efi_system_table *system,
efi_handle_t device,
struct efi_simple_file_system_protocol **rootfs)
{
struct efi_guid guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
return system->boot->open_protocol(
device,
&guid,
(void **)rootfs,
loader,
NULL,
EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
}
static efi_status_t get_rootdir(
struct efi_simple_file_system_protocol *rootfs,
struct efi_file_protocol **rootdir)
{
return rootfs->open_volume(rootfs, rootdir);
}
efi_status_t setup_loader(
efi_handle_t handle,
struct efi_system_table *system,
struct loader *loader)
{
efi_status_t status = EFI_SUCCESS;
memset(loader, 0, sizeof(*loader));
loader->system = system;
loader->handle = handle;
status = get_loader_image(handle, system, &loader->image);
if (status != EFI_SUCCESS) {
err(
system,
"failed to get loader image protocol\r\n");
return status;
}
loader->root_device = loader->image->device;
status = get_rootfs(
handle, system, loader->root_device, &loader->rootfs);
if (status != EFI_SUCCESS) {
err(
system,
"failed to get root volume\r\n");
return status;
}
status = get_rootdir(loader->rootfs, &loader->rootdir);
if (status != EFI_SUCCESS) {
err(
system,
"failed to get root filesystem directory\r\n");
return status;
}
return EFI_SUCCESS;
}
static efi_status_t read_elf64_header(
struct efi_system_table *system,
struct efi_file_protocol *file,
struct elf64_ehdr *hdr)
{
return efi_read_fixed(system, file, /*offset*/0, sizeof(*hdr), hdr);
}
static efi_status_t verify_elf64_header(
struct efi_system_table *system,
const struct elf64_ehdr *hdr)
{
if (hdr->e_ident[EI_MAG0] != 0x7f
|| hdr->e_ident[EI_MAG1] != 'E'
|| hdr->e_ident[EI_MAG2] != 'L'
|| hdr->e_ident[EI_MAG3] != 'F') {
err(system, "No ELF magic sequence in the ELF header\r\n");
return EFI_UNSUPPORTED;
}
if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
err(
system,
"Unsupported ELF file type 0x%x, the only type 0x%x is supported\r\n",
(unsigned)hdr->e_type,
(unsigned)ET_EXEC);
return EFI_UNSUPPORTED;
}
if (hdr->e_ident[EI_CLASS] != ELFCLASS64) {
err(
system,
"Unsupported ELF class 0x%x, the only class 0x%x is supported\r\n",
(unsigned)hdr->e_ident[EI_CLASS],
(unsigned)ELFCLASS64);
return EFI_UNSUPPORTED;
}
if (hdr->e_phnum == 0) {
err(
system,
"ELF file doesn't contain any program headers\r\n");
return EFI_UNSUPPORTED;
}
if (hdr->e_phentsize != sizeof(struct elf64_phdr)) {
err(
system,
"Unexpected ELF program header size %u, only size %u is supported\r\n",
(unsigned)hdr->e_phentsize,
(unsigned)sizeof(struct elf64_phdr));
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}
static efi_status_t read_elf64_program_headers(
struct efi_system_table *system,
struct efi_boot_table *boot,
struct efi_file_protocol *file,
const struct elf64_ehdr *hdr,
struct elf64_phdr **phdrs)
{
efi_status_t status;
status = boot->allocate_pool(
EFI_LOADER_DATA,
hdr->e_phnum * hdr->e_phentsize,
(void **)phdrs);
if (status != EFI_SUCCESS) {
err(
system,
"failed to allocate buffer for program headers\r\n");
return status;
}
status = efi_read_fixed(
system,
file,
hdr->e_phoff,
hdr->e_phentsize * hdr->e_phnum,
(void *)*phdrs);
if (status != EFI_SUCCESS) {
err(
system,
"failed to read program headers\r\n");
boot->free_pool((void *)*phdrs);
return status;
}
return EFI_SUCCESS;
}
static void elf64_image_size(
struct loader *loader,
uint64_t alignment,
uint64_t *begin,
uint64_t *end)
{
*begin = UINT64_MAX;
*end = 0;
for (size_t i = 0; i < loader->kernel_header.e_phnum; ++i) {
struct elf64_phdr *phdr = &loader->program_headers[i];
uint64_t phdr_begin, phdr_end;
uint64_t align = alignment;
if (phdr->p_type != PT_LOAD)
continue;
if (phdr->p_align > align)
align = phdr->p_align;
phdr_begin = phdr->p_vaddr;
phdr_begin &= ~(align - 1);
if (*begin > phdr_begin)
*begin = phdr_begin;
phdr_end = phdr->p_vaddr + phdr->p_memsz + align - 1;
phdr_end &= ~(align - 1);
if (*end < phdr_end)
*end = phdr_end;
}
}
efi_status_t load_kernel(struct loader *loader)
{
efi_status_t status = EFI_SUCCESS;
uint64_t page_size = 4096;
uint64_t image_begin;
uint64_t image_end;
uint64_t image_size;
uint64_t image_addr;
status = loader->rootdir->open(
loader->rootdir,
&loader->kernel_image,
(uint16_t *)loader->module[loader->kernel].path,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to open kernel file\r\n");
return status;
}
status = read_elf64_header(
loader->system, loader->kernel_image, &loader->kernel_header);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to read ELF header\r\n");
return status;
}
status = verify_elf64_header(
loader->system, &loader->kernel_header);
if (status != EFI_SUCCESS) {
err(
loader->system,
"ELF header didn't pass verifications\r\n");
return status;
}
status = read_elf64_program_headers(
loader->system,
loader->system->boot,
loader->kernel_image,
&loader->kernel_header,
&loader->program_headers);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to read ELF program headers\r\n");
return status;
}
elf64_image_size(
loader,
page_size,
&image_begin,
&image_end);
image_size = image_end - image_begin;
status = loader->system->boot->allocate_pages(
EFI_ALLOCATE_ANY_PAGES,
EFI_LOADER_DATA,
image_size / page_size,
&image_addr);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to allocate buffer for the kernel\r\n");
return status;
}
memset((void *)image_addr, 0, image_size);
for (size_t i = 0; i < loader->kernel_header.e_phnum; ++i) {
struct elf64_phdr *phdr = &loader->program_headers[i];
uint64_t phdr_addr;
if (phdr->p_type != PT_LOAD)
continue;
phdr_addr = image_addr + phdr->p_vaddr - image_begin;
status = efi_read_fixed(
loader->system,
loader->kernel_image,
phdr->p_offset,
phdr->p_filesz,
(void *)phdr_addr);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to read the kernel segment in memory\r\n");
return status;
}
status = reserve(
loader,
"kernel",
phdr_addr,
phdr_addr + phdr->p_memsz);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to mark kernel segment as reserved\r\n");
return status;
}
}
loader->kernel_image_entry =
image_addr + loader->kernel_header.e_entry - image_begin;
return EFI_SUCCESS;
}
static efi_status_t load_module(
struct loader *loader,
struct efi_file_protocol *file,
const char *name)
{
efi_status_t status = EFI_SUCCESS;
struct efi_guid guid = EFI_FILE_INFO_GUID;
void *addr = NULL;
struct efi_file_info file_info;
efi_uint_t size;
size = sizeof(file_info);
status = file->get_info(
file,
&guid,
&size,
(void *)&file_info);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to find module file size\r\n");
return status;
}
status = loader->system->boot->allocate_pool(
EFI_LOADER_DATA,
file_info.file_size,
&addr);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to allocate memory for module\r\n");
return status;
}
status = efi_read_fixed(
loader->system,
file,
/* offset */0,
/* size */file_info.file_size,
addr);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to read module in memory\r\n");
return status;
}
status = reserve(
loader,
name,
(uint64_t)addr,
(uint64_t)addr + file_info.file_size);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to mark module memory as reserved\r\n");
return status;
}
return EFI_SUCCESS;
}
efi_status_t load_modules(struct loader *loader)
{
for (size_t i = 0; i < loader->modules; ++i) {
efi_status_t status = EFI_SUCCESS;
struct efi_file_protocol *file = NULL;
if (i == loader->kernel)
continue;
status = loader->rootdir->open(
loader->rootdir,
&file,
(uint16_t *)loader->module[i].path,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to open module file\r\n");
return status;
}
status = load_module(loader, file, loader->module[i].name);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to load module\r\n");
return status;
}
status = file->close(file);
if (status != EFI_SUCCESS) {
err(
loader->system,
"failed to close module file\r\n");
return status;
}
}
return EFI_SUCCESS;
}
static efi_status_t exit_efi_boot_services(struct loader *loader)
{
struct efi_memory_descriptor *mmap;
efi_uint_t mmap_size = 4096;
efi_uint_t mmap_key;
efi_uint_t desc_size;
uint32_t desc_version;
efi_status_t status;
while (1) {
status = loader->system->boot->allocate_pool(
EFI_LOADER_DATA,
mmap_size,
(void **)&mmap);
if (status != EFI_SUCCESS)
return status;
status = loader->system->boot->get_memory_map(
&mmap_size,
mmap,
&mmap_key,
&desc_size,
&desc_version);
if (status == EFI_SUCCESS)
break;
loader->system->boot->free_pool(mmap);
// If the buffer size turned out too small then get_memory_map
// should have updated mmap_size to contain the buffer size
// needed for the memory map. However subsequent free_pool and
// allocate_pool might change the memory map and therefore I
// additionally multiply it by 2.
if (status == EFI_BUFFER_TOO_SMALL) {
mmap_size *= 2;
continue;
}
return status;
}
status = loader->system->boot->exit_boot_services(
loader->handle,
mmap_key);
if (status != EFI_SUCCESS)
loader->system->boot->free_pool(mmap);
return status;
}
efi_status_t start_kernel(struct loader *loader)
{
efi_status_t status = EFI_SUCCESS;
void (ELFABI *entry)(struct reserve *, size_t);
info(loader->system, "Shutting down UEFI boot services\r\n");
status = exit_efi_boot_services(loader);
if (status != EFI_SUCCESS)
return status;
/* If we got this far there is no way back since all the EFI services
* have been shut down by this point. */
entry = (void (ELFABI *)(struct reserve *, size_t))
loader->kernel_image_entry;
(*entry)(loader->reserve, loader->reserves);
while (1) {}
return EFI_LOAD_ERROR;
}