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