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