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 *)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, 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 	status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
425 				 hdr->initrd_addr_max,
426 				 above4g ? ULONG_MAX : hdr->initrd_addr_max);
427 	if (status != EFI_SUCCESS)
428 		goto fail2;
429 	hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
430 	hdr->ramdisk_size  = ramdisk_size & 0xffffffff;
431 	boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
432 	boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
433 
434 	efi_stub_entry(handle, sys_table, boot_params);
435 	/* not reached */
436 
437 fail2:
438 	efi_free(options_size, (unsigned long)cmdline_ptr);
439 fail:
440 	efi_free(0x4000, (unsigned long)boot_params);
441 
442 	return status;
443 }
444 
445 static void add_e820ext(struct boot_params *params,
446 			struct setup_data *e820ext, u32 nr_entries)
447 {
448 	struct setup_data *data;
449 
450 	e820ext->type = SETUP_E820_EXT;
451 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
452 	e820ext->next = 0;
453 
454 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
455 
456 	while (data && data->next)
457 		data = (struct setup_data *)(unsigned long)data->next;
458 
459 	if (data)
460 		data->next = (unsigned long)e820ext;
461 	else
462 		params->hdr.setup_data = (unsigned long)e820ext;
463 }
464 
465 static efi_status_t
466 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
467 {
468 	struct boot_e820_entry *entry = params->e820_table;
469 	struct efi_info *efi = &params->efi_info;
470 	struct boot_e820_entry *prev = NULL;
471 	u32 nr_entries;
472 	u32 nr_desc;
473 	int i;
474 
475 	nr_entries = 0;
476 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
477 
478 	for (i = 0; i < nr_desc; i++) {
479 		efi_memory_desc_t *d;
480 		unsigned int e820_type = 0;
481 		unsigned long m = efi->efi_memmap;
482 
483 #ifdef CONFIG_X86_64
484 		m |= (u64)efi->efi_memmap_hi << 32;
485 #endif
486 
487 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
488 		switch (d->type) {
489 		case EFI_RESERVED_TYPE:
490 		case EFI_RUNTIME_SERVICES_CODE:
491 		case EFI_RUNTIME_SERVICES_DATA:
492 		case EFI_MEMORY_MAPPED_IO:
493 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
494 		case EFI_PAL_CODE:
495 			e820_type = E820_TYPE_RESERVED;
496 			break;
497 
498 		case EFI_UNUSABLE_MEMORY:
499 			e820_type = E820_TYPE_UNUSABLE;
500 			break;
501 
502 		case EFI_ACPI_RECLAIM_MEMORY:
503 			e820_type = E820_TYPE_ACPI;
504 			break;
505 
506 		case EFI_LOADER_CODE:
507 		case EFI_LOADER_DATA:
508 		case EFI_BOOT_SERVICES_CODE:
509 		case EFI_BOOT_SERVICES_DATA:
510 		case EFI_CONVENTIONAL_MEMORY:
511 			if (efi_soft_reserve_enabled() &&
512 			    (d->attribute & EFI_MEMORY_SP))
513 				e820_type = E820_TYPE_SOFT_RESERVED;
514 			else
515 				e820_type = E820_TYPE_RAM;
516 			break;
517 
518 		case EFI_ACPI_MEMORY_NVS:
519 			e820_type = E820_TYPE_NVS;
520 			break;
521 
522 		case EFI_PERSISTENT_MEMORY:
523 			e820_type = E820_TYPE_PMEM;
524 			break;
525 
526 		default:
527 			continue;
528 		}
529 
530 		/* Merge adjacent mappings */
531 		if (prev && prev->type == e820_type &&
532 		    (prev->addr + prev->size) == d->phys_addr) {
533 			prev->size += d->num_pages << 12;
534 			continue;
535 		}
536 
537 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
538 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
539 				   sizeof(struct setup_data);
540 
541 			if (!e820ext || e820ext_size < need)
542 				return EFI_BUFFER_TOO_SMALL;
543 
544 			/* boot_params map full, switch to e820 extended */
545 			entry = (struct boot_e820_entry *)e820ext->data;
546 		}
547 
548 		entry->addr = d->phys_addr;
549 		entry->size = d->num_pages << PAGE_SHIFT;
550 		entry->type = e820_type;
551 		prev = entry++;
552 		nr_entries++;
553 	}
554 
555 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
556 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
557 
558 		add_e820ext(params, e820ext, nr_e820ext);
559 		nr_entries -= nr_e820ext;
560 	}
561 
562 	params->e820_entries = (u8)nr_entries;
563 
564 	return EFI_SUCCESS;
565 }
566 
567 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
568 				  u32 *e820ext_size)
569 {
570 	efi_status_t status;
571 	unsigned long size;
572 
573 	size = sizeof(struct setup_data) +
574 		sizeof(struct e820_entry) * nr_desc;
575 
576 	if (*e820ext) {
577 		efi_bs_call(free_pool, *e820ext);
578 		*e820ext = NULL;
579 		*e820ext_size = 0;
580 	}
581 
582 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
583 			     (void **)e820ext);
584 	if (status == EFI_SUCCESS)
585 		*e820ext_size = size;
586 
587 	return status;
588 }
589 
590 static efi_status_t allocate_e820(struct boot_params *params,
591 				  struct setup_data **e820ext,
592 				  u32 *e820ext_size)
593 {
594 	unsigned long map_size, desc_size, buff_size;
595 	struct efi_boot_memmap boot_map;
596 	efi_memory_desc_t *map;
597 	efi_status_t status;
598 	__u32 nr_desc;
599 
600 	boot_map.map		= &map;
601 	boot_map.map_size	= &map_size;
602 	boot_map.desc_size	= &desc_size;
603 	boot_map.desc_ver	= NULL;
604 	boot_map.key_ptr	= NULL;
605 	boot_map.buff_size	= &buff_size;
606 
607 	status = efi_get_memory_map(&boot_map);
608 	if (status != EFI_SUCCESS)
609 		return status;
610 
611 	nr_desc = buff_size / desc_size;
612 
613 	if (nr_desc > ARRAY_SIZE(params->e820_table)) {
614 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
615 
616 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
617 		if (status != EFI_SUCCESS)
618 			return status;
619 	}
620 
621 	return EFI_SUCCESS;
622 }
623 
624 struct exit_boot_struct {
625 	struct boot_params	*boot_params;
626 	struct efi_info		*efi;
627 };
628 
629 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
630 				   void *priv)
631 {
632 	const char *signature;
633 	struct exit_boot_struct *p = priv;
634 
635 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
636 				   : EFI32_LOADER_SIGNATURE;
637 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
638 
639 	p->efi->efi_systab		= (unsigned long)efi_system_table();
640 	p->efi->efi_memdesc_size	= *map->desc_size;
641 	p->efi->efi_memdesc_version	= *map->desc_ver;
642 	p->efi->efi_memmap		= (unsigned long)*map->map;
643 	p->efi->efi_memmap_size		= *map->map_size;
644 
645 #ifdef CONFIG_X86_64
646 	p->efi->efi_systab_hi		= (unsigned long)efi_system_table() >> 32;
647 	p->efi->efi_memmap_hi		= (unsigned long)*map->map >> 32;
648 #endif
649 
650 	return EFI_SUCCESS;
651 }
652 
653 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
654 {
655 	unsigned long map_sz, key, desc_size, buff_size;
656 	efi_memory_desc_t *mem_map;
657 	struct setup_data *e820ext = NULL;
658 	__u32 e820ext_size = 0;
659 	efi_status_t status;
660 	__u32 desc_version;
661 	struct efi_boot_memmap map;
662 	struct exit_boot_struct priv;
663 
664 	map.map			= &mem_map;
665 	map.map_size		= &map_sz;
666 	map.desc_size		= &desc_size;
667 	map.desc_ver		= &desc_version;
668 	map.key_ptr		= &key;
669 	map.buff_size		= &buff_size;
670 	priv.boot_params	= boot_params;
671 	priv.efi		= &boot_params->efi_info;
672 
673 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
674 	if (status != EFI_SUCCESS)
675 		return status;
676 
677 	/* Might as well exit boot services now */
678 	status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
679 	if (status != EFI_SUCCESS)
680 		return status;
681 
682 	/* Historic? */
683 	boot_params->alt_mem_k	= 32 * 1024;
684 
685 	status = setup_e820(boot_params, e820ext, e820ext_size);
686 	if (status != EFI_SUCCESS)
687 		return status;
688 
689 	return EFI_SUCCESS;
690 }
691 
692 /*
693  * On success we return a pointer to a boot_params structure, and NULL
694  * on failure.
695  */
696 struct boot_params *efi_main(efi_handle_t handle,
697 			     efi_system_table_t *sys_table_arg,
698 			     struct boot_params *boot_params)
699 {
700 	unsigned long bzimage_addr = (unsigned long)startup_32;
701 	struct setup_header *hdr = &boot_params->hdr;
702 	efi_status_t status;
703 	unsigned long cmdline_paddr;
704 
705 	sys_table = sys_table_arg;
706 
707 	/* Check if we were booted by the EFI firmware */
708 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
709 		goto fail;
710 
711 	/*
712 	 * If the kernel isn't already loaded at the preferred load
713 	 * address, relocate it.
714 	 */
715 	if (bzimage_addr != hdr->pref_address) {
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_printk("efi_relocate_kernel() failed!\n");
723 			goto fail;
724 		}
725 	}
726 	hdr->code32_start = (u32)bzimage_addr;
727 
728 	/*
729 	 * efi_pe_entry() may have been called before efi_main(), in which
730 	 * case this is the second time we parse the cmdline. This is ok,
731 	 * parsing the cmdline multiple times does not have side-effects.
732 	 */
733 	cmdline_paddr = ((u64)hdr->cmd_line_ptr |
734 			 ((u64)boot_params->ext_cmd_line_ptr << 32));
735 	efi_parse_options((char *)cmdline_paddr);
736 
737 	/*
738 	 * If the boot loader gave us a value for secure_boot then we use that,
739 	 * otherwise we ask the BIOS.
740 	 */
741 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
742 		boot_params->secure_boot = efi_get_secureboot();
743 
744 	/* Ask the firmware to clear memory on unclean shutdown */
745 	efi_enable_reset_attack_mitigation();
746 
747 	efi_random_get_seed();
748 
749 	efi_retrieve_tpm2_eventlog();
750 
751 	setup_graphics(boot_params);
752 
753 	setup_efi_pci(boot_params);
754 
755 	setup_quirks(boot_params);
756 
757 	status = exit_boot(boot_params, handle);
758 	if (status != EFI_SUCCESS) {
759 		efi_printk("exit_boot() failed!\n");
760 		goto fail;
761 	}
762 
763 	return boot_params;
764 fail:
765 	efi_printk("efi_main() failed!\n");
766 
767 	for (;;)
768 		asm("hlt");
769 }
770