efi/libstub: Simplify efi_high_alloc() and rename to efi_allocate_pages()
[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 static 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 setup_header *hdr;
362         efi_loaded_image_t *image;
363         efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
364         int options_size = 0;
365         efi_status_t status;
366         char *cmdline_ptr;
367         unsigned long ramdisk_addr;
368         unsigned long ramdisk_size;
369
370         sys_table = sys_table_arg;
371
372         /* Check if we were booted by the EFI firmware */
373         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
374                 return EFI_INVALID_PARAMETER;
375
376         status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image);
377         if (status != EFI_SUCCESS) {
378                 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
379                 return status;
380         }
381
382         status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params);
383         if (status != EFI_SUCCESS) {
384                 efi_printk("Failed to allocate lowmem for boot params\n");
385                 return status;
386         }
387
388         memset(boot_params, 0x0, 0x4000);
389
390         hdr = &boot_params->hdr;
391
392         /* Copy the second sector to boot_params */
393         memcpy(&hdr->jump, image->image_base + 512, 512);
394
395         /*
396          * Fill out some of the header fields ourselves because the
397          * EFI firmware loader doesn't load the first sector.
398          */
399         hdr->root_flags = 1;
400         hdr->vid_mode   = 0xffff;
401         hdr->boot_flag  = 0xAA55;
402
403         hdr->type_of_loader = 0x21;
404
405         /* Convert unicode cmdline to ascii */
406         cmdline_ptr = efi_convert_cmdline(image, &options_size);
407         if (!cmdline_ptr)
408                 goto fail;
409
410         hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
411         /* Fill in upper bits of command line address, NOP on 32 bit  */
412         boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
413
414         hdr->ramdisk_image = 0;
415         hdr->ramdisk_size = 0;
416
417         status = efi_parse_options(cmdline_ptr);
418         if (status != EFI_SUCCESS)
419                 goto fail2;
420
421         status = handle_cmdline_files(image,
422                                       (char *)(unsigned long)hdr->cmd_line_ptr,
423                                       "initrd=", hdr->initrd_addr_max,
424                                       &ramdisk_addr, &ramdisk_size);
425
426         if (status != EFI_SUCCESS &&
427             hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
428                 efi_printk("Trying to load files to higher address\n");
429                 status = handle_cmdline_files(image,
430                                       (char *)(unsigned long)hdr->cmd_line_ptr,
431                                       "initrd=", -1UL,
432                                       &ramdisk_addr, &ramdisk_size);
433         }
434
435         if (status != EFI_SUCCESS)
436                 goto fail2;
437         hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
438         hdr->ramdisk_size  = ramdisk_size & 0xffffffff;
439         boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
440         boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
441
442         efi_stub_entry(handle, sys_table, boot_params);
443         /* not reached */
444
445 fail2:
446         efi_free(options_size, hdr->cmd_line_ptr);
447 fail:
448         efi_free(0x4000, (unsigned long)boot_params);
449
450         return status;
451 }
452
453 static void add_e820ext(struct boot_params *params,
454                         struct setup_data *e820ext, u32 nr_entries)
455 {
456         struct setup_data *data;
457
458         e820ext->type = SETUP_E820_EXT;
459         e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
460         e820ext->next = 0;
461
462         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
463
464         while (data && data->next)
465                 data = (struct setup_data *)(unsigned long)data->next;
466
467         if (data)
468                 data->next = (unsigned long)e820ext;
469         else
470                 params->hdr.setup_data = (unsigned long)e820ext;
471 }
472
473 static efi_status_t
474 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
475 {
476         struct boot_e820_entry *entry = params->e820_table;
477         struct efi_info *efi = &params->efi_info;
478         struct boot_e820_entry *prev = NULL;
479         u32 nr_entries;
480         u32 nr_desc;
481         int i;
482
483         nr_entries = 0;
484         nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
485
486         for (i = 0; i < nr_desc; i++) {
487                 efi_memory_desc_t *d;
488                 unsigned int e820_type = 0;
489                 unsigned long m = efi->efi_memmap;
490
491 #ifdef CONFIG_X86_64
492                 m |= (u64)efi->efi_memmap_hi << 32;
493 #endif
494
495                 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
496                 switch (d->type) {
497                 case EFI_RESERVED_TYPE:
498                 case EFI_RUNTIME_SERVICES_CODE:
499                 case EFI_RUNTIME_SERVICES_DATA:
500                 case EFI_MEMORY_MAPPED_IO:
501                 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
502                 case EFI_PAL_CODE:
503                         e820_type = E820_TYPE_RESERVED;
504                         break;
505
506                 case EFI_UNUSABLE_MEMORY:
507                         e820_type = E820_TYPE_UNUSABLE;
508                         break;
509
510                 case EFI_ACPI_RECLAIM_MEMORY:
511                         e820_type = E820_TYPE_ACPI;
512                         break;
513
514                 case EFI_LOADER_CODE:
515                 case EFI_LOADER_DATA:
516                 case EFI_BOOT_SERVICES_CODE:
517                 case EFI_BOOT_SERVICES_DATA:
518                 case EFI_CONVENTIONAL_MEMORY:
519                         if (efi_soft_reserve_enabled() &&
520                             (d->attribute & EFI_MEMORY_SP))
521                                 e820_type = E820_TYPE_SOFT_RESERVED;
522                         else
523                                 e820_type = E820_TYPE_RAM;
524                         break;
525
526                 case EFI_ACPI_MEMORY_NVS:
527                         e820_type = E820_TYPE_NVS;
528                         break;
529
530                 case EFI_PERSISTENT_MEMORY:
531                         e820_type = E820_TYPE_PMEM;
532                         break;
533
534                 default:
535                         continue;
536                 }
537
538                 /* Merge adjacent mappings */
539                 if (prev && prev->type == e820_type &&
540                     (prev->addr + prev->size) == d->phys_addr) {
541                         prev->size += d->num_pages << 12;
542                         continue;
543                 }
544
545                 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
546                         u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
547                                    sizeof(struct setup_data);
548
549                         if (!e820ext || e820ext_size < need)
550                                 return EFI_BUFFER_TOO_SMALL;
551
552                         /* boot_params map full, switch to e820 extended */
553                         entry = (struct boot_e820_entry *)e820ext->data;
554                 }
555
556                 entry->addr = d->phys_addr;
557                 entry->size = d->num_pages << PAGE_SHIFT;
558                 entry->type = e820_type;
559                 prev = entry++;
560                 nr_entries++;
561         }
562
563         if (nr_entries > ARRAY_SIZE(params->e820_table)) {
564                 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
565
566                 add_e820ext(params, e820ext, nr_e820ext);
567                 nr_entries -= nr_e820ext;
568         }
569
570         params->e820_entries = (u8)nr_entries;
571
572         return EFI_SUCCESS;
573 }
574
575 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
576                                   u32 *e820ext_size)
577 {
578         efi_status_t status;
579         unsigned long size;
580
581         size = sizeof(struct setup_data) +
582                 sizeof(struct e820_entry) * nr_desc;
583
584         if (*e820ext) {
585                 efi_bs_call(free_pool, *e820ext);
586                 *e820ext = NULL;
587                 *e820ext_size = 0;
588         }
589
590         status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
591                              (void **)e820ext);
592         if (status == EFI_SUCCESS)
593                 *e820ext_size = size;
594
595         return status;
596 }
597
598 static efi_status_t allocate_e820(struct boot_params *params,
599                                   struct setup_data **e820ext,
600                                   u32 *e820ext_size)
601 {
602         unsigned long map_size, desc_size, buff_size;
603         struct efi_boot_memmap boot_map;
604         efi_memory_desc_t *map;
605         efi_status_t status;
606         __u32 nr_desc;
607
608         boot_map.map            = &map;
609         boot_map.map_size       = &map_size;
610         boot_map.desc_size      = &desc_size;
611         boot_map.desc_ver       = NULL;
612         boot_map.key_ptr        = NULL;
613         boot_map.buff_size      = &buff_size;
614
615         status = efi_get_memory_map(&boot_map);
616         if (status != EFI_SUCCESS)
617                 return status;
618
619         nr_desc = buff_size / desc_size;
620
621         if (nr_desc > ARRAY_SIZE(params->e820_table)) {
622                 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
623
624                 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
625                 if (status != EFI_SUCCESS)
626                         return status;
627         }
628
629         return EFI_SUCCESS;
630 }
631
632 struct exit_boot_struct {
633         struct boot_params      *boot_params;
634         struct efi_info         *efi;
635 };
636
637 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
638                                    void *priv)
639 {
640         const char *signature;
641         struct exit_boot_struct *p = priv;
642
643         signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
644                                    : EFI32_LOADER_SIGNATURE;
645         memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
646
647         p->efi->efi_systab              = (unsigned long)efi_system_table();
648         p->efi->efi_memdesc_size        = *map->desc_size;
649         p->efi->efi_memdesc_version     = *map->desc_ver;
650         p->efi->efi_memmap              = (unsigned long)*map->map;
651         p->efi->efi_memmap_size         = *map->map_size;
652
653 #ifdef CONFIG_X86_64
654         p->efi->efi_systab_hi           = (unsigned long)efi_system_table() >> 32;
655         p->efi->efi_memmap_hi           = (unsigned long)*map->map >> 32;
656 #endif
657
658         return EFI_SUCCESS;
659 }
660
661 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
662 {
663         unsigned long map_sz, key, desc_size, buff_size;
664         efi_memory_desc_t *mem_map;
665         struct setup_data *e820ext = NULL;
666         __u32 e820ext_size = 0;
667         efi_status_t status;
668         __u32 desc_version;
669         struct efi_boot_memmap map;
670         struct exit_boot_struct priv;
671
672         map.map                 = &mem_map;
673         map.map_size            = &map_sz;
674         map.desc_size           = &desc_size;
675         map.desc_ver            = &desc_version;
676         map.key_ptr             = &key;
677         map.buff_size           = &buff_size;
678         priv.boot_params        = boot_params;
679         priv.efi                = &boot_params->efi_info;
680
681         status = allocate_e820(boot_params, &e820ext, &e820ext_size);
682         if (status != EFI_SUCCESS)
683                 return status;
684
685         /* Might as well exit boot services now */
686         status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
687         if (status != EFI_SUCCESS)
688                 return status;
689
690         /* Historic? */
691         boot_params->alt_mem_k  = 32 * 1024;
692
693         status = setup_e820(boot_params, e820ext, e820ext_size);
694         if (status != EFI_SUCCESS)
695                 return status;
696
697         return EFI_SUCCESS;
698 }
699
700 /*
701  * On success we return a pointer to a boot_params structure, and NULL
702  * on failure.
703  */
704 struct boot_params *efi_main(efi_handle_t handle,
705                              efi_system_table_t *sys_table_arg,
706                              struct boot_params *boot_params)
707 {
708         unsigned long bzimage_addr = (unsigned long)startup_32;
709         struct setup_header *hdr = &boot_params->hdr;
710         efi_status_t status;
711         unsigned long cmdline_paddr;
712
713         sys_table = sys_table_arg;
714
715         /* Check if we were booted by the EFI firmware */
716         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
717                 goto fail;
718
719         /*
720          * If the kernel isn't already loaded at the preferred load
721          * address, relocate it.
722          */
723         if (bzimage_addr != hdr->pref_address) {
724                 status = efi_relocate_kernel(&bzimage_addr,
725                                              hdr->init_size, hdr->init_size,
726                                              hdr->pref_address,
727                                              hdr->kernel_alignment,
728                                              LOAD_PHYSICAL_ADDR);
729                 if (status != EFI_SUCCESS) {
730                         efi_printk("efi_relocate_kernel() failed!\n");
731                         goto fail;
732                 }
733         }
734         hdr->code32_start = (u32)bzimage_addr;
735
736         /*
737          * make_boot_params() may have been called before efi_main(), in which
738          * case this is the second time we parse the cmdline. This is ok,
739          * parsing the cmdline multiple times does not have side-effects.
740          */
741         cmdline_paddr = ((u64)hdr->cmd_line_ptr |
742                          ((u64)boot_params->ext_cmd_line_ptr << 32));
743         efi_parse_options((char *)cmdline_paddr);
744
745         /*
746          * If the boot loader gave us a value for secure_boot then we use that,
747          * otherwise we ask the BIOS.
748          */
749         if (boot_params->secure_boot == efi_secureboot_mode_unset)
750                 boot_params->secure_boot = efi_get_secureboot();
751
752         /* Ask the firmware to clear memory on unclean shutdown */
753         efi_enable_reset_attack_mitigation();
754
755         efi_random_get_seed();
756
757         efi_retrieve_tpm2_eventlog();
758
759         setup_graphics(boot_params);
760
761         setup_efi_pci(boot_params);
762
763         setup_quirks(boot_params);
764
765         status = exit_boot(boot_params, handle);
766         if (status != EFI_SUCCESS) {
767                 efi_printk("exit_boot() failed!\n");
768                 goto fail;
769         }
770
771         return boot_params;
772 fail:
773         efi_printk("efi_main() failed!\n");
774
775         for (;;)
776                 asm("hlt");
777 }