efi/x86: Merge 32-bit and 64-bit UGA draw protocol setup routines
[linux/fpc-iii.git] / arch / x86 / boot / compressed / eboot.c
blobac8e442db71f36dcec016102fc94fa57c0c42d6f
1 /* -----------------------------------------------------------------------
3 * Copyright 2011 Intel Corporation; author Matt Fleming
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
8 * ----------------------------------------------------------------------- */
10 #include <linux/efi.h>
11 #include <linux/pci.h>
13 #include <asm/efi.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
16 #include <asm/desc.h>
18 #include "../string.h"
19 #include "eboot.h"
21 static efi_system_table_t *sys_table;
23 static struct efi_config *efi_early;
25 __pure const struct efi_config *__efi_early(void)
27 return efi_early;
30 #define BOOT_SERVICES(bits) \
31 static void setup_boot_services##bits(struct efi_config *c) \
32 { \
33 efi_system_table_##bits##_t *table; \
35 table = (typeof(table))sys_table; \
37 c->runtime_services = table->runtime; \
38 c->boot_services = table->boottime; \
39 c->text_output = table->con_out; \
41 BOOT_SERVICES(32);
42 BOOT_SERVICES(64);
44 static inline efi_status_t __open_volume32(void *__image, void **__fh)
46 efi_file_io_interface_t *io;
47 efi_loaded_image_32_t *image = __image;
48 efi_file_handle_32_t *fh;
49 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
50 efi_status_t status;
51 void *handle = (void *)(unsigned long)image->device_handle;
52 unsigned long func;
54 status = efi_call_early(handle_protocol, handle,
55 &fs_proto, (void **)&io);
56 if (status != EFI_SUCCESS) {
57 efi_printk(sys_table, "Failed to handle fs_proto\n");
58 return status;
61 func = (unsigned long)io->open_volume;
62 status = efi_early->call(func, io, &fh);
63 if (status != EFI_SUCCESS)
64 efi_printk(sys_table, "Failed to open volume\n");
66 *__fh = fh;
68 return status;
71 static inline efi_status_t __open_volume64(void *__image, void **__fh)
73 efi_file_io_interface_t *io;
74 efi_loaded_image_64_t *image = __image;
75 efi_file_handle_64_t *fh;
76 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
77 efi_status_t status;
78 void *handle = (void *)(unsigned long)image->device_handle;
79 unsigned long func;
81 status = efi_call_early(handle_protocol, handle,
82 &fs_proto, (void **)&io);
83 if (status != EFI_SUCCESS) {
84 efi_printk(sys_table, "Failed to handle fs_proto\n");
85 return status;
88 func = (unsigned long)io->open_volume;
89 status = efi_early->call(func, io, &fh);
90 if (status != EFI_SUCCESS)
91 efi_printk(sys_table, "Failed to open volume\n");
93 *__fh = fh;
95 return status;
98 efi_status_t
99 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
101 if (efi_early->is64)
102 return __open_volume64(__image, __fh);
104 return __open_volume32(__image, __fh);
107 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
109 efi_call_proto(efi_simple_text_output_protocol, output_string,
110 efi_early->text_output, str);
113 static efi_status_t
114 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
116 struct pci_setup_rom *rom = NULL;
117 efi_status_t status;
118 unsigned long size;
119 uint64_t romsize;
120 void *romimage;
123 * Some firmware images contain EFI function pointers at the place where
124 * the romimage and romsize fields are supposed to be. Typically the EFI
125 * code is mapped at high addresses, translating to an unrealistically
126 * large romsize. The UEFI spec limits the size of option ROMs to 16
127 * MiB so we reject any ROMs over 16 MiB in size to catch this.
129 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
130 romimage, pci);
131 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
132 if (!romimage || !romsize || romsize > SZ_16M)
133 return EFI_INVALID_PARAMETER;
135 size = romsize + sizeof(*rom);
137 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
138 if (status != EFI_SUCCESS) {
139 efi_printk(sys_table, "Failed to allocate memory for 'rom'\n");
140 return status;
143 memset(rom, 0, sizeof(*rom));
145 rom->data.type = SETUP_PCI;
146 rom->data.len = size - sizeof(struct setup_data);
147 rom->data.next = 0;
148 rom->pcilen = pci->romsize;
149 *__rom = rom;
151 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
152 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
153 &rom->vendor);
155 if (status != EFI_SUCCESS) {
156 efi_printk(sys_table, "Failed to read rom->vendor\n");
157 goto free_struct;
160 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
161 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
162 &rom->devid);
164 if (status != EFI_SUCCESS) {
165 efi_printk(sys_table, "Failed to read rom->devid\n");
166 goto free_struct;
169 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
170 &rom->segment, &rom->bus, &rom->device,
171 &rom->function);
173 if (status != EFI_SUCCESS)
174 goto free_struct;
176 memcpy(rom->romdata, romimage, romsize);
177 return status;
179 free_struct:
180 efi_call_early(free_pool, rom);
181 return status;
185 * There's no way to return an informative status from this function,
186 * because any analysis (and printing of error messages) needs to be
187 * done directly at the EFI function call-site.
189 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
190 * just didn't find any PCI devices, but there's no way to tell outside
191 * the context of the call.
193 static void setup_efi_pci(struct boot_params *params)
195 efi_status_t status;
196 void **pci_handle = NULL;
197 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
198 unsigned long size = 0;
199 unsigned long nr_pci;
200 struct setup_data *data;
201 int i;
203 status = efi_call_early(locate_handle,
204 EFI_LOCATE_BY_PROTOCOL,
205 &pci_proto, NULL, &size, pci_handle);
207 if (status == EFI_BUFFER_TOO_SMALL) {
208 status = efi_call_early(allocate_pool,
209 EFI_LOADER_DATA,
210 size, (void **)&pci_handle);
212 if (status != EFI_SUCCESS) {
213 efi_printk(sys_table, "Failed to allocate memory for 'pci_handle'\n");
214 return;
217 status = efi_call_early(locate_handle,
218 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
219 NULL, &size, pci_handle);
222 if (status != EFI_SUCCESS)
223 goto free_handle;
225 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
227 while (data && data->next)
228 data = (struct setup_data *)(unsigned long)data->next;
230 nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
231 for (i = 0; i < nr_pci; i++) {
232 efi_pci_io_protocol_t *pci = NULL;
233 struct pci_setup_rom *rom;
235 status = efi_call_early(handle_protocol,
236 efi_is_64bit() ? ((u64 *)pci_handle)[i]
237 : ((u32 *)pci_handle)[i],
238 &pci_proto, (void **)&pci);
239 if (status != EFI_SUCCESS || !pci)
240 continue;
242 status = preserve_pci_rom_image(pci, &rom);
243 if (status != EFI_SUCCESS)
244 continue;
246 if (data)
247 data->next = (unsigned long)rom;
248 else
249 params->hdr.setup_data = (unsigned long)rom;
251 data = (struct setup_data *)rom;
254 free_handle:
255 efi_call_early(free_pool, pci_handle);
258 static void retrieve_apple_device_properties(struct boot_params *boot_params)
260 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
261 struct setup_data *data, *new;
262 efi_status_t status;
263 u32 size = 0;
264 void *p;
266 status = efi_call_early(locate_protocol, &guid, NULL, &p);
267 if (status != EFI_SUCCESS)
268 return;
270 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
271 efi_printk(sys_table, "Unsupported properties proto version\n");
272 return;
275 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
276 if (!size)
277 return;
279 do {
280 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
281 size + sizeof(struct setup_data), &new);
282 if (status != EFI_SUCCESS) {
283 efi_printk(sys_table, "Failed to allocate memory for 'properties'\n");
284 return;
287 status = efi_call_proto(apple_properties_protocol, get_all, p,
288 new->data, &size);
290 if (status == EFI_BUFFER_TOO_SMALL)
291 efi_call_early(free_pool, new);
292 } while (status == EFI_BUFFER_TOO_SMALL);
294 new->type = SETUP_APPLE_PROPERTIES;
295 new->len = size;
296 new->next = 0;
298 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
299 if (!data) {
300 boot_params->hdr.setup_data = (unsigned long)new;
301 } else {
302 while (data->next)
303 data = (struct setup_data *)(unsigned long)data->next;
304 data->next = (unsigned long)new;
308 static const efi_char16_t apple[] = L"Apple";
310 static void setup_quirks(struct boot_params *boot_params)
312 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
313 efi_table_attr(efi_system_table, fw_vendor, sys_table);
315 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
316 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
317 retrieve_apple_device_properties(boot_params);
322 * See if we have Universal Graphics Adapter (UGA) protocol
324 static efi_status_t
325 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
327 efi_status_t status;
328 u32 width, height;
329 void **uga_handle = NULL;
330 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
331 unsigned long nr_ugas;
332 int i;
334 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
335 size, (void **)&uga_handle);
336 if (status != EFI_SUCCESS)
337 return status;
339 status = efi_call_early(locate_handle,
340 EFI_LOCATE_BY_PROTOCOL,
341 uga_proto, NULL, &size, uga_handle);
342 if (status != EFI_SUCCESS)
343 goto free_handle;
345 height = 0;
346 width = 0;
348 first_uga = NULL;
349 nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
350 for (i = 0; i < nr_ugas; i++) {
351 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
352 u32 w, h, depth, refresh;
353 void *pciio;
354 unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
355 : ((u32 *)uga_handle)[i];
357 status = efi_call_early(handle_protocol, handle,
358 uga_proto, (void **)&uga);
359 if (status != EFI_SUCCESS)
360 continue;
362 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
364 status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
365 &w, &h, &depth, &refresh);
366 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
367 width = w;
368 height = h;
371 * Once we've found a UGA supporting PCIIO,
372 * don't bother looking any further.
374 if (pciio)
375 break;
377 first_uga = uga;
381 if (!width && !height)
382 goto free_handle;
384 /* EFI framebuffer */
385 si->orig_video_isVGA = VIDEO_TYPE_EFI;
387 si->lfb_depth = 32;
388 si->lfb_width = width;
389 si->lfb_height = height;
391 si->red_size = 8;
392 si->red_pos = 16;
393 si->green_size = 8;
394 si->green_pos = 8;
395 si->blue_size = 8;
396 si->blue_pos = 0;
397 si->rsvd_size = 8;
398 si->rsvd_pos = 24;
400 free_handle:
401 efi_call_early(free_pool, uga_handle);
403 return status;
406 void setup_graphics(struct boot_params *boot_params)
408 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
409 struct screen_info *si;
410 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
411 efi_status_t status;
412 unsigned long size;
413 void **gop_handle = NULL;
414 void **uga_handle = NULL;
416 si = &boot_params->screen_info;
417 memset(si, 0, sizeof(*si));
419 size = 0;
420 status = efi_call_early(locate_handle,
421 EFI_LOCATE_BY_PROTOCOL,
422 &graphics_proto, NULL, &size, gop_handle);
423 if (status == EFI_BUFFER_TOO_SMALL)
424 status = efi_setup_gop(NULL, si, &graphics_proto, size);
426 if (status != EFI_SUCCESS) {
427 size = 0;
428 status = efi_call_early(locate_handle,
429 EFI_LOCATE_BY_PROTOCOL,
430 &uga_proto, NULL, &size, uga_handle);
431 if (status == EFI_BUFFER_TOO_SMALL)
432 setup_uga(si, &uga_proto, size);
437 * Because the x86 boot code expects to be passed a boot_params we
438 * need to create one ourselves (usually the bootloader would create
439 * one for us).
441 * The caller is responsible for filling out ->code32_start in the
442 * returned boot_params.
444 struct boot_params *make_boot_params(struct efi_config *c)
446 struct boot_params *boot_params;
447 struct apm_bios_info *bi;
448 struct setup_header *hdr;
449 efi_loaded_image_t *image;
450 void *options, *handle;
451 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
452 int options_size = 0;
453 efi_status_t status;
454 char *cmdline_ptr;
455 u16 *s2;
456 u8 *s1;
457 int i;
458 unsigned long ramdisk_addr;
459 unsigned long ramdisk_size;
461 efi_early = c;
462 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
463 handle = (void *)(unsigned long)efi_early->image_handle;
465 /* Check if we were booted by the EFI firmware */
466 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
467 return NULL;
469 if (efi_early->is64)
470 setup_boot_services64(efi_early);
471 else
472 setup_boot_services32(efi_early);
474 status = efi_call_early(handle_protocol, handle,
475 &proto, (void *)&image);
476 if (status != EFI_SUCCESS) {
477 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
478 return NULL;
481 status = efi_low_alloc(sys_table, 0x4000, 1,
482 (unsigned long *)&boot_params);
483 if (status != EFI_SUCCESS) {
484 efi_printk(sys_table, "Failed to allocate lowmem for boot params\n");
485 return NULL;
488 memset(boot_params, 0x0, 0x4000);
490 hdr = &boot_params->hdr;
491 bi = &boot_params->apm_bios_info;
493 /* Copy the second sector to boot_params */
494 memcpy(&hdr->jump, image->image_base + 512, 512);
497 * Fill out some of the header fields ourselves because the
498 * EFI firmware loader doesn't load the first sector.
500 hdr->root_flags = 1;
501 hdr->vid_mode = 0xffff;
502 hdr->boot_flag = 0xAA55;
504 hdr->type_of_loader = 0x21;
506 /* Convert unicode cmdline to ascii */
507 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
508 if (!cmdline_ptr)
509 goto fail;
511 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
512 /* Fill in upper bits of command line address, NOP on 32 bit */
513 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
515 hdr->ramdisk_image = 0;
516 hdr->ramdisk_size = 0;
518 /* Clear APM BIOS info */
519 memset(bi, 0, sizeof(*bi));
521 status = efi_parse_options(cmdline_ptr);
522 if (status != EFI_SUCCESS)
523 goto fail2;
525 status = handle_cmdline_files(sys_table, image,
526 (char *)(unsigned long)hdr->cmd_line_ptr,
527 "initrd=", hdr->initrd_addr_max,
528 &ramdisk_addr, &ramdisk_size);
530 if (status != EFI_SUCCESS &&
531 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
532 efi_printk(sys_table, "Trying to load files to higher address\n");
533 status = handle_cmdline_files(sys_table, image,
534 (char *)(unsigned long)hdr->cmd_line_ptr,
535 "initrd=", -1UL,
536 &ramdisk_addr, &ramdisk_size);
539 if (status != EFI_SUCCESS)
540 goto fail2;
541 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
542 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
543 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
544 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
546 return boot_params;
548 fail2:
549 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
550 fail:
551 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
553 return NULL;
556 static void add_e820ext(struct boot_params *params,
557 struct setup_data *e820ext, u32 nr_entries)
559 struct setup_data *data;
560 efi_status_t status;
561 unsigned long size;
563 e820ext->type = SETUP_E820_EXT;
564 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
565 e820ext->next = 0;
567 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
569 while (data && data->next)
570 data = (struct setup_data *)(unsigned long)data->next;
572 if (data)
573 data->next = (unsigned long)e820ext;
574 else
575 params->hdr.setup_data = (unsigned long)e820ext;
578 static efi_status_t
579 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
581 struct boot_e820_entry *entry = params->e820_table;
582 struct efi_info *efi = &params->efi_info;
583 struct boot_e820_entry *prev = NULL;
584 u32 nr_entries;
585 u32 nr_desc;
586 int i;
588 nr_entries = 0;
589 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
591 for (i = 0; i < nr_desc; i++) {
592 efi_memory_desc_t *d;
593 unsigned int e820_type = 0;
594 unsigned long m = efi->efi_memmap;
596 #ifdef CONFIG_X86_64
597 m |= (u64)efi->efi_memmap_hi << 32;
598 #endif
600 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
601 switch (d->type) {
602 case EFI_RESERVED_TYPE:
603 case EFI_RUNTIME_SERVICES_CODE:
604 case EFI_RUNTIME_SERVICES_DATA:
605 case EFI_MEMORY_MAPPED_IO:
606 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
607 case EFI_PAL_CODE:
608 e820_type = E820_TYPE_RESERVED;
609 break;
611 case EFI_UNUSABLE_MEMORY:
612 e820_type = E820_TYPE_UNUSABLE;
613 break;
615 case EFI_ACPI_RECLAIM_MEMORY:
616 e820_type = E820_TYPE_ACPI;
617 break;
619 case EFI_LOADER_CODE:
620 case EFI_LOADER_DATA:
621 case EFI_BOOT_SERVICES_CODE:
622 case EFI_BOOT_SERVICES_DATA:
623 case EFI_CONVENTIONAL_MEMORY:
624 e820_type = E820_TYPE_RAM;
625 break;
627 case EFI_ACPI_MEMORY_NVS:
628 e820_type = E820_TYPE_NVS;
629 break;
631 case EFI_PERSISTENT_MEMORY:
632 e820_type = E820_TYPE_PMEM;
633 break;
635 default:
636 continue;
639 /* Merge adjacent mappings */
640 if (prev && prev->type == e820_type &&
641 (prev->addr + prev->size) == d->phys_addr) {
642 prev->size += d->num_pages << 12;
643 continue;
646 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
647 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
648 sizeof(struct setup_data);
650 if (!e820ext || e820ext_size < need)
651 return EFI_BUFFER_TOO_SMALL;
653 /* boot_params map full, switch to e820 extended */
654 entry = (struct boot_e820_entry *)e820ext->data;
657 entry->addr = d->phys_addr;
658 entry->size = d->num_pages << PAGE_SHIFT;
659 entry->type = e820_type;
660 prev = entry++;
661 nr_entries++;
664 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
665 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
667 add_e820ext(params, e820ext, nr_e820ext);
668 nr_entries -= nr_e820ext;
671 params->e820_entries = (u8)nr_entries;
673 return EFI_SUCCESS;
676 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
677 u32 *e820ext_size)
679 efi_status_t status;
680 unsigned long size;
682 size = sizeof(struct setup_data) +
683 sizeof(struct e820_entry) * nr_desc;
685 if (*e820ext) {
686 efi_call_early(free_pool, *e820ext);
687 *e820ext = NULL;
688 *e820ext_size = 0;
691 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
692 size, (void **)e820ext);
693 if (status == EFI_SUCCESS)
694 *e820ext_size = size;
696 return status;
699 struct exit_boot_struct {
700 struct boot_params *boot_params;
701 struct efi_info *efi;
702 struct setup_data *e820ext;
703 __u32 e820ext_size;
704 bool is64;
707 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
708 struct efi_boot_memmap *map,
709 void *priv)
711 static bool first = true;
712 const char *signature;
713 __u32 nr_desc;
714 efi_status_t status;
715 struct exit_boot_struct *p = priv;
717 if (first) {
718 nr_desc = *map->buff_size / *map->desc_size;
719 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
720 u32 nr_e820ext = nr_desc -
721 ARRAY_SIZE(p->boot_params->e820_table);
723 status = alloc_e820ext(nr_e820ext, &p->e820ext,
724 &p->e820ext_size);
725 if (status != EFI_SUCCESS)
726 return status;
728 first = false;
731 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
732 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
734 p->efi->efi_systab = (unsigned long)sys_table_arg;
735 p->efi->efi_memdesc_size = *map->desc_size;
736 p->efi->efi_memdesc_version = *map->desc_ver;
737 p->efi->efi_memmap = (unsigned long)*map->map;
738 p->efi->efi_memmap_size = *map->map_size;
740 #ifdef CONFIG_X86_64
741 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
742 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
743 #endif
745 return EFI_SUCCESS;
748 static efi_status_t exit_boot(struct boot_params *boot_params,
749 void *handle, bool is64)
751 unsigned long map_sz, key, desc_size, buff_size;
752 efi_memory_desc_t *mem_map;
753 struct setup_data *e820ext;
754 __u32 e820ext_size;
755 efi_status_t status;
756 __u32 desc_version;
757 struct efi_boot_memmap map;
758 struct exit_boot_struct priv;
760 map.map = &mem_map;
761 map.map_size = &map_sz;
762 map.desc_size = &desc_size;
763 map.desc_ver = &desc_version;
764 map.key_ptr = &key;
765 map.buff_size = &buff_size;
766 priv.boot_params = boot_params;
767 priv.efi = &boot_params->efi_info;
768 priv.e820ext = NULL;
769 priv.e820ext_size = 0;
770 priv.is64 = is64;
772 /* Might as well exit boot services now */
773 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
774 exit_boot_func);
775 if (status != EFI_SUCCESS)
776 return status;
778 e820ext = priv.e820ext;
779 e820ext_size = priv.e820ext_size;
781 /* Historic? */
782 boot_params->alt_mem_k = 32 * 1024;
784 status = setup_e820(boot_params, e820ext, e820ext_size);
785 if (status != EFI_SUCCESS)
786 return status;
788 return EFI_SUCCESS;
792 * On success we return a pointer to a boot_params structure, and NULL
793 * on failure.
795 struct boot_params *
796 efi_main(struct efi_config *c, struct boot_params *boot_params)
798 struct desc_ptr *gdt = NULL;
799 efi_loaded_image_t *image;
800 struct setup_header *hdr = &boot_params->hdr;
801 efi_status_t status;
802 struct desc_struct *desc;
803 void *handle;
804 efi_system_table_t *_table;
805 bool is64;
807 efi_early = c;
809 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
810 handle = (void *)(unsigned long)efi_early->image_handle;
811 is64 = efi_early->is64;
813 sys_table = _table;
815 /* Check if we were booted by the EFI firmware */
816 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
817 goto fail;
819 if (is64)
820 setup_boot_services64(efi_early);
821 else
822 setup_boot_services32(efi_early);
825 * If the boot loader gave us a value for secure_boot then we use that,
826 * otherwise we ask the BIOS.
828 if (boot_params->secure_boot == efi_secureboot_mode_unset)
829 boot_params->secure_boot = efi_get_secureboot(sys_table);
831 /* Ask the firmware to clear memory on unclean shutdown */
832 efi_enable_reset_attack_mitigation(sys_table);
833 efi_retrieve_tpm2_eventlog(sys_table);
835 setup_graphics(boot_params);
837 setup_efi_pci(boot_params);
839 setup_quirks(boot_params);
841 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
842 sizeof(*gdt), (void **)&gdt);
843 if (status != EFI_SUCCESS) {
844 efi_printk(sys_table, "Failed to allocate memory for 'gdt' structure\n");
845 goto fail;
848 gdt->size = 0x800;
849 status = efi_low_alloc(sys_table, gdt->size, 8,
850 (unsigned long *)&gdt->address);
851 if (status != EFI_SUCCESS) {
852 efi_printk(sys_table, "Failed to allocate memory for 'gdt'\n");
853 goto fail;
857 * If the kernel isn't already loaded at the preferred load
858 * address, relocate it.
860 if (hdr->pref_address != hdr->code32_start) {
861 unsigned long bzimage_addr = hdr->code32_start;
862 status = efi_relocate_kernel(sys_table, &bzimage_addr,
863 hdr->init_size, hdr->init_size,
864 hdr->pref_address,
865 hdr->kernel_alignment);
866 if (status != EFI_SUCCESS) {
867 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
868 goto fail;
871 hdr->pref_address = hdr->code32_start;
872 hdr->code32_start = bzimage_addr;
875 status = exit_boot(boot_params, handle, is64);
876 if (status != EFI_SUCCESS) {
877 efi_printk(sys_table, "exit_boot() failed!\n");
878 goto fail;
881 memset((char *)gdt->address, 0x0, gdt->size);
882 desc = (struct desc_struct *)gdt->address;
884 /* The first GDT is a dummy. */
885 desc++;
887 if (IS_ENABLED(CONFIG_X86_64)) {
888 /* __KERNEL32_CS */
889 desc->limit0 = 0xffff;
890 desc->base0 = 0x0000;
891 desc->base1 = 0x0000;
892 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
893 desc->s = DESC_TYPE_CODE_DATA;
894 desc->dpl = 0;
895 desc->p = 1;
896 desc->limit1 = 0xf;
897 desc->avl = 0;
898 desc->l = 0;
899 desc->d = SEG_OP_SIZE_32BIT;
900 desc->g = SEG_GRANULARITY_4KB;
901 desc->base2 = 0x00;
903 desc++;
904 } else {
905 /* Second entry is unused on 32-bit */
906 desc++;
909 /* __KERNEL_CS */
910 desc->limit0 = 0xffff;
911 desc->base0 = 0x0000;
912 desc->base1 = 0x0000;
913 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
914 desc->s = DESC_TYPE_CODE_DATA;
915 desc->dpl = 0;
916 desc->p = 1;
917 desc->limit1 = 0xf;
918 desc->avl = 0;
920 if (IS_ENABLED(CONFIG_X86_64)) {
921 desc->l = 1;
922 desc->d = 0;
923 } else {
924 desc->l = 0;
925 desc->d = SEG_OP_SIZE_32BIT;
927 desc->g = SEG_GRANULARITY_4KB;
928 desc->base2 = 0x00;
929 desc++;
931 /* __KERNEL_DS */
932 desc->limit0 = 0xffff;
933 desc->base0 = 0x0000;
934 desc->base1 = 0x0000;
935 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
936 desc->s = DESC_TYPE_CODE_DATA;
937 desc->dpl = 0;
938 desc->p = 1;
939 desc->limit1 = 0xf;
940 desc->avl = 0;
941 desc->l = 0;
942 desc->d = SEG_OP_SIZE_32BIT;
943 desc->g = SEG_GRANULARITY_4KB;
944 desc->base2 = 0x00;
945 desc++;
947 if (IS_ENABLED(CONFIG_X86_64)) {
948 /* Task segment value */
949 desc->limit0 = 0x0000;
950 desc->base0 = 0x0000;
951 desc->base1 = 0x0000;
952 desc->type = SEG_TYPE_TSS;
953 desc->s = 0;
954 desc->dpl = 0;
955 desc->p = 1;
956 desc->limit1 = 0x0;
957 desc->avl = 0;
958 desc->l = 0;
959 desc->d = 0;
960 desc->g = SEG_GRANULARITY_4KB;
961 desc->base2 = 0x00;
962 desc++;
965 asm volatile("cli");
966 asm volatile ("lgdt %0" : : "m" (*gdt));
968 return boot_params;
969 fail:
970 efi_printk(sys_table, "efi_main() failed!\n");
972 return NULL;