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