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