x86/oprofile: Fix bogus GCC-8 warning in nmi_setup()
[cris-mirror.git] / arch / x86 / boot / compressed / eboot.c
blob353e20c3f114f3132ef18dbc03e7a8110f4e22df
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;
67 return status;
70 static inline efi_status_t __open_volume64(void *__image, void **__fh)
72 efi_file_io_interface_t *io;
73 efi_loaded_image_64_t *image = __image;
74 efi_file_handle_64_t *fh;
75 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
76 efi_status_t status;
77 void *handle = (void *)(unsigned long)image->device_handle;
78 unsigned long func;
80 status = efi_call_early(handle_protocol, handle,
81 &fs_proto, (void **)&io);
82 if (status != EFI_SUCCESS) {
83 efi_printk(sys_table, "Failed to handle fs_proto\n");
84 return status;
87 func = (unsigned long)io->open_volume;
88 status = efi_early->call(func, io, &fh);
89 if (status != EFI_SUCCESS)
90 efi_printk(sys_table, "Failed to open volume\n");
92 *__fh = fh;
93 return status;
96 efi_status_t
97 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
99 if (efi_early->is64)
100 return __open_volume64(__image, __fh);
102 return __open_volume32(__image, __fh);
105 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
107 efi_call_proto(efi_simple_text_output_protocol, output_string,
108 efi_early->text_output, str);
111 static efi_status_t
112 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
114 struct pci_setup_rom *rom = NULL;
115 efi_status_t status;
116 unsigned long size;
117 uint64_t attributes;
119 status = efi_early->call(pci->attributes, pci,
120 EfiPciIoAttributeOperationGet, 0, 0,
121 &attributes);
122 if (status != EFI_SUCCESS)
123 return status;
125 if (!pci->romimage || !pci->romsize)
126 return EFI_INVALID_PARAMETER;
128 size = pci->romsize + sizeof(*rom);
130 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
131 if (status != EFI_SUCCESS) {
132 efi_printk(sys_table, "Failed to alloc mem for rom\n");
133 return status;
136 memset(rom, 0, sizeof(*rom));
138 rom->data.type = SETUP_PCI;
139 rom->data.len = size - sizeof(struct setup_data);
140 rom->data.next = 0;
141 rom->pcilen = pci->romsize;
142 *__rom = rom;
144 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
145 PCI_VENDOR_ID, 1, &(rom->vendor));
147 if (status != EFI_SUCCESS) {
148 efi_printk(sys_table, "Failed to read rom->vendor\n");
149 goto free_struct;
152 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
153 PCI_DEVICE_ID, 1, &(rom->devid));
155 if (status != EFI_SUCCESS) {
156 efi_printk(sys_table, "Failed to read rom->devid\n");
157 goto free_struct;
160 status = efi_early->call(pci->get_location, pci, &(rom->segment),
161 &(rom->bus), &(rom->device), &(rom->function));
163 if (status != EFI_SUCCESS)
164 goto free_struct;
166 memcpy(rom->romdata, pci->romimage, pci->romsize);
167 return status;
169 free_struct:
170 efi_call_early(free_pool, rom);
171 return status;
174 static void
175 setup_efi_pci32(struct boot_params *params, void **pci_handle,
176 unsigned long size)
178 efi_pci_io_protocol_32 *pci = NULL;
179 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
180 u32 *handles = (u32 *)(unsigned long)pci_handle;
181 efi_status_t status;
182 unsigned long nr_pci;
183 struct setup_data *data;
184 int i;
186 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
188 while (data && data->next)
189 data = (struct setup_data *)(unsigned long)data->next;
191 nr_pci = size / sizeof(u32);
192 for (i = 0; i < nr_pci; i++) {
193 struct pci_setup_rom *rom = NULL;
194 u32 h = handles[i];
196 status = efi_call_early(handle_protocol, h,
197 &pci_proto, (void **)&pci);
199 if (status != EFI_SUCCESS)
200 continue;
202 if (!pci)
203 continue;
205 status = __setup_efi_pci32(pci, &rom);
206 if (status != EFI_SUCCESS)
207 continue;
209 if (data)
210 data->next = (unsigned long)rom;
211 else
212 params->hdr.setup_data = (unsigned long)rom;
214 data = (struct setup_data *)rom;
219 static efi_status_t
220 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
222 struct pci_setup_rom *rom;
223 efi_status_t status;
224 unsigned long size;
225 uint64_t attributes;
227 status = efi_early->call(pci->attributes, pci,
228 EfiPciIoAttributeOperationGet, 0,
229 &attributes);
230 if (status != EFI_SUCCESS)
231 return status;
233 if (!pci->romimage || !pci->romsize)
234 return EFI_INVALID_PARAMETER;
236 size = pci->romsize + sizeof(*rom);
238 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
239 if (status != EFI_SUCCESS) {
240 efi_printk(sys_table, "Failed to alloc mem for rom\n");
241 return status;
244 rom->data.type = SETUP_PCI;
245 rom->data.len = size - sizeof(struct setup_data);
246 rom->data.next = 0;
247 rom->pcilen = pci->romsize;
248 *__rom = rom;
250 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
251 PCI_VENDOR_ID, 1, &(rom->vendor));
253 if (status != EFI_SUCCESS) {
254 efi_printk(sys_table, "Failed to read rom->vendor\n");
255 goto free_struct;
258 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
259 PCI_DEVICE_ID, 1, &(rom->devid));
261 if (status != EFI_SUCCESS) {
262 efi_printk(sys_table, "Failed to read rom->devid\n");
263 goto free_struct;
266 status = efi_early->call(pci->get_location, pci, &(rom->segment),
267 &(rom->bus), &(rom->device), &(rom->function));
269 if (status != EFI_SUCCESS)
270 goto free_struct;
272 memcpy(rom->romdata, pci->romimage, pci->romsize);
273 return status;
275 free_struct:
276 efi_call_early(free_pool, rom);
277 return status;
281 static void
282 setup_efi_pci64(struct boot_params *params, void **pci_handle,
283 unsigned long size)
285 efi_pci_io_protocol_64 *pci = NULL;
286 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
287 u64 *handles = (u64 *)(unsigned long)pci_handle;
288 efi_status_t status;
289 unsigned long nr_pci;
290 struct setup_data *data;
291 int i;
293 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
295 while (data && data->next)
296 data = (struct setup_data *)(unsigned long)data->next;
298 nr_pci = size / sizeof(u64);
299 for (i = 0; i < nr_pci; i++) {
300 struct pci_setup_rom *rom = NULL;
301 u64 h = handles[i];
303 status = efi_call_early(handle_protocol, h,
304 &pci_proto, (void **)&pci);
306 if (status != EFI_SUCCESS)
307 continue;
309 if (!pci)
310 continue;
312 status = __setup_efi_pci64(pci, &rom);
313 if (status != EFI_SUCCESS)
314 continue;
316 if (data)
317 data->next = (unsigned long)rom;
318 else
319 params->hdr.setup_data = (unsigned long)rom;
321 data = (struct setup_data *)rom;
327 * There's no way to return an informative status from this function,
328 * because any analysis (and printing of error messages) needs to be
329 * done directly at the EFI function call-site.
331 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
332 * just didn't find any PCI devices, but there's no way to tell outside
333 * the context of the call.
335 static void setup_efi_pci(struct boot_params *params)
337 efi_status_t status;
338 void **pci_handle = NULL;
339 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
340 unsigned long size = 0;
342 status = efi_call_early(locate_handle,
343 EFI_LOCATE_BY_PROTOCOL,
344 &pci_proto, NULL, &size, pci_handle);
346 if (status == EFI_BUFFER_TOO_SMALL) {
347 status = efi_call_early(allocate_pool,
348 EFI_LOADER_DATA,
349 size, (void **)&pci_handle);
351 if (status != EFI_SUCCESS) {
352 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
353 return;
356 status = efi_call_early(locate_handle,
357 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
358 NULL, &size, pci_handle);
361 if (status != EFI_SUCCESS)
362 goto free_handle;
364 if (efi_early->is64)
365 setup_efi_pci64(params, pci_handle, size);
366 else
367 setup_efi_pci32(params, pci_handle, size);
369 free_handle:
370 efi_call_early(free_pool, pci_handle);
373 static void retrieve_apple_device_properties(struct boot_params *boot_params)
375 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
376 struct setup_data *data, *new;
377 efi_status_t status;
378 u32 size = 0;
379 void *p;
381 status = efi_call_early(locate_protocol, &guid, NULL, &p);
382 if (status != EFI_SUCCESS)
383 return;
385 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
386 efi_printk(sys_table, "Unsupported properties proto version\n");
387 return;
390 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
391 if (!size)
392 return;
394 do {
395 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
396 size + sizeof(struct setup_data), &new);
397 if (status != EFI_SUCCESS) {
398 efi_printk(sys_table,
399 "Failed to alloc mem for properties\n");
400 return;
403 status = efi_call_proto(apple_properties_protocol, get_all, p,
404 new->data, &size);
406 if (status == EFI_BUFFER_TOO_SMALL)
407 efi_call_early(free_pool, new);
408 } while (status == EFI_BUFFER_TOO_SMALL);
410 new->type = SETUP_APPLE_PROPERTIES;
411 new->len = size;
412 new->next = 0;
414 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
415 if (!data)
416 boot_params->hdr.setup_data = (unsigned long)new;
417 else {
418 while (data->next)
419 data = (struct setup_data *)(unsigned long)data->next;
420 data->next = (unsigned long)new;
424 static void setup_quirks(struct boot_params *boot_params)
426 efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
427 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
428 efi_table_attr(efi_system_table, fw_vendor, sys_table);
430 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
431 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
432 retrieve_apple_device_properties(boot_params);
436 static efi_status_t
437 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
439 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
440 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
441 unsigned long nr_ugas;
442 u32 *handles = (u32 *)uga_handle;;
443 efi_status_t status = EFI_INVALID_PARAMETER;
444 int i;
446 first_uga = NULL;
447 nr_ugas = size / sizeof(u32);
448 for (i = 0; i < nr_ugas; i++) {
449 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
450 u32 w, h, depth, refresh;
451 void *pciio;
452 u32 handle = handles[i];
454 status = efi_call_early(handle_protocol, handle,
455 &uga_proto, (void **)&uga);
456 if (status != EFI_SUCCESS)
457 continue;
459 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
461 status = efi_early->call((unsigned long)uga->get_mode, uga,
462 &w, &h, &depth, &refresh);
463 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
464 *width = w;
465 *height = h;
468 * Once we've found a UGA supporting PCIIO,
469 * don't bother looking any further.
471 if (pciio)
472 break;
474 first_uga = uga;
478 return status;
481 static efi_status_t
482 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
484 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
485 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
486 unsigned long nr_ugas;
487 u64 *handles = (u64 *)uga_handle;;
488 efi_status_t status = EFI_INVALID_PARAMETER;
489 int i;
491 first_uga = NULL;
492 nr_ugas = size / sizeof(u64);
493 for (i = 0; i < nr_ugas; i++) {
494 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
495 u32 w, h, depth, refresh;
496 void *pciio;
497 u64 handle = handles[i];
499 status = efi_call_early(handle_protocol, handle,
500 &uga_proto, (void **)&uga);
501 if (status != EFI_SUCCESS)
502 continue;
504 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
506 status = efi_early->call((unsigned long)uga->get_mode, uga,
507 &w, &h, &depth, &refresh);
508 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
509 *width = w;
510 *height = h;
513 * Once we've found a UGA supporting PCIIO,
514 * don't bother looking any further.
516 if (pciio)
517 break;
519 first_uga = uga;
523 return status;
527 * See if we have Universal Graphics Adapter (UGA) protocol
529 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
530 unsigned long size)
532 efi_status_t status;
533 u32 width, height;
534 void **uga_handle = NULL;
536 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
537 size, (void **)&uga_handle);
538 if (status != EFI_SUCCESS)
539 return status;
541 status = efi_call_early(locate_handle,
542 EFI_LOCATE_BY_PROTOCOL,
543 uga_proto, NULL, &size, uga_handle);
544 if (status != EFI_SUCCESS)
545 goto free_handle;
547 height = 0;
548 width = 0;
550 if (efi_early->is64)
551 status = setup_uga64(uga_handle, size, &width, &height);
552 else
553 status = setup_uga32(uga_handle, size, &width, &height);
555 if (!width && !height)
556 goto free_handle;
558 /* EFI framebuffer */
559 si->orig_video_isVGA = VIDEO_TYPE_EFI;
561 si->lfb_depth = 32;
562 si->lfb_width = width;
563 si->lfb_height = height;
565 si->red_size = 8;
566 si->red_pos = 16;
567 si->green_size = 8;
568 si->green_pos = 8;
569 si->blue_size = 8;
570 si->blue_pos = 0;
571 si->rsvd_size = 8;
572 si->rsvd_pos = 24;
574 free_handle:
575 efi_call_early(free_pool, uga_handle);
576 return status;
579 void setup_graphics(struct boot_params *boot_params)
581 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
582 struct screen_info *si;
583 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
584 efi_status_t status;
585 unsigned long size;
586 void **gop_handle = NULL;
587 void **uga_handle = NULL;
589 si = &boot_params->screen_info;
590 memset(si, 0, sizeof(*si));
592 size = 0;
593 status = efi_call_early(locate_handle,
594 EFI_LOCATE_BY_PROTOCOL,
595 &graphics_proto, NULL, &size, gop_handle);
596 if (status == EFI_BUFFER_TOO_SMALL)
597 status = efi_setup_gop(NULL, si, &graphics_proto, size);
599 if (status != EFI_SUCCESS) {
600 size = 0;
601 status = efi_call_early(locate_handle,
602 EFI_LOCATE_BY_PROTOCOL,
603 &uga_proto, NULL, &size, uga_handle);
604 if (status == EFI_BUFFER_TOO_SMALL)
605 setup_uga(si, &uga_proto, size);
610 * Because the x86 boot code expects to be passed a boot_params we
611 * need to create one ourselves (usually the bootloader would create
612 * one for us).
614 * The caller is responsible for filling out ->code32_start in the
615 * returned boot_params.
617 struct boot_params *make_boot_params(struct efi_config *c)
619 struct boot_params *boot_params;
620 struct apm_bios_info *bi;
621 struct setup_header *hdr;
622 efi_loaded_image_t *image;
623 void *options, *handle;
624 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
625 int options_size = 0;
626 efi_status_t status;
627 char *cmdline_ptr;
628 u16 *s2;
629 u8 *s1;
630 int i;
631 unsigned long ramdisk_addr;
632 unsigned long ramdisk_size;
634 efi_early = c;
635 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
636 handle = (void *)(unsigned long)efi_early->image_handle;
638 /* Check if we were booted by the EFI firmware */
639 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
640 return NULL;
642 if (efi_early->is64)
643 setup_boot_services64(efi_early);
644 else
645 setup_boot_services32(efi_early);
647 status = efi_call_early(handle_protocol, handle,
648 &proto, (void *)&image);
649 if (status != EFI_SUCCESS) {
650 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
651 return NULL;
654 status = efi_low_alloc(sys_table, 0x4000, 1,
655 (unsigned long *)&boot_params);
656 if (status != EFI_SUCCESS) {
657 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
658 return NULL;
661 memset(boot_params, 0x0, 0x4000);
663 hdr = &boot_params->hdr;
664 bi = &boot_params->apm_bios_info;
666 /* Copy the second sector to boot_params */
667 memcpy(&hdr->jump, image->image_base + 512, 512);
670 * Fill out some of the header fields ourselves because the
671 * EFI firmware loader doesn't load the first sector.
673 hdr->root_flags = 1;
674 hdr->vid_mode = 0xffff;
675 hdr->boot_flag = 0xAA55;
677 hdr->type_of_loader = 0x21;
679 /* Convert unicode cmdline to ascii */
680 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
681 if (!cmdline_ptr)
682 goto fail;
683 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
684 /* Fill in upper bits of command line address, NOP on 32 bit */
685 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
687 hdr->ramdisk_image = 0;
688 hdr->ramdisk_size = 0;
690 /* Clear APM BIOS info */
691 memset(bi, 0, sizeof(*bi));
693 status = efi_parse_options(cmdline_ptr);
694 if (status != EFI_SUCCESS)
695 goto fail2;
697 status = handle_cmdline_files(sys_table, image,
698 (char *)(unsigned long)hdr->cmd_line_ptr,
699 "initrd=", hdr->initrd_addr_max,
700 &ramdisk_addr, &ramdisk_size);
702 if (status != EFI_SUCCESS &&
703 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
704 efi_printk(sys_table, "Trying to load files to higher address\n");
705 status = handle_cmdline_files(sys_table, image,
706 (char *)(unsigned long)hdr->cmd_line_ptr,
707 "initrd=", -1UL,
708 &ramdisk_addr, &ramdisk_size);
711 if (status != EFI_SUCCESS)
712 goto fail2;
713 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
714 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
715 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
716 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
718 return boot_params;
719 fail2:
720 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
721 fail:
722 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
723 return NULL;
726 static void add_e820ext(struct boot_params *params,
727 struct setup_data *e820ext, u32 nr_entries)
729 struct setup_data *data;
730 efi_status_t status;
731 unsigned long size;
733 e820ext->type = SETUP_E820_EXT;
734 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
735 e820ext->next = 0;
737 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
739 while (data && data->next)
740 data = (struct setup_data *)(unsigned long)data->next;
742 if (data)
743 data->next = (unsigned long)e820ext;
744 else
745 params->hdr.setup_data = (unsigned long)e820ext;
748 static efi_status_t setup_e820(struct boot_params *params,
749 struct setup_data *e820ext, u32 e820ext_size)
751 struct boot_e820_entry *entry = params->e820_table;
752 struct efi_info *efi = &params->efi_info;
753 struct boot_e820_entry *prev = NULL;
754 u32 nr_entries;
755 u32 nr_desc;
756 int i;
758 nr_entries = 0;
759 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
761 for (i = 0; i < nr_desc; i++) {
762 efi_memory_desc_t *d;
763 unsigned int e820_type = 0;
764 unsigned long m = efi->efi_memmap;
766 #ifdef CONFIG_X86_64
767 m |= (u64)efi->efi_memmap_hi << 32;
768 #endif
770 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
771 switch (d->type) {
772 case EFI_RESERVED_TYPE:
773 case EFI_RUNTIME_SERVICES_CODE:
774 case EFI_RUNTIME_SERVICES_DATA:
775 case EFI_MEMORY_MAPPED_IO:
776 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
777 case EFI_PAL_CODE:
778 e820_type = E820_TYPE_RESERVED;
779 break;
781 case EFI_UNUSABLE_MEMORY:
782 e820_type = E820_TYPE_UNUSABLE;
783 break;
785 case EFI_ACPI_RECLAIM_MEMORY:
786 e820_type = E820_TYPE_ACPI;
787 break;
789 case EFI_LOADER_CODE:
790 case EFI_LOADER_DATA:
791 case EFI_BOOT_SERVICES_CODE:
792 case EFI_BOOT_SERVICES_DATA:
793 case EFI_CONVENTIONAL_MEMORY:
794 e820_type = E820_TYPE_RAM;
795 break;
797 case EFI_ACPI_MEMORY_NVS:
798 e820_type = E820_TYPE_NVS;
799 break;
801 case EFI_PERSISTENT_MEMORY:
802 e820_type = E820_TYPE_PMEM;
803 break;
805 default:
806 continue;
809 /* Merge adjacent mappings */
810 if (prev && prev->type == e820_type &&
811 (prev->addr + prev->size) == d->phys_addr) {
812 prev->size += d->num_pages << 12;
813 continue;
816 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
817 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
818 sizeof(struct setup_data);
820 if (!e820ext || e820ext_size < need)
821 return EFI_BUFFER_TOO_SMALL;
823 /* boot_params map full, switch to e820 extended */
824 entry = (struct boot_e820_entry *)e820ext->data;
827 entry->addr = d->phys_addr;
828 entry->size = d->num_pages << PAGE_SHIFT;
829 entry->type = e820_type;
830 prev = entry++;
831 nr_entries++;
834 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
835 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
837 add_e820ext(params, e820ext, nr_e820ext);
838 nr_entries -= nr_e820ext;
841 params->e820_entries = (u8)nr_entries;
843 return EFI_SUCCESS;
846 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
847 u32 *e820ext_size)
849 efi_status_t status;
850 unsigned long size;
852 size = sizeof(struct setup_data) +
853 sizeof(struct e820_entry) * nr_desc;
855 if (*e820ext) {
856 efi_call_early(free_pool, *e820ext);
857 *e820ext = NULL;
858 *e820ext_size = 0;
861 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
862 size, (void **)e820ext);
863 if (status == EFI_SUCCESS)
864 *e820ext_size = size;
866 return status;
869 struct exit_boot_struct {
870 struct boot_params *boot_params;
871 struct efi_info *efi;
872 struct setup_data *e820ext;
873 __u32 e820ext_size;
874 bool is64;
877 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
878 struct efi_boot_memmap *map,
879 void *priv)
881 static bool first = true;
882 const char *signature;
883 __u32 nr_desc;
884 efi_status_t status;
885 struct exit_boot_struct *p = priv;
887 if (first) {
888 nr_desc = *map->buff_size / *map->desc_size;
889 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
890 u32 nr_e820ext = nr_desc -
891 ARRAY_SIZE(p->boot_params->e820_table);
893 status = alloc_e820ext(nr_e820ext, &p->e820ext,
894 &p->e820ext_size);
895 if (status != EFI_SUCCESS)
896 return status;
898 first = false;
901 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
902 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
904 p->efi->efi_systab = (unsigned long)sys_table_arg;
905 p->efi->efi_memdesc_size = *map->desc_size;
906 p->efi->efi_memdesc_version = *map->desc_ver;
907 p->efi->efi_memmap = (unsigned long)*map->map;
908 p->efi->efi_memmap_size = *map->map_size;
910 #ifdef CONFIG_X86_64
911 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
912 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
913 #endif
915 return EFI_SUCCESS;
918 static efi_status_t exit_boot(struct boot_params *boot_params,
919 void *handle, bool is64)
921 unsigned long map_sz, key, desc_size, buff_size;
922 efi_memory_desc_t *mem_map;
923 struct setup_data *e820ext;
924 __u32 e820ext_size;
925 efi_status_t status;
926 __u32 desc_version;
927 struct efi_boot_memmap map;
928 struct exit_boot_struct priv;
930 map.map = &mem_map;
931 map.map_size = &map_sz;
932 map.desc_size = &desc_size;
933 map.desc_ver = &desc_version;
934 map.key_ptr = &key;
935 map.buff_size = &buff_size;
936 priv.boot_params = boot_params;
937 priv.efi = &boot_params->efi_info;
938 priv.e820ext = NULL;
939 priv.e820ext_size = 0;
940 priv.is64 = is64;
942 /* Might as well exit boot services now */
943 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
944 exit_boot_func);
945 if (status != EFI_SUCCESS)
946 return status;
948 e820ext = priv.e820ext;
949 e820ext_size = priv.e820ext_size;
950 /* Historic? */
951 boot_params->alt_mem_k = 32 * 1024;
953 status = setup_e820(boot_params, e820ext, e820ext_size);
954 if (status != EFI_SUCCESS)
955 return status;
957 return EFI_SUCCESS;
961 * On success we return a pointer to a boot_params structure, and NULL
962 * on failure.
964 struct boot_params *efi_main(struct efi_config *c,
965 struct boot_params *boot_params)
967 struct desc_ptr *gdt = NULL;
968 efi_loaded_image_t *image;
969 struct setup_header *hdr = &boot_params->hdr;
970 efi_status_t status;
971 struct desc_struct *desc;
972 void *handle;
973 efi_system_table_t *_table;
974 bool is64;
976 efi_early = c;
978 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
979 handle = (void *)(unsigned long)efi_early->image_handle;
980 is64 = efi_early->is64;
982 sys_table = _table;
984 /* Check if we were booted by the EFI firmware */
985 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
986 goto fail;
988 if (is64)
989 setup_boot_services64(efi_early);
990 else
991 setup_boot_services32(efi_early);
994 * If the boot loader gave us a value for secure_boot then we use that,
995 * otherwise we ask the BIOS.
997 if (boot_params->secure_boot == efi_secureboot_mode_unset)
998 boot_params->secure_boot = efi_get_secureboot(sys_table);
1000 /* Ask the firmware to clear memory on unclean shutdown */
1001 efi_enable_reset_attack_mitigation(sys_table);
1002 efi_retrieve_tpm2_eventlog(sys_table);
1004 setup_graphics(boot_params);
1006 setup_efi_pci(boot_params);
1008 setup_quirks(boot_params);
1010 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1011 sizeof(*gdt), (void **)&gdt);
1012 if (status != EFI_SUCCESS) {
1013 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1014 goto fail;
1017 gdt->size = 0x800;
1018 status = efi_low_alloc(sys_table, gdt->size, 8,
1019 (unsigned long *)&gdt->address);
1020 if (status != EFI_SUCCESS) {
1021 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1022 goto fail;
1026 * If the kernel isn't already loaded at the preferred load
1027 * address, relocate it.
1029 if (hdr->pref_address != hdr->code32_start) {
1030 unsigned long bzimage_addr = hdr->code32_start;
1031 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1032 hdr->init_size, hdr->init_size,
1033 hdr->pref_address,
1034 hdr->kernel_alignment);
1035 if (status != EFI_SUCCESS) {
1036 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1037 goto fail;
1040 hdr->pref_address = hdr->code32_start;
1041 hdr->code32_start = bzimage_addr;
1044 status = exit_boot(boot_params, handle, is64);
1045 if (status != EFI_SUCCESS) {
1046 efi_printk(sys_table, "exit_boot() failed!\n");
1047 goto fail;
1050 memset((char *)gdt->address, 0x0, gdt->size);
1051 desc = (struct desc_struct *)gdt->address;
1053 /* The first GDT is a dummy. */
1054 desc++;
1056 if (IS_ENABLED(CONFIG_X86_64)) {
1057 /* __KERNEL32_CS */
1058 desc->limit0 = 0xffff;
1059 desc->base0 = 0x0000;
1060 desc->base1 = 0x0000;
1061 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1062 desc->s = DESC_TYPE_CODE_DATA;
1063 desc->dpl = 0;
1064 desc->p = 1;
1065 desc->limit1 = 0xf;
1066 desc->avl = 0;
1067 desc->l = 0;
1068 desc->d = SEG_OP_SIZE_32BIT;
1069 desc->g = SEG_GRANULARITY_4KB;
1070 desc->base2 = 0x00;
1071 desc++;
1072 } else {
1073 /* Second entry is unused on 32-bit */
1074 desc++;
1077 /* __KERNEL_CS */
1078 desc->limit0 = 0xffff;
1079 desc->base0 = 0x0000;
1080 desc->base1 = 0x0000;
1081 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1082 desc->s = DESC_TYPE_CODE_DATA;
1083 desc->dpl = 0;
1084 desc->p = 1;
1085 desc->limit1 = 0xf;
1086 desc->avl = 0;
1087 if (IS_ENABLED(CONFIG_X86_64)) {
1088 desc->l = 1;
1089 desc->d = 0;
1090 } else {
1091 desc->l = 0;
1092 desc->d = SEG_OP_SIZE_32BIT;
1094 desc->g = SEG_GRANULARITY_4KB;
1095 desc->base2 = 0x00;
1096 desc++;
1098 /* __KERNEL_DS */
1099 desc->limit0 = 0xffff;
1100 desc->base0 = 0x0000;
1101 desc->base1 = 0x0000;
1102 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1103 desc->s = DESC_TYPE_CODE_DATA;
1104 desc->dpl = 0;
1105 desc->p = 1;
1106 desc->limit1 = 0xf;
1107 desc->avl = 0;
1108 desc->l = 0;
1109 desc->d = SEG_OP_SIZE_32BIT;
1110 desc->g = SEG_GRANULARITY_4KB;
1111 desc->base2 = 0x00;
1112 desc++;
1114 if (IS_ENABLED(CONFIG_X86_64)) {
1115 /* Task segment value */
1116 desc->limit0 = 0x0000;
1117 desc->base0 = 0x0000;
1118 desc->base1 = 0x0000;
1119 desc->type = SEG_TYPE_TSS;
1120 desc->s = 0;
1121 desc->dpl = 0;
1122 desc->p = 1;
1123 desc->limit1 = 0x0;
1124 desc->avl = 0;
1125 desc->l = 0;
1126 desc->d = 0;
1127 desc->g = SEG_GRANULARITY_4KB;
1128 desc->base2 = 0x00;
1129 desc++;
1132 asm volatile("cli");
1133 asm volatile ("lgdt %0" : : "m" (*gdt));
1135 return boot_params;
1136 fail:
1137 efi_printk(sys_table, "efi_main() failed!\n");
1138 return NULL;