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