1
2
3
4
5
6
7
8
9#include <linux/efi.h>
10#include <linux/pci.h>
11
12#include <asm/efi.h>
13#include <asm/e820/types.h>
14#include <asm/setup.h>
15#include <asm/desc.h>
16
17#include "../string.h"
18#include "eboot.h"
19
20static efi_system_table_t *sys_table;
21
22static struct efi_config *efi_early;
23
24__pure const struct efi_config *__efi_early(void)
25{
26 return efi_early;
27}
28
29#define BOOT_SERVICES(bits) \
30static void setup_boot_services##bits(struct efi_config *c) \
31{ \
32 efi_system_table_##bits##_t *table; \
33 \
34 table = (typeof(table))sys_table; \
35 \
36 c->runtime_services = table->runtime; \
37 c->boot_services = table->boottime; \
38 c->text_output = table->con_out; \
39}
40BOOT_SERVICES(32);
41BOOT_SERVICES(64);
42
43void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
44{
45 efi_call_proto(efi_simple_text_output_protocol, output_string,
46 efi_early->text_output, str);
47}
48
49static efi_status_t
50preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
51{
52 struct pci_setup_rom *rom = NULL;
53 efi_status_t status;
54 unsigned long size;
55 uint64_t romsize;
56 void *romimage;
57
58
59
60
61
62
63
64
65 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
66 romimage, pci);
67 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
68 if (!romimage || !romsize || romsize > SZ_16M)
69 return EFI_INVALID_PARAMETER;
70
71 size = romsize + sizeof(*rom);
72
73 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
74 if (status != EFI_SUCCESS) {
75 efi_printk(sys_table, "Failed to allocate memory for 'rom'\n");
76 return status;
77 }
78
79 memset(rom, 0, sizeof(*rom));
80
81 rom->data.type = SETUP_PCI;
82 rom->data.len = size - sizeof(struct setup_data);
83 rom->data.next = 0;
84 rom->pcilen = pci->romsize;
85 *__rom = rom;
86
87 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
88 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
89 &rom->vendor);
90
91 if (status != EFI_SUCCESS) {
92 efi_printk(sys_table, "Failed to read rom->vendor\n");
93 goto free_struct;
94 }
95
96 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
97 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
98 &rom->devid);
99
100 if (status != EFI_SUCCESS) {
101 efi_printk(sys_table, "Failed to read rom->devid\n");
102 goto free_struct;
103 }
104
105 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
106 &rom->segment, &rom->bus, &rom->device,
107 &rom->function);
108
109 if (status != EFI_SUCCESS)
110 goto free_struct;
111
112 memcpy(rom->romdata, romimage, romsize);
113 return status;
114
115free_struct:
116 efi_call_early(free_pool, rom);
117 return status;
118}
119
120
121
122
123
124
125
126
127
128
129static void setup_efi_pci(struct boot_params *params)
130{
131 efi_status_t status;
132 void **pci_handle = NULL;
133 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
134 unsigned long size = 0;
135 unsigned long nr_pci;
136 struct setup_data *data;
137 int i;
138
139 status = efi_call_early(locate_handle,
140 EFI_LOCATE_BY_PROTOCOL,
141 &pci_proto, NULL, &size, pci_handle);
142
143 if (status == EFI_BUFFER_TOO_SMALL) {
144 status = efi_call_early(allocate_pool,
145 EFI_LOADER_DATA,
146 size, (void **)&pci_handle);
147
148 if (status != EFI_SUCCESS) {
149 efi_printk(sys_table, "Failed to allocate memory for 'pci_handle'\n");
150 return;
151 }
152
153 status = efi_call_early(locate_handle,
154 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
155 NULL, &size, pci_handle);
156 }
157
158 if (status != EFI_SUCCESS)
159 goto free_handle;
160
161 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
162
163 while (data && data->next)
164 data = (struct setup_data *)(unsigned long)data->next;
165
166 nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
167 for (i = 0; i < nr_pci; i++) {
168 efi_pci_io_protocol_t *pci = NULL;
169 struct pci_setup_rom *rom;
170
171 status = efi_call_early(handle_protocol,
172 efi_is_64bit() ? ((u64 *)pci_handle)[i]
173 : ((u32 *)pci_handle)[i],
174 &pci_proto, (void **)&pci);
175 if (status != EFI_SUCCESS || !pci)
176 continue;
177
178 status = preserve_pci_rom_image(pci, &rom);
179 if (status != EFI_SUCCESS)
180 continue;
181
182 if (data)
183 data->next = (unsigned long)rom;
184 else
185 params->hdr.setup_data = (unsigned long)rom;
186
187 data = (struct setup_data *)rom;
188 }
189
190free_handle:
191 efi_call_early(free_pool, pci_handle);
192}
193
194static void retrieve_apple_device_properties(struct boot_params *boot_params)
195{
196 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
197 struct setup_data *data, *new;
198 efi_status_t status;
199 u32 size = 0;
200 void *p;
201
202 status = efi_call_early(locate_protocol, &guid, NULL, &p);
203 if (status != EFI_SUCCESS)
204 return;
205
206 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
207 efi_printk(sys_table, "Unsupported properties proto version\n");
208 return;
209 }
210
211 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
212 if (!size)
213 return;
214
215 do {
216 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
217 size + sizeof(struct setup_data), &new);
218 if (status != EFI_SUCCESS) {
219 efi_printk(sys_table, "Failed to allocate memory for 'properties'\n");
220 return;
221 }
222
223 status = efi_call_proto(apple_properties_protocol, get_all, p,
224 new->data, &size);
225
226 if (status == EFI_BUFFER_TOO_SMALL)
227 efi_call_early(free_pool, new);
228 } while (status == EFI_BUFFER_TOO_SMALL);
229
230 new->type = SETUP_APPLE_PROPERTIES;
231 new->len = size;
232 new->next = 0;
233
234 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
235 if (!data) {
236 boot_params->hdr.setup_data = (unsigned long)new;
237 } else {
238 while (data->next)
239 data = (struct setup_data *)(unsigned long)data->next;
240 data->next = (unsigned long)new;
241 }
242}
243
244static const efi_char16_t apple[] = L"Apple";
245
246static void setup_quirks(struct boot_params *boot_params)
247{
248 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
249 efi_table_attr(efi_system_table, fw_vendor, sys_table);
250
251 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
252 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
253 retrieve_apple_device_properties(boot_params);
254 }
255}
256
257
258
259
260static efi_status_t
261setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
262{
263 efi_status_t status;
264 u32 width, height;
265 void **uga_handle = NULL;
266 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
267 unsigned long nr_ugas;
268 int i;
269
270 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
271 size, (void **)&uga_handle);
272 if (status != EFI_SUCCESS)
273 return status;
274
275 status = efi_call_early(locate_handle,
276 EFI_LOCATE_BY_PROTOCOL,
277 uga_proto, NULL, &size, uga_handle);
278 if (status != EFI_SUCCESS)
279 goto free_handle;
280
281 height = 0;
282 width = 0;
283
284 first_uga = NULL;
285 nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
286 for (i = 0; i < nr_ugas; i++) {
287 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
288 u32 w, h, depth, refresh;
289 void *pciio;
290 unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
291 : ((u32 *)uga_handle)[i];
292
293 status = efi_call_early(handle_protocol, handle,
294 uga_proto, (void **)&uga);
295 if (status != EFI_SUCCESS)
296 continue;
297
298 pciio = NULL;
299 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
300
301 status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
302 &w, &h, &depth, &refresh);
303 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
304 width = w;
305 height = h;
306
307
308
309
310
311 if (pciio)
312 break;
313
314 first_uga = uga;
315 }
316 }
317
318 if (!width && !height)
319 goto free_handle;
320
321
322 si->orig_video_isVGA = VIDEO_TYPE_EFI;
323
324 si->lfb_depth = 32;
325 si->lfb_width = width;
326 si->lfb_height = height;
327
328 si->red_size = 8;
329 si->red_pos = 16;
330 si->green_size = 8;
331 si->green_pos = 8;
332 si->blue_size = 8;
333 si->blue_pos = 0;
334 si->rsvd_size = 8;
335 si->rsvd_pos = 24;
336
337free_handle:
338 efi_call_early(free_pool, uga_handle);
339
340 return status;
341}
342
343void setup_graphics(struct boot_params *boot_params)
344{
345 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
346 struct screen_info *si;
347 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
348 efi_status_t status;
349 unsigned long size;
350 void **gop_handle = NULL;
351 void **uga_handle = NULL;
352
353 si = &boot_params->screen_info;
354 memset(si, 0, sizeof(*si));
355
356 size = 0;
357 status = efi_call_early(locate_handle,
358 EFI_LOCATE_BY_PROTOCOL,
359 &graphics_proto, NULL, &size, gop_handle);
360 if (status == EFI_BUFFER_TOO_SMALL)
361 status = efi_setup_gop(NULL, si, &graphics_proto, size);
362
363 if (status != EFI_SUCCESS) {
364 size = 0;
365 status = efi_call_early(locate_handle,
366 EFI_LOCATE_BY_PROTOCOL,
367 &uga_proto, NULL, &size, uga_handle);
368 if (status == EFI_BUFFER_TOO_SMALL)
369 setup_uga(si, &uga_proto, size);
370 }
371}
372
373
374
375
376
377
378
379
380
381struct boot_params *make_boot_params(struct efi_config *c)
382{
383 struct boot_params *boot_params;
384 struct apm_bios_info *bi;
385 struct setup_header *hdr;
386 efi_loaded_image_t *image;
387 void *handle;
388 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
389 int options_size = 0;
390 efi_status_t status;
391 char *cmdline_ptr;
392 unsigned long ramdisk_addr;
393 unsigned long ramdisk_size;
394
395 efi_early = c;
396 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
397 handle = (void *)(unsigned long)efi_early->image_handle;
398
399
400 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
401 return NULL;
402
403 if (efi_is_64bit())
404 setup_boot_services64(efi_early);
405 else
406 setup_boot_services32(efi_early);
407
408 status = efi_call_early(handle_protocol, handle,
409 &proto, (void *)&image);
410 if (status != EFI_SUCCESS) {
411 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
412 return NULL;
413 }
414
415 status = efi_low_alloc(sys_table, 0x4000, 1,
416 (unsigned long *)&boot_params);
417 if (status != EFI_SUCCESS) {
418 efi_printk(sys_table, "Failed to allocate lowmem for boot params\n");
419 return NULL;
420 }
421
422 memset(boot_params, 0x0, 0x4000);
423
424 hdr = &boot_params->hdr;
425 bi = &boot_params->apm_bios_info;
426
427
428 memcpy(&hdr->jump, image->image_base + 512, 512);
429
430
431
432
433
434 hdr->root_flags = 1;
435 hdr->vid_mode = 0xffff;
436 hdr->boot_flag = 0xAA55;
437
438 hdr->type_of_loader = 0x21;
439
440
441 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
442 if (!cmdline_ptr)
443 goto fail;
444
445 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
446
447 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
448
449 hdr->ramdisk_image = 0;
450 hdr->ramdisk_size = 0;
451
452
453 memset(bi, 0, sizeof(*bi));
454
455 status = efi_parse_options(cmdline_ptr);
456 if (status != EFI_SUCCESS)
457 goto fail2;
458
459 status = handle_cmdline_files(sys_table, image,
460 (char *)(unsigned long)hdr->cmd_line_ptr,
461 "initrd=", hdr->initrd_addr_max,
462 &ramdisk_addr, &ramdisk_size);
463
464 if (status != EFI_SUCCESS &&
465 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
466 efi_printk(sys_table, "Trying to load files to higher address\n");
467 status = handle_cmdline_files(sys_table, image,
468 (char *)(unsigned long)hdr->cmd_line_ptr,
469 "initrd=", -1UL,
470 &ramdisk_addr, &ramdisk_size);
471 }
472
473 if (status != EFI_SUCCESS)
474 goto fail2;
475 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
476 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
477 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
478 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
479
480 return boot_params;
481
482fail2:
483 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
484fail:
485 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
486
487 return NULL;
488}
489
490static void add_e820ext(struct boot_params *params,
491 struct setup_data *e820ext, u32 nr_entries)
492{
493 struct setup_data *data;
494
495 e820ext->type = SETUP_E820_EXT;
496 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
497 e820ext->next = 0;
498
499 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
500
501 while (data && data->next)
502 data = (struct setup_data *)(unsigned long)data->next;
503
504 if (data)
505 data->next = (unsigned long)e820ext;
506 else
507 params->hdr.setup_data = (unsigned long)e820ext;
508}
509
510static efi_status_t
511setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
512{
513 struct boot_e820_entry *entry = params->e820_table;
514 struct efi_info *efi = ¶ms->efi_info;
515 struct boot_e820_entry *prev = NULL;
516 u32 nr_entries;
517 u32 nr_desc;
518 int i;
519
520 nr_entries = 0;
521 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
522
523 for (i = 0; i < nr_desc; i++) {
524 efi_memory_desc_t *d;
525 unsigned int e820_type = 0;
526 unsigned long m = efi->efi_memmap;
527
528#ifdef CONFIG_X86_64
529 m |= (u64)efi->efi_memmap_hi << 32;
530#endif
531
532 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
533 switch (d->type) {
534 case EFI_RESERVED_TYPE:
535 case EFI_RUNTIME_SERVICES_CODE:
536 case EFI_RUNTIME_SERVICES_DATA:
537 case EFI_MEMORY_MAPPED_IO:
538 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
539 case EFI_PAL_CODE:
540 e820_type = E820_TYPE_RESERVED;
541 break;
542
543 case EFI_UNUSABLE_MEMORY:
544 e820_type = E820_TYPE_UNUSABLE;
545 break;
546
547 case EFI_ACPI_RECLAIM_MEMORY:
548 e820_type = E820_TYPE_ACPI;
549 break;
550
551 case EFI_LOADER_CODE:
552 case EFI_LOADER_DATA:
553 case EFI_BOOT_SERVICES_CODE:
554 case EFI_BOOT_SERVICES_DATA:
555 case EFI_CONVENTIONAL_MEMORY:
556 e820_type = E820_TYPE_RAM;
557 break;
558
559 case EFI_ACPI_MEMORY_NVS:
560 e820_type = E820_TYPE_NVS;
561 break;
562
563 case EFI_PERSISTENT_MEMORY:
564 e820_type = E820_TYPE_PMEM;
565 break;
566
567 default:
568 continue;
569 }
570
571
572 if (prev && prev->type == e820_type &&
573 (prev->addr + prev->size) == d->phys_addr) {
574 prev->size += d->num_pages << 12;
575 continue;
576 }
577
578 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
579 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
580 sizeof(struct setup_data);
581
582 if (!e820ext || e820ext_size < need)
583 return EFI_BUFFER_TOO_SMALL;
584
585
586 entry = (struct boot_e820_entry *)e820ext->data;
587 }
588
589 entry->addr = d->phys_addr;
590 entry->size = d->num_pages << PAGE_SHIFT;
591 entry->type = e820_type;
592 prev = entry++;
593 nr_entries++;
594 }
595
596 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
597 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
598
599 add_e820ext(params, e820ext, nr_e820ext);
600 nr_entries -= nr_e820ext;
601 }
602
603 params->e820_entries = (u8)nr_entries;
604
605 return EFI_SUCCESS;
606}
607
608static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
609 u32 *e820ext_size)
610{
611 efi_status_t status;
612 unsigned long size;
613
614 size = sizeof(struct setup_data) +
615 sizeof(struct e820_entry) * nr_desc;
616
617 if (*e820ext) {
618 efi_call_early(free_pool, *e820ext);
619 *e820ext = NULL;
620 *e820ext_size = 0;
621 }
622
623 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
624 size, (void **)e820ext);
625 if (status == EFI_SUCCESS)
626 *e820ext_size = size;
627
628 return status;
629}
630
631static efi_status_t allocate_e820(struct boot_params *params,
632 struct setup_data **e820ext,
633 u32 *e820ext_size)
634{
635 unsigned long map_size, desc_size, buff_size;
636 struct efi_boot_memmap boot_map;
637 efi_memory_desc_t *map;
638 efi_status_t status;
639 __u32 nr_desc;
640
641 boot_map.map = ↦
642 boot_map.map_size = &map_size;
643 boot_map.desc_size = &desc_size;
644 boot_map.desc_ver = NULL;
645 boot_map.key_ptr = NULL;
646 boot_map.buff_size = &buff_size;
647
648 status = efi_get_memory_map(sys_table, &boot_map);
649 if (status != EFI_SUCCESS)
650 return status;
651
652 nr_desc = buff_size / desc_size;
653
654 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
655 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
656
657 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
658 if (status != EFI_SUCCESS)
659 return status;
660 }
661
662 return EFI_SUCCESS;
663}
664
665struct exit_boot_struct {
666 struct boot_params *boot_params;
667 struct efi_info *efi;
668};
669
670static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
671 struct efi_boot_memmap *map,
672 void *priv)
673{
674 const char *signature;
675 struct exit_boot_struct *p = priv;
676
677 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
678 : EFI32_LOADER_SIGNATURE;
679 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
680
681 p->efi->efi_systab = (unsigned long)sys_table_arg;
682 p->efi->efi_memdesc_size = *map->desc_size;
683 p->efi->efi_memdesc_version = *map->desc_ver;
684 p->efi->efi_memmap = (unsigned long)*map->map;
685 p->efi->efi_memmap_size = *map->map_size;
686
687#ifdef CONFIG_X86_64
688 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
689 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
690#endif
691
692 return EFI_SUCCESS;
693}
694
695static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
696{
697 unsigned long map_sz, key, desc_size, buff_size;
698 efi_memory_desc_t *mem_map;
699 struct setup_data *e820ext = NULL;
700 __u32 e820ext_size = 0;
701 efi_status_t status;
702 __u32 desc_version;
703 struct efi_boot_memmap map;
704 struct exit_boot_struct priv;
705
706 map.map = &mem_map;
707 map.map_size = &map_sz;
708 map.desc_size = &desc_size;
709 map.desc_ver = &desc_version;
710 map.key_ptr = &key;
711 map.buff_size = &buff_size;
712 priv.boot_params = boot_params;
713 priv.efi = &boot_params->efi_info;
714
715 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
716 if (status != EFI_SUCCESS)
717 return status;
718
719
720 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
721 exit_boot_func);
722 if (status != EFI_SUCCESS)
723 return status;
724
725
726 boot_params->alt_mem_k = 32 * 1024;
727
728 status = setup_e820(boot_params, e820ext, e820ext_size);
729 if (status != EFI_SUCCESS)
730 return status;
731
732 return EFI_SUCCESS;
733}
734
735
736
737
738
739struct boot_params *
740efi_main(struct efi_config *c, struct boot_params *boot_params)
741{
742 struct desc_ptr *gdt = NULL;
743 struct setup_header *hdr = &boot_params->hdr;
744 efi_status_t status;
745 struct desc_struct *desc;
746 void *handle;
747 efi_system_table_t *_table;
748 unsigned long cmdline_paddr;
749
750 efi_early = c;
751
752 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
753 handle = (void *)(unsigned long)efi_early->image_handle;
754
755 sys_table = _table;
756
757
758 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
759 goto fail;
760
761 if (efi_is_64bit())
762 setup_boot_services64(efi_early);
763 else
764 setup_boot_services32(efi_early);
765
766
767
768
769
770
771 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
772 ((u64)boot_params->ext_cmd_line_ptr << 32));
773 efi_parse_options((char *)cmdline_paddr);
774
775
776
777
778
779 if (boot_params->secure_boot == efi_secureboot_mode_unset)
780 boot_params->secure_boot = efi_get_secureboot(sys_table);
781
782
783 efi_enable_reset_attack_mitigation(sys_table);
784 efi_retrieve_tpm2_eventlog(sys_table);
785
786 setup_graphics(boot_params);
787
788 setup_efi_pci(boot_params);
789
790 setup_quirks(boot_params);
791
792 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
793 sizeof(*gdt), (void **)&gdt);
794 if (status != EFI_SUCCESS) {
795 efi_printk(sys_table, "Failed to allocate memory for 'gdt' structure\n");
796 goto fail;
797 }
798
799 gdt->size = 0x800;
800 status = efi_low_alloc(sys_table, gdt->size, 8,
801 (unsigned long *)&gdt->address);
802 if (status != EFI_SUCCESS) {
803 efi_printk(sys_table, "Failed to allocate memory for 'gdt'\n");
804 goto fail;
805 }
806
807
808
809
810
811 if (hdr->pref_address != hdr->code32_start) {
812 unsigned long bzimage_addr = hdr->code32_start;
813 status = efi_relocate_kernel(sys_table, &bzimage_addr,
814 hdr->init_size, hdr->init_size,
815 hdr->pref_address,
816 hdr->kernel_alignment);
817 if (status != EFI_SUCCESS) {
818 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
819 goto fail;
820 }
821
822 hdr->pref_address = hdr->code32_start;
823 hdr->code32_start = bzimage_addr;
824 }
825
826 status = exit_boot(boot_params, handle);
827 if (status != EFI_SUCCESS) {
828 efi_printk(sys_table, "exit_boot() failed!\n");
829 goto fail;
830 }
831
832 memset((char *)gdt->address, 0x0, gdt->size);
833 desc = (struct desc_struct *)gdt->address;
834
835
836 desc++;
837
838 if (IS_ENABLED(CONFIG_X86_64)) {
839
840 desc->limit0 = 0xffff;
841 desc->base0 = 0x0000;
842 desc->base1 = 0x0000;
843 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
844 desc->s = DESC_TYPE_CODE_DATA;
845 desc->dpl = 0;
846 desc->p = 1;
847 desc->limit1 = 0xf;
848 desc->avl = 0;
849 desc->l = 0;
850 desc->d = SEG_OP_SIZE_32BIT;
851 desc->g = SEG_GRANULARITY_4KB;
852 desc->base2 = 0x00;
853
854 desc++;
855 } else {
856
857 desc++;
858 }
859
860
861 desc->limit0 = 0xffff;
862 desc->base0 = 0x0000;
863 desc->base1 = 0x0000;
864 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
865 desc->s = DESC_TYPE_CODE_DATA;
866 desc->dpl = 0;
867 desc->p = 1;
868 desc->limit1 = 0xf;
869 desc->avl = 0;
870
871 if (IS_ENABLED(CONFIG_X86_64)) {
872 desc->l = 1;
873 desc->d = 0;
874 } else {
875 desc->l = 0;
876 desc->d = SEG_OP_SIZE_32BIT;
877 }
878 desc->g = SEG_GRANULARITY_4KB;
879 desc->base2 = 0x00;
880 desc++;
881
882
883 desc->limit0 = 0xffff;
884 desc->base0 = 0x0000;
885 desc->base1 = 0x0000;
886 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
887 desc->s = DESC_TYPE_CODE_DATA;
888 desc->dpl = 0;
889 desc->p = 1;
890 desc->limit1 = 0xf;
891 desc->avl = 0;
892 desc->l = 0;
893 desc->d = SEG_OP_SIZE_32BIT;
894 desc->g = SEG_GRANULARITY_4KB;
895 desc->base2 = 0x00;
896 desc++;
897
898 if (IS_ENABLED(CONFIG_X86_64)) {
899
900 desc->limit0 = 0x0000;
901 desc->base0 = 0x0000;
902 desc->base1 = 0x0000;
903 desc->type = SEG_TYPE_TSS;
904 desc->s = 0;
905 desc->dpl = 0;
906 desc->p = 1;
907 desc->limit1 = 0x0;
908 desc->avl = 0;
909 desc->l = 0;
910 desc->d = 0;
911 desc->g = SEG_GRANULARITY_4KB;
912 desc->base2 = 0x00;
913 desc++;
914 }
915
916 asm volatile("cli");
917 asm volatile ("lgdt %0" : : "m" (*gdt));
918
919 return boot_params;
920fail:
921 efi_printk(sys_table, "efi_main() failed!\n");
922
923 return NULL;
924}
925