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