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