Linux Audio

Check our new training course

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