xref: /linux/drivers/firmware/efi/libstub/x86-stub.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
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 #include <asm/kaslr.h>
19 #include <asm/sev.h>
20 
21 #include "efistub.h"
22 #include "x86-stub.h"
23 
24 const efi_system_table_t *efi_system_table;
25 const efi_dxe_services_table_t *efi_dxe_table;
26 static efi_loaded_image_t *image = NULL;
27 static efi_memory_attribute_protocol_t *memattr;
28 
29 typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
30 union sev_memory_acceptance_protocol {
31 	struct {
32 		efi_status_t (__efiapi * allow_unaccepted_memory)(
33 			sev_memory_acceptance_protocol_t *);
34 	};
35 	struct {
36 		u32 allow_unaccepted_memory;
37 	} mixed_mode;
38 };
39 
40 static efi_status_t
41 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
42 {
43 	struct pci_setup_rom *rom = NULL;
44 	efi_status_t status;
45 	unsigned long size;
46 	uint64_t romsize;
47 	void *romimage;
48 
49 	/*
50 	 * Some firmware images contain EFI function pointers at the place where
51 	 * the romimage and romsize fields are supposed to be. Typically the EFI
52 	 * code is mapped at high addresses, translating to an unrealistically
53 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
54 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
55 	 */
56 	romimage = efi_table_attr(pci, romimage);
57 	romsize = efi_table_attr(pci, romsize);
58 	if (!romimage || !romsize || romsize > SZ_16M)
59 		return EFI_INVALID_PARAMETER;
60 
61 	size = romsize + sizeof(*rom);
62 
63 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
64 			     (void **)&rom);
65 	if (status != EFI_SUCCESS) {
66 		efi_err("Failed to allocate memory for 'rom'\n");
67 		return status;
68 	}
69 
70 	memset(rom, 0, sizeof(*rom));
71 
72 	rom->data.type	= SETUP_PCI;
73 	rom->data.len	= size - sizeof(struct setup_data);
74 	rom->data.next	= 0;
75 	rom->pcilen	= romsize;
76 	*__rom = rom;
77 
78 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
79 				PCI_VENDOR_ID, 1, &rom->vendor);
80 
81 	if (status != EFI_SUCCESS) {
82 		efi_err("Failed to read rom->vendor\n");
83 		goto free_struct;
84 	}
85 
86 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
87 				PCI_DEVICE_ID, 1, &rom->devid);
88 
89 	if (status != EFI_SUCCESS) {
90 		efi_err("Failed to read rom->devid\n");
91 		goto free_struct;
92 	}
93 
94 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
95 				&rom->device, &rom->function);
96 
97 	if (status != EFI_SUCCESS)
98 		goto free_struct;
99 
100 	memcpy(rom->romdata, romimage, romsize);
101 	return status;
102 
103 free_struct:
104 	efi_bs_call(free_pool, rom);
105 	return status;
106 }
107 
108 /*
109  * There's no way to return an informative status from this function,
110  * because any analysis (and printing of error messages) needs to be
111  * done directly at the EFI function call-site.
112  *
113  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
114  * just didn't find any PCI devices, but there's no way to tell outside
115  * the context of the call.
116  */
117 static void setup_efi_pci(struct boot_params *params)
118 {
119 	efi_status_t status;
120 	void **pci_handle = NULL;
121 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
122 	unsigned long size = 0;
123 	struct setup_data *data;
124 	efi_handle_t h;
125 	int i;
126 
127 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 			     &pci_proto, NULL, &size, pci_handle);
129 
130 	if (status == EFI_BUFFER_TOO_SMALL) {
131 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
132 				     (void **)&pci_handle);
133 
134 		if (status != EFI_SUCCESS) {
135 			efi_err("Failed to allocate memory for 'pci_handle'\n");
136 			return;
137 		}
138 
139 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
140 				     &pci_proto, NULL, &size, pci_handle);
141 	}
142 
143 	if (status != EFI_SUCCESS)
144 		goto free_handle;
145 
146 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
147 
148 	while (data && data->next)
149 		data = (struct setup_data *)(unsigned long)data->next;
150 
151 	for_each_efi_handle(h, pci_handle, size, i) {
152 		efi_pci_io_protocol_t *pci = NULL;
153 		struct pci_setup_rom *rom;
154 
155 		status = efi_bs_call(handle_protocol, h, &pci_proto,
156 				     (void **)&pci);
157 		if (status != EFI_SUCCESS || !pci)
158 			continue;
159 
160 		status = preserve_pci_rom_image(pci, &rom);
161 		if (status != EFI_SUCCESS)
162 			continue;
163 
164 		if (data)
165 			data->next = (unsigned long)rom;
166 		else
167 			params->hdr.setup_data = (unsigned long)rom;
168 
169 		data = (struct setup_data *)rom;
170 	}
171 
172 free_handle:
173 	efi_bs_call(free_pool, pci_handle);
174 }
175 
176 static void retrieve_apple_device_properties(struct boot_params *boot_params)
177 {
178 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
179 	struct setup_data *data, *new;
180 	efi_status_t status;
181 	u32 size = 0;
182 	apple_properties_protocol_t *p;
183 
184 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
185 	if (status != EFI_SUCCESS)
186 		return;
187 
188 	if (efi_table_attr(p, version) != 0x10000) {
189 		efi_err("Unsupported properties proto version\n");
190 		return;
191 	}
192 
193 	efi_call_proto(p, get_all, NULL, &size);
194 	if (!size)
195 		return;
196 
197 	do {
198 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
199 				     size + sizeof(struct setup_data),
200 				     (void **)&new);
201 		if (status != EFI_SUCCESS) {
202 			efi_err("Failed to allocate memory for 'properties'\n");
203 			return;
204 		}
205 
206 		status = efi_call_proto(p, get_all, new->data, &size);
207 
208 		if (status == EFI_BUFFER_TOO_SMALL)
209 			efi_bs_call(free_pool, new);
210 	} while (status == EFI_BUFFER_TOO_SMALL);
211 
212 	new->type = SETUP_APPLE_PROPERTIES;
213 	new->len  = size;
214 	new->next = 0;
215 
216 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
217 	if (!data) {
218 		boot_params->hdr.setup_data = (unsigned long)new;
219 	} else {
220 		while (data->next)
221 			data = (struct setup_data *)(unsigned long)data->next;
222 		data->next = (unsigned long)new;
223 	}
224 }
225 
226 void efi_adjust_memory_range_protection(unsigned long start,
227 					unsigned long size)
228 {
229 	efi_status_t status;
230 	efi_gcd_memory_space_desc_t desc;
231 	unsigned long end, next;
232 	unsigned long rounded_start, rounded_end;
233 	unsigned long unprotect_start, unprotect_size;
234 
235 	rounded_start = rounddown(start, EFI_PAGE_SIZE);
236 	rounded_end = roundup(start + size, EFI_PAGE_SIZE);
237 
238 	if (memattr != NULL) {
239 		efi_call_proto(memattr, clear_memory_attributes, rounded_start,
240 			       rounded_end - rounded_start, EFI_MEMORY_XP);
241 		return;
242 	}
243 
244 	if (efi_dxe_table == NULL)
245 		return;
246 
247 	/*
248 	 * Don't modify memory region attributes, they are
249 	 * already suitable, to lower the possibility to
250 	 * encounter firmware bugs.
251 	 */
252 
253 	for (end = start + size; start < end; start = next) {
254 
255 		status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
256 
257 		if (status != EFI_SUCCESS)
258 			return;
259 
260 		next = desc.base_address + desc.length;
261 
262 		/*
263 		 * Only system memory is suitable for trampoline/kernel image placement,
264 		 * so only this type of memory needs its attributes to be modified.
265 		 */
266 
267 		if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
268 		    (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
269 			continue;
270 
271 		unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
272 		unprotect_size = min(rounded_end, next) - unprotect_start;
273 
274 		status = efi_dxe_call(set_memory_space_attributes,
275 				      unprotect_start, unprotect_size,
276 				      EFI_MEMORY_WB);
277 
278 		if (status != EFI_SUCCESS) {
279 			efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
280 				 unprotect_start,
281 				 unprotect_start + unprotect_size,
282 				 status);
283 		}
284 	}
285 }
286 
287 static void setup_unaccepted_memory(void)
288 {
289 	efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
290 	sev_memory_acceptance_protocol_t *proto;
291 	efi_status_t status;
292 
293 	if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
294 		return;
295 
296 	/*
297 	 * Enable unaccepted memory before calling exit boot services in order
298 	 * for the UEFI to not accept all memory on EBS.
299 	 */
300 	status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
301 			     (void **)&proto);
302 	if (status != EFI_SUCCESS)
303 		return;
304 
305 	status = efi_call_proto(proto, allow_unaccepted_memory);
306 	if (status != EFI_SUCCESS)
307 		efi_err("Memory acceptance protocol failed\n");
308 }
309 
310 static const efi_char16_t apple[] = L"Apple";
311 
312 static void setup_quirks(struct boot_params *boot_params)
313 {
314 	efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
315 		efi_table_attr(efi_system_table, fw_vendor);
316 
317 	if (!memcmp(fw_vendor, apple, sizeof(apple))) {
318 		if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
319 			retrieve_apple_device_properties(boot_params);
320 	}
321 }
322 
323 /*
324  * See if we have Universal Graphics Adapter (UGA) protocol
325  */
326 static efi_status_t
327 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
328 {
329 	efi_status_t status;
330 	u32 width, height;
331 	void **uga_handle = NULL;
332 	efi_uga_draw_protocol_t *uga = NULL, *first_uga;
333 	efi_handle_t handle;
334 	int i;
335 
336 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
337 			     (void **)&uga_handle);
338 	if (status != EFI_SUCCESS)
339 		return status;
340 
341 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
342 			     uga_proto, NULL, &size, uga_handle);
343 	if (status != EFI_SUCCESS)
344 		goto free_handle;
345 
346 	height = 0;
347 	width = 0;
348 
349 	first_uga = NULL;
350 	for_each_efi_handle(handle, uga_handle, size, i) {
351 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
352 		u32 w, h, depth, refresh;
353 		void *pciio;
354 
355 		status = efi_bs_call(handle_protocol, handle, uga_proto,
356 				     (void **)&uga);
357 		if (status != EFI_SUCCESS)
358 			continue;
359 
360 		pciio = NULL;
361 		efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
362 
363 		status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
364 		if (status == EFI_SUCCESS && (!first_uga || pciio)) {
365 			width = w;
366 			height = h;
367 
368 			/*
369 			 * Once we've found a UGA supporting PCIIO,
370 			 * don't bother looking any further.
371 			 */
372 			if (pciio)
373 				break;
374 
375 			first_uga = uga;
376 		}
377 	}
378 
379 	if (!width && !height)
380 		goto free_handle;
381 
382 	/* EFI framebuffer */
383 	si->orig_video_isVGA	= VIDEO_TYPE_EFI;
384 
385 	si->lfb_depth		= 32;
386 	si->lfb_width		= width;
387 	si->lfb_height		= height;
388 
389 	si->red_size		= 8;
390 	si->red_pos		= 16;
391 	si->green_size		= 8;
392 	si->green_pos		= 8;
393 	si->blue_size		= 8;
394 	si->blue_pos		= 0;
395 	si->rsvd_size		= 8;
396 	si->rsvd_pos		= 24;
397 
398 free_handle:
399 	efi_bs_call(free_pool, uga_handle);
400 
401 	return status;
402 }
403 
404 static void setup_graphics(struct boot_params *boot_params)
405 {
406 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
407 	struct screen_info *si;
408 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
409 	efi_status_t status;
410 	unsigned long size;
411 	void **gop_handle = NULL;
412 	void **uga_handle = NULL;
413 
414 	si = &boot_params->screen_info;
415 	memset(si, 0, sizeof(*si));
416 
417 	size = 0;
418 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
419 			     &graphics_proto, NULL, &size, gop_handle);
420 	if (status == EFI_BUFFER_TOO_SMALL)
421 		status = efi_setup_gop(si, &graphics_proto, size);
422 
423 	if (status != EFI_SUCCESS) {
424 		size = 0;
425 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
426 				     &uga_proto, NULL, &size, uga_handle);
427 		if (status == EFI_BUFFER_TOO_SMALL)
428 			setup_uga(si, &uga_proto, size);
429 	}
430 }
431 
432 
433 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
434 {
435 	efi_bs_call(exit, handle, status, 0, NULL);
436 	for(;;)
437 		asm("hlt");
438 }
439 
440 void __noreturn efi_stub_entry(efi_handle_t handle,
441 			       efi_system_table_t *sys_table_arg,
442 			       struct boot_params *boot_params);
443 
444 /*
445  * Because the x86 boot code expects to be passed a boot_params we
446  * need to create one ourselves (usually the bootloader would create
447  * one for us).
448  */
449 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
450 				   efi_system_table_t *sys_table_arg)
451 {
452 	static struct boot_params boot_params __page_aligned_bss;
453 	struct setup_header *hdr = &boot_params.hdr;
454 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
455 	int options_size = 0;
456 	efi_status_t status;
457 	char *cmdline_ptr;
458 
459 	efi_system_table = sys_table_arg;
460 
461 	/* Check if we were booted by the EFI firmware */
462 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
463 		efi_exit(handle, EFI_INVALID_PARAMETER);
464 
465 	status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
466 	if (status != EFI_SUCCESS) {
467 		efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
468 		efi_exit(handle, status);
469 	}
470 
471 	/* Assign the setup_header fields that the kernel actually cares about */
472 	hdr->root_flags	= 1;
473 	hdr->vid_mode	= 0xffff;
474 
475 	hdr->type_of_loader = 0x21;
476 
477 	/* Convert unicode cmdline to ascii */
478 	cmdline_ptr = efi_convert_cmdline(image, &options_size);
479 	if (!cmdline_ptr)
480 		goto fail;
481 
482 	efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,
483 			  &boot_params.ext_cmd_line_ptr);
484 
485 	efi_stub_entry(handle, sys_table_arg, &boot_params);
486 	/* not reached */
487 
488 fail:
489 	efi_exit(handle, status);
490 }
491 
492 static void add_e820ext(struct boot_params *params,
493 			struct setup_data *e820ext, u32 nr_entries)
494 {
495 	struct setup_data *data;
496 
497 	e820ext->type = SETUP_E820_EXT;
498 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
499 	e820ext->next = 0;
500 
501 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
502 
503 	while (data && data->next)
504 		data = (struct setup_data *)(unsigned long)data->next;
505 
506 	if (data)
507 		data->next = (unsigned long)e820ext;
508 	else
509 		params->hdr.setup_data = (unsigned long)e820ext;
510 }
511 
512 static efi_status_t
513 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
514 {
515 	struct boot_e820_entry *entry = params->e820_table;
516 	struct efi_info *efi = &params->efi_info;
517 	struct boot_e820_entry *prev = NULL;
518 	u32 nr_entries;
519 	u32 nr_desc;
520 	int i;
521 
522 	nr_entries = 0;
523 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
524 
525 	for (i = 0; i < nr_desc; i++) {
526 		efi_memory_desc_t *d;
527 		unsigned int e820_type = 0;
528 		unsigned long m = efi->efi_memmap;
529 
530 #ifdef CONFIG_X86_64
531 		m |= (u64)efi->efi_memmap_hi << 32;
532 #endif
533 
534 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
535 		switch (d->type) {
536 		case EFI_RESERVED_TYPE:
537 		case EFI_RUNTIME_SERVICES_CODE:
538 		case EFI_RUNTIME_SERVICES_DATA:
539 		case EFI_MEMORY_MAPPED_IO:
540 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
541 		case EFI_PAL_CODE:
542 			e820_type = E820_TYPE_RESERVED;
543 			break;
544 
545 		case EFI_UNUSABLE_MEMORY:
546 			e820_type = E820_TYPE_UNUSABLE;
547 			break;
548 
549 		case EFI_ACPI_RECLAIM_MEMORY:
550 			e820_type = E820_TYPE_ACPI;
551 			break;
552 
553 		case EFI_LOADER_CODE:
554 		case EFI_LOADER_DATA:
555 		case EFI_BOOT_SERVICES_CODE:
556 		case EFI_BOOT_SERVICES_DATA:
557 		case EFI_CONVENTIONAL_MEMORY:
558 			if (efi_soft_reserve_enabled() &&
559 			    (d->attribute & EFI_MEMORY_SP))
560 				e820_type = E820_TYPE_SOFT_RESERVED;
561 			else
562 				e820_type = E820_TYPE_RAM;
563 			break;
564 
565 		case EFI_ACPI_MEMORY_NVS:
566 			e820_type = E820_TYPE_NVS;
567 			break;
568 
569 		case EFI_PERSISTENT_MEMORY:
570 			e820_type = E820_TYPE_PMEM;
571 			break;
572 
573 		case EFI_UNACCEPTED_MEMORY:
574 			if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
575 				continue;
576 			e820_type = E820_TYPE_RAM;
577 			process_unaccepted_memory(d->phys_addr,
578 						  d->phys_addr + PAGE_SIZE * d->num_pages);
579 			break;
580 		default:
581 			continue;
582 		}
583 
584 		/* Merge adjacent mappings */
585 		if (prev && prev->type == e820_type &&
586 		    (prev->addr + prev->size) == d->phys_addr) {
587 			prev->size += d->num_pages << 12;
588 			continue;
589 		}
590 
591 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
592 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
593 				   sizeof(struct setup_data);
594 
595 			if (!e820ext || e820ext_size < need)
596 				return EFI_BUFFER_TOO_SMALL;
597 
598 			/* boot_params map full, switch to e820 extended */
599 			entry = (struct boot_e820_entry *)e820ext->data;
600 		}
601 
602 		entry->addr = d->phys_addr;
603 		entry->size = d->num_pages << PAGE_SHIFT;
604 		entry->type = e820_type;
605 		prev = entry++;
606 		nr_entries++;
607 	}
608 
609 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
610 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
611 
612 		add_e820ext(params, e820ext, nr_e820ext);
613 		nr_entries -= nr_e820ext;
614 	}
615 
616 	params->e820_entries = (u8)nr_entries;
617 
618 	return EFI_SUCCESS;
619 }
620 
621 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
622 				  u32 *e820ext_size)
623 {
624 	efi_status_t status;
625 	unsigned long size;
626 
627 	size = sizeof(struct setup_data) +
628 		sizeof(struct e820_entry) * nr_desc;
629 
630 	if (*e820ext) {
631 		efi_bs_call(free_pool, *e820ext);
632 		*e820ext = NULL;
633 		*e820ext_size = 0;
634 	}
635 
636 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
637 			     (void **)e820ext);
638 	if (status == EFI_SUCCESS)
639 		*e820ext_size = size;
640 
641 	return status;
642 }
643 
644 static efi_status_t allocate_e820(struct boot_params *params,
645 				  struct setup_data **e820ext,
646 				  u32 *e820ext_size)
647 {
648 	struct efi_boot_memmap *map;
649 	efi_status_t status;
650 	__u32 nr_desc;
651 
652 	status = efi_get_memory_map(&map, false);
653 	if (status != EFI_SUCCESS)
654 		return status;
655 
656 	nr_desc = map->map_size / map->desc_size;
657 	if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
658 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
659 				 EFI_MMAP_NR_SLACK_SLOTS;
660 
661 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
662 	}
663 
664 	if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
665 		status = allocate_unaccepted_bitmap(nr_desc, map);
666 
667 	efi_bs_call(free_pool, map);
668 	return status;
669 }
670 
671 struct exit_boot_struct {
672 	struct boot_params	*boot_params;
673 	struct efi_info		*efi;
674 };
675 
676 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
677 				   void *priv)
678 {
679 	const char *signature;
680 	struct exit_boot_struct *p = priv;
681 
682 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
683 				   : EFI32_LOADER_SIGNATURE;
684 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
685 
686 	efi_set_u64_split((unsigned long)efi_system_table,
687 			  &p->efi->efi_systab, &p->efi->efi_systab_hi);
688 	p->efi->efi_memdesc_size	= map->desc_size;
689 	p->efi->efi_memdesc_version	= map->desc_ver;
690 	efi_set_u64_split((unsigned long)map->map,
691 			  &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
692 	p->efi->efi_memmap_size		= map->map_size;
693 
694 	return EFI_SUCCESS;
695 }
696 
697 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
698 {
699 	struct setup_data *e820ext = NULL;
700 	__u32 e820ext_size = 0;
701 	efi_status_t status;
702 	struct exit_boot_struct priv;
703 
704 	priv.boot_params	= boot_params;
705 	priv.efi		= &boot_params->efi_info;
706 
707 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
708 	if (status != EFI_SUCCESS)
709 		return status;
710 
711 	/* Might as well exit boot services now */
712 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
713 	if (status != EFI_SUCCESS)
714 		return status;
715 
716 	/* Historic? */
717 	boot_params->alt_mem_k	= 32 * 1024;
718 
719 	status = setup_e820(boot_params, e820ext, e820ext_size);
720 	if (status != EFI_SUCCESS)
721 		return status;
722 
723 	return EFI_SUCCESS;
724 }
725 
726 static bool have_unsupported_snp_features(void)
727 {
728 	u64 unsupported;
729 
730 	unsupported = snp_get_unsupported_features(sev_get_status());
731 	if (unsupported) {
732 		efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
733 			unsupported);
734 		return true;
735 	}
736 	return false;
737 }
738 
739 static void efi_get_seed(void *seed, int size)
740 {
741 	efi_get_random_bytes(size, seed);
742 
743 	/*
744 	 * This only updates seed[0] when running on 32-bit, but in that case,
745 	 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
746 	 */
747 	*(unsigned long *)seed ^= kaslr_get_random_long("EFI");
748 }
749 
750 static void error(char *str)
751 {
752 	efi_warn("Decompression failed: %s\n", str);
753 }
754 
755 static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
756 {
757 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
758 	unsigned long addr, alloc_size, entry;
759 	efi_status_t status;
760 	u32 seed[2] = {};
761 
762 	/* determine the required size of the allocation */
763 	alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
764 			   MIN_KERNEL_ALIGN);
765 
766 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
767 		u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
768 
769 		efi_get_seed(seed, sizeof(seed));
770 
771 		virt_addr += (range * seed[1]) >> 32;
772 		virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
773 	}
774 
775 	status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
776 				  seed[0], EFI_LOADER_CODE,
777 				  EFI_X86_KERNEL_ALLOC_LIMIT);
778 	if (status != EFI_SUCCESS)
779 		return status;
780 
781 	entry = decompress_kernel((void *)addr, virt_addr, error);
782 	if (entry == ULONG_MAX) {
783 		efi_free(alloc_size, addr);
784 		return EFI_LOAD_ERROR;
785 	}
786 
787 	*kernel_entry = addr + entry;
788 
789 	efi_adjust_memory_range_protection(addr, kernel_total_size);
790 
791 	return EFI_SUCCESS;
792 }
793 
794 static void __noreturn enter_kernel(unsigned long kernel_addr,
795 				    struct boot_params *boot_params)
796 {
797 	/* enter decompressed kernel with boot_params pointer in RSI/ESI */
798 	asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
799 
800 	unreachable();
801 }
802 
803 /*
804  * On success, this routine will jump to the relocated image directly and never
805  * return.  On failure, it will exit to the firmware via efi_exit() instead of
806  * returning.
807  */
808 void __noreturn efi_stub_entry(efi_handle_t handle,
809 			       efi_system_table_t *sys_table_arg,
810 			       struct boot_params *boot_params)
811 {
812 	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
813 	struct setup_header *hdr = &boot_params->hdr;
814 	const struct linux_efi_initrd *initrd = NULL;
815 	unsigned long kernel_entry;
816 	efi_status_t status;
817 
818 	boot_params_ptr = boot_params;
819 
820 	efi_system_table = sys_table_arg;
821 	/* Check if we were booted by the EFI firmware */
822 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
823 		efi_exit(handle, EFI_INVALID_PARAMETER);
824 
825 	if (have_unsupported_snp_features())
826 		efi_exit(handle, EFI_UNSUPPORTED);
827 
828 	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
829 		efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
830 		if (efi_dxe_table &&
831 		    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
832 			efi_warn("Ignoring DXE services table: invalid signature\n");
833 			efi_dxe_table = NULL;
834 		}
835 	}
836 
837 	/* grab the memory attributes protocol if it exists */
838 	efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
839 
840 	status = efi_setup_5level_paging();
841 	if (status != EFI_SUCCESS) {
842 		efi_err("efi_setup_5level_paging() failed!\n");
843 		goto fail;
844 	}
845 
846 #ifdef CONFIG_CMDLINE_BOOL
847 	status = efi_parse_options(CONFIG_CMDLINE);
848 	if (status != EFI_SUCCESS) {
849 		efi_err("Failed to parse options\n");
850 		goto fail;
851 	}
852 #endif
853 	if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
854 		unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
855 					       ((u64)boot_params->ext_cmd_line_ptr << 32));
856 		status = efi_parse_options((char *)cmdline_paddr);
857 		if (status != EFI_SUCCESS) {
858 			efi_err("Failed to parse options\n");
859 			goto fail;
860 		}
861 	}
862 
863 	status = efi_decompress_kernel(&kernel_entry);
864 	if (status != EFI_SUCCESS) {
865 		efi_err("Failed to decompress kernel\n");
866 		goto fail;
867 	}
868 
869 	/*
870 	 * At this point, an initrd may already have been loaded by the
871 	 * bootloader and passed via bootparams. We permit an initrd loaded
872 	 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
873 	 *
874 	 * If the device path is not present, any command-line initrd=
875 	 * arguments will be processed only if image is not NULL, which will be
876 	 * the case only if we were loaded via the PE entry point.
877 	 */
878 	status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
879 				 &initrd);
880 	if (status != EFI_SUCCESS)
881 		goto fail;
882 	if (initrd && initrd->size > 0) {
883 		efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
884 				  &boot_params->ext_ramdisk_image);
885 		efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
886 				  &boot_params->ext_ramdisk_size);
887 	}
888 
889 
890 	/*
891 	 * If the boot loader gave us a value for secure_boot then we use that,
892 	 * otherwise we ask the BIOS.
893 	 */
894 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
895 		boot_params->secure_boot = efi_get_secureboot();
896 
897 	/* Ask the firmware to clear memory on unclean shutdown */
898 	efi_enable_reset_attack_mitigation();
899 
900 	efi_random_get_seed();
901 
902 	efi_retrieve_tpm2_eventlog();
903 
904 	setup_graphics(boot_params);
905 
906 	setup_efi_pci(boot_params);
907 
908 	setup_quirks(boot_params);
909 
910 	setup_unaccepted_memory();
911 
912 	status = exit_boot(boot_params, handle);
913 	if (status != EFI_SUCCESS) {
914 		efi_err("exit_boot() failed!\n");
915 		goto fail;
916 	}
917 
918 	/*
919 	 * Call the SEV init code while still running with the firmware's
920 	 * GDT/IDT, so #VC exceptions will be handled by EFI.
921 	 */
922 	sev_enable(boot_params);
923 
924 	efi_5level_switch();
925 
926 	enter_kernel(kernel_entry, boot_params);
927 fail:
928 	efi_err("efi_stub_entry() failed!\n");
929 
930 	efi_exit(handle, status);
931 }
932 
933 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
934 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
935 			struct boot_params *boot_params)
936 {
937 	extern char _bss[], _ebss[];
938 
939 	memset(_bss, 0, _ebss - _bss);
940 	efi_stub_entry(handle, sys_table_arg, boot_params);
941 }
942 
943 #ifndef CONFIG_EFI_MIXED
944 extern __alias(efi_handover_entry)
945 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
946 		      struct boot_params *boot_params);
947 
948 extern __alias(efi_handover_entry)
949 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
950 		      struct boot_params *boot_params);
951 #endif
952 #endif
953