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