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 static efi_system_table_t *sys_table;
21 extern const bool efi_is64;
22 
23 __pure efi_system_table_t *efi_system_table(void)
24 {
25 	return sys_table;
26 }
27 
28 __attribute_const__ bool efi_is_64bit(void)
29 {
30 	if (IS_ENABLED(CONFIG_EFI_MIXED))
31 		return efi_is64;
32 	return IS_ENABLED(CONFIG_X86_64);
33 }
34 
35 static efi_status_t
36 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
37 {
38 	struct pci_setup_rom *rom = NULL;
39 	efi_status_t status;
40 	unsigned long size;
41 	uint64_t romsize;
42 	void *romimage;
43 
44 	/*
45 	 * Some firmware images contain EFI function pointers at the place where
46 	 * the romimage and romsize fields are supposed to be. Typically the EFI
47 	 * code is mapped at high addresses, translating to an unrealistically
48 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
49 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
50 	 */
51 	romimage = efi_table_attr(pci, romimage);
52 	romsize = efi_table_attr(pci, romsize);
53 	if (!romimage || !romsize || romsize > SZ_16M)
54 		return EFI_INVALID_PARAMETER;
55 
56 	size = romsize + sizeof(*rom);
57 
58 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
59 			     (void **)&rom);
60 	if (status != EFI_SUCCESS) {
61 		efi_printk("Failed to allocate memory for 'rom'\n");
62 		return status;
63 	}
64 
65 	memset(rom, 0, sizeof(*rom));
66 
67 	rom->data.type	= SETUP_PCI;
68 	rom->data.len	= size - sizeof(struct setup_data);
69 	rom->data.next	= 0;
70 	rom->pcilen	= pci->romsize;
71 	*__rom = rom;
72 
73 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
74 				PCI_VENDOR_ID, 1, &rom->vendor);
75 
76 	if (status != EFI_SUCCESS) {
77 		efi_printk("Failed to read rom->vendor\n");
78 		goto free_struct;
79 	}
80 
81 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
82 				PCI_DEVICE_ID, 1, &rom->devid);
83 
84 	if (status != EFI_SUCCESS) {
85 		efi_printk("Failed to read rom->devid\n");
86 		goto free_struct;
87 	}
88 
89 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
90 				&rom->device, &rom->function);
91 
92 	if (status != EFI_SUCCESS)
93 		goto free_struct;
94 
95 	memcpy(rom->romdata, romimage, romsize);
96 	return status;
97 
98 free_struct:
99 	efi_bs_call(free_pool, rom);
100 	return status;
101 }
102 
103 /*
104  * There's no way to return an informative status from this function,
105  * because any analysis (and printing of error messages) needs to be
106  * done directly at the EFI function call-site.
107  *
108  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
109  * just didn't find any PCI devices, but there's no way to tell outside
110  * the context of the call.
111  */
112 static void setup_efi_pci(struct boot_params *params)
113 {
114 	efi_status_t status;
115 	void **pci_handle = NULL;
116 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
117 	unsigned long size = 0;
118 	struct setup_data *data;
119 	efi_handle_t h;
120 	int i;
121 
122 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
123 			     &pci_proto, NULL, &size, pci_handle);
124 
125 	if (status == EFI_BUFFER_TOO_SMALL) {
126 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
127 				     (void **)&pci_handle);
128 
129 		if (status != EFI_SUCCESS) {
130 			efi_printk("Failed to allocate memory for 'pci_handle'\n");
131 			return;
132 		}
133 
134 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
135 				     &pci_proto, NULL, &size, pci_handle);
136 	}
137 
138 	if (status != EFI_SUCCESS)
139 		goto free_handle;
140 
141 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
142 
143 	while (data && data->next)
144 		data = (struct setup_data *)(unsigned long)data->next;
145 
146 	for_each_efi_handle(h, pci_handle, size, i) {
147 		efi_pci_io_protocol_t *pci = NULL;
148 		struct pci_setup_rom *rom;
149 
150 		status = efi_bs_call(handle_protocol, h, &pci_proto,
151 				     (void **)&pci);
152 		if (status != EFI_SUCCESS || !pci)
153 			continue;
154 
155 		status = preserve_pci_rom_image(pci, &rom);
156 		if (status != EFI_SUCCESS)
157 			continue;
158 
159 		if (data)
160 			data->next = (unsigned long)rom;
161 		else
162 			params->hdr.setup_data = (unsigned long)rom;
163 
164 		data = (struct setup_data *)rom;
165 	}
166 
167 free_handle:
168 	efi_bs_call(free_pool, pci_handle);
169 }
170 
171 static void retrieve_apple_device_properties(struct boot_params *boot_params)
172 {
173 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
174 	struct setup_data *data, *new;
175 	efi_status_t status;
176 	u32 size = 0;
177 	apple_properties_protocol_t *p;
178 
179 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
180 	if (status != EFI_SUCCESS)
181 		return;
182 
183 	if (efi_table_attr(p, version) != 0x10000) {
184 		efi_printk("Unsupported properties proto version\n");
185 		return;
186 	}
187 
188 	efi_call_proto(p, get_all, NULL, &size);
189 	if (!size)
190 		return;
191 
192 	do {
193 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
194 				     size + sizeof(struct setup_data),
195 				     (void **)&new);
196 		if (status != EFI_SUCCESS) {
197 			efi_printk("Failed to allocate memory for 'properties'\n");
198 			return;
199 		}
200 
201 		status = efi_call_proto(p, get_all, new->data, &size);
202 
203 		if (status == EFI_BUFFER_TOO_SMALL)
204 			efi_bs_call(free_pool, new);
205 	} while (status == EFI_BUFFER_TOO_SMALL);
206 
207 	new->type = SETUP_APPLE_PROPERTIES;
208 	new->len  = size;
209 	new->next = 0;
210 
211 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
212 	if (!data) {
213 		boot_params->hdr.setup_data = (unsigned long)new;
214 	} else {
215 		while (data->next)
216 			data = (struct setup_data *)(unsigned long)data->next;
217 		data->next = (unsigned long)new;
218 	}
219 }
220 
221 static const efi_char16_t apple[] = L"Apple";
222 
223 static void setup_quirks(struct boot_params *boot_params)
224 {
225 	efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
226 		efi_table_attr(efi_system_table(), fw_vendor);
227 
228 	if (!memcmp(fw_vendor, apple, sizeof(apple))) {
229 		if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
230 			retrieve_apple_device_properties(boot_params);
231 	}
232 }
233 
234 /*
235  * See if we have Universal Graphics Adapter (UGA) protocol
236  */
237 static efi_status_t
238 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
239 {
240 	efi_status_t status;
241 	u32 width, height;
242 	void **uga_handle = NULL;
243 	efi_uga_draw_protocol_t *uga = NULL, *first_uga;
244 	efi_handle_t handle;
245 	int i;
246 
247 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
248 			     (void **)&uga_handle);
249 	if (status != EFI_SUCCESS)
250 		return status;
251 
252 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
253 			     uga_proto, NULL, &size, uga_handle);
254 	if (status != EFI_SUCCESS)
255 		goto free_handle;
256 
257 	height = 0;
258 	width = 0;
259 
260 	first_uga = NULL;
261 	for_each_efi_handle(handle, uga_handle, size, i) {
262 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
263 		u32 w, h, depth, refresh;
264 		void *pciio;
265 
266 		status = efi_bs_call(handle_protocol, handle, uga_proto,
267 				     (void **)&uga);
268 		if (status != EFI_SUCCESS)
269 			continue;
270 
271 		pciio = NULL;
272 		efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
273 
274 		status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
275 		if (status == EFI_SUCCESS && (!first_uga || pciio)) {
276 			width = w;
277 			height = h;
278 
279 			/*
280 			 * Once we've found a UGA supporting PCIIO,
281 			 * don't bother looking any further.
282 			 */
283 			if (pciio)
284 				break;
285 
286 			first_uga = uga;
287 		}
288 	}
289 
290 	if (!width && !height)
291 		goto free_handle;
292 
293 	/* EFI framebuffer */
294 	si->orig_video_isVGA	= VIDEO_TYPE_EFI;
295 
296 	si->lfb_depth		= 32;
297 	si->lfb_width		= width;
298 	si->lfb_height		= height;
299 
300 	si->red_size		= 8;
301 	si->red_pos		= 16;
302 	si->green_size		= 8;
303 	si->green_pos		= 8;
304 	si->blue_size		= 8;
305 	si->blue_pos		= 0;
306 	si->rsvd_size		= 8;
307 	si->rsvd_pos		= 24;
308 
309 free_handle:
310 	efi_bs_call(free_pool, uga_handle);
311 
312 	return status;
313 }
314 
315 static void setup_graphics(struct boot_params *boot_params)
316 {
317 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
318 	struct screen_info *si;
319 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
320 	efi_status_t status;
321 	unsigned long size;
322 	void **gop_handle = NULL;
323 	void **uga_handle = NULL;
324 
325 	si = &boot_params->screen_info;
326 	memset(si, 0, sizeof(*si));
327 
328 	size = 0;
329 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
330 			     &graphics_proto, NULL, &size, gop_handle);
331 	if (status == EFI_BUFFER_TOO_SMALL)
332 		status = efi_setup_gop(si, &graphics_proto, size);
333 
334 	if (status != EFI_SUCCESS) {
335 		size = 0;
336 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
337 				     &uga_proto, NULL, &size, uga_handle);
338 		if (status == EFI_BUFFER_TOO_SMALL)
339 			setup_uga(si, &uga_proto, size);
340 	}
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 	efi_loaded_image_t *image;
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 	bool above4g;
367 
368 	sys_table = sys_table_arg;
369 
370 	/* Check if we were booted by the EFI firmware */
371 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
372 		return EFI_INVALID_PARAMETER;
373 
374 	status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image);
375 	if (status != EFI_SUCCESS) {
376 		efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
377 		return status;
378 	}
379 
380 	hdr = &((struct boot_params *)efi_table_attr(image, image_base))->hdr;
381 	above4g = hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G;
382 
383 	status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params,
384 				    above4g ? ULONG_MAX : UINT_MAX);
385 	if (status != EFI_SUCCESS) {
386 		efi_printk("Failed to allocate lowmem for boot params\n");
387 		return status;
388 	}
389 
390 	memset(boot_params, 0x0, 0x4000);
391 
392 	hdr = &boot_params->hdr;
393 
394 	/* Copy the second sector to boot_params */
395 	memcpy(&hdr->jump, efi_table_attr(image, image_base) + 512, 512);
396 
397 	/*
398 	 * Fill out some of the header fields ourselves because the
399 	 * EFI firmware loader doesn't load the first sector.
400 	 */
401 	hdr->root_flags	= 1;
402 	hdr->vid_mode	= 0xffff;
403 	hdr->boot_flag	= 0xAA55;
404 
405 	hdr->type_of_loader = 0x21;
406 
407 	/* Convert unicode cmdline to ascii */
408 	cmdline_ptr = efi_convert_cmdline(image, &options_size,
409 					  above4g ? ULONG_MAX : UINT_MAX);
410 	if (!cmdline_ptr)
411 		goto fail;
412 
413 	hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
414 	/* Fill in upper bits of command line address, NOP on 32 bit  */
415 	boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
416 
417 	hdr->ramdisk_image = 0;
418 	hdr->ramdisk_size = 0;
419 
420 	status = efi_parse_options(cmdline_ptr);
421 	if (status != EFI_SUCCESS)
422 		goto fail2;
423 
424 	if (!noinitrd()) {
425 		status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
426 					 hdr->initrd_addr_max,
427 					 above4g ? ULONG_MAX
428 						 : hdr->initrd_addr_max);
429 		if (status != EFI_SUCCESS)
430 			goto fail2;
431 		hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
432 		hdr->ramdisk_size  = ramdisk_size & 0xffffffff;
433 		boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
434 		boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
435 	}
436 
437 	efi_stub_entry(handle, sys_table, boot_params);
438 	/* not reached */
439 
440 fail2:
441 	efi_free(options_size, (unsigned long)cmdline_ptr);
442 fail:
443 	efi_free(0x4000, (unsigned long)boot_params);
444 
445 	return 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 a pointer to a boot_params structure, and NULL
697  * on failure.
698  */
699 struct boot_params *efi_main(efi_handle_t handle,
700 			     efi_system_table_t *sys_table_arg,
701 			     struct boot_params *boot_params)
702 {
703 	unsigned long bzimage_addr = (unsigned long)startup_32;
704 	struct setup_header *hdr = &boot_params->hdr;
705 	efi_status_t status;
706 	unsigned long cmdline_paddr;
707 
708 	sys_table = sys_table_arg;
709 
710 	/* Check if we were booted by the EFI firmware */
711 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
712 		goto fail;
713 
714 	/*
715 	 * If the kernel isn't already loaded at the preferred load
716 	 * address, relocate it.
717 	 */
718 	if (bzimage_addr != hdr->pref_address) {
719 		status = efi_relocate_kernel(&bzimage_addr,
720 					     hdr->init_size, hdr->init_size,
721 					     hdr->pref_address,
722 					     hdr->kernel_alignment,
723 					     LOAD_PHYSICAL_ADDR);
724 		if (status != EFI_SUCCESS) {
725 			efi_printk("efi_relocate_kernel() failed!\n");
726 			goto fail;
727 		}
728 	}
729 	hdr->code32_start = (u32)bzimage_addr;
730 
731 	/*
732 	 * efi_pe_entry() may have been called before efi_main(), in which
733 	 * case this is the second time we parse the cmdline. This is ok,
734 	 * parsing the cmdline multiple times does not have side-effects.
735 	 */
736 	cmdline_paddr = ((u64)hdr->cmd_line_ptr |
737 			 ((u64)boot_params->ext_cmd_line_ptr << 32));
738 	efi_parse_options((char *)cmdline_paddr);
739 
740 	/*
741 	 * At this point, an initrd may already have been loaded, either by
742 	 * the bootloader and passed via bootparams, or loaded from a initrd=
743 	 * command line option by efi_pe_entry() above. In either case, we
744 	 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device
745 	 * path to supersede it.
746 	 */
747 	if (!noinitrd()) {
748 		unsigned long addr, size;
749 		unsigned long max_addr = hdr->initrd_addr_max;
750 
751 		if (hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)
752 			max_addr = ULONG_MAX;
753 
754 		status = efi_load_initrd_dev_path(&addr, &size, max_addr);
755 		if (status == EFI_SUCCESS) {
756 			hdr->ramdisk_image		= (u32)addr;
757 			hdr->ramdisk_size 		= (u32)size;
758 			boot_params->ext_ramdisk_image	= (u64)addr >> 32;
759 			boot_params->ext_ramdisk_size 	= (u64)size >> 32;
760 		} else if (status != EFI_NOT_FOUND) {
761 			efi_printk("efi_load_initrd_dev_path() failed!\n");
762 			goto fail;
763 		}
764 	}
765 
766 	/*
767 	 * If the boot loader gave us a value for secure_boot then we use that,
768 	 * otherwise we ask the BIOS.
769 	 */
770 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
771 		boot_params->secure_boot = efi_get_secureboot();
772 
773 	/* Ask the firmware to clear memory on unclean shutdown */
774 	efi_enable_reset_attack_mitigation();
775 
776 	efi_random_get_seed();
777 
778 	efi_retrieve_tpm2_eventlog();
779 
780 	setup_graphics(boot_params);
781 
782 	setup_efi_pci(boot_params);
783 
784 	setup_quirks(boot_params);
785 
786 	status = exit_boot(boot_params, handle);
787 	if (status != EFI_SUCCESS) {
788 		efi_printk("exit_boot() failed!\n");
789 		goto fail;
790 	}
791 
792 	return boot_params;
793 fail:
794 	efi_printk("efi_main() failed!\n");
795 
796 	for (;;)
797 		asm("hlt");
798 }
799