efi.c 28.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * efi.c - EFI subsystem
 *
 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
 *
 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
 * allowing the efivarfs to be mounted or the efivars module to be loaded.
 * The existance of /sys/firmware/efi may also be used by userspace to
 * determine that the system supports EFI.
 */

15 16
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

17 18 19
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/init.h>
20
#include <linux/debugfs.h>
21 22
#include <linux/device.h>
#include <linux/efi.h>
23
#include <linux/of.h>
24
#include <linux/initrd.h>
25
#include <linux/io.h>
26
#include <linux/kexec.h>
L
Lee, Chun-Yi 已提交
27
#include <linux/platform_device.h>
28 29
#include <linux/random.h>
#include <linux/reboot.h>
30 31 32
#include <linux/slab.h>
#include <linux/acpi.h>
#include <linux/ucs2_string.h>
33
#include <linux/memblock.h>
34
#include <linux/security.h>
35

36
#include <asm/early_ioremap.h>
37

38
struct efi __read_mostly efi = {
39
	.runtime_supported_mask = EFI_RT_SUPPORTED_ALL,
40 41 42 43 44
	.acpi			= EFI_INVALID_TABLE_ADDR,
	.acpi20			= EFI_INVALID_TABLE_ADDR,
	.smbios			= EFI_INVALID_TABLE_ADDR,
	.smbios3		= EFI_INVALID_TABLE_ADDR,
	.esrt			= EFI_INVALID_TABLE_ADDR,
45
	.tpm_log		= EFI_INVALID_TABLE_ADDR,
46
	.tpm_final_log		= EFI_INVALID_TABLE_ADDR,
47 48 49
#ifdef CONFIG_LOAD_UEFI_KEYS
	.mokvar_table		= EFI_INVALID_TABLE_ADDR,
#endif
50 51 52
#ifdef CONFIG_EFI_COCO_SECRET
	.coco_secret		= EFI_INVALID_TABLE_ADDR,
#endif
53 54
};
EXPORT_SYMBOL(efi);
55

56
unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;
57
static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
58
static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
59
static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR;
60

61 62
extern unsigned long screen_info_table;

63
struct mm_struct efi_mm = {
64
	.mm_mt			= MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock),
65 66
	.mm_users		= ATOMIC_INIT(2),
	.mm_count		= ATOMIC_INIT(1),
67
	.write_protect_seq      = SEQCNT_ZERO(efi_mm.write_protect_seq),
68
	MMAP_LOCK_INITIALIZER(efi_mm)
69 70
	.page_table_lock	= __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
	.mmlist			= LIST_HEAD_INIT(efi_mm.mmlist),
71
	.cpu_bitmap		= { [BITS_TO_LONGS(NR_CPUS)] = 0},
72 73
};

74 75
struct workqueue_struct *efi_rts_wq;

76
static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME);
77 78 79 80 81 82 83 84 85 86 87 88
static int __init setup_noefi(char *arg)
{
	disable_runtime = true;
	return 0;
}
early_param("noefi", setup_noefi);

bool efi_runtime_disabled(void)
{
	return disable_runtime;
}

89 90 91 92 93
bool __pure __efi_soft_reserve_enabled(void)
{
	return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE);
}

D
Dave Young 已提交
94 95
static int __init parse_efi_cmdline(char *str)
{
96 97 98 99 100
	if (!str) {
		pr_warn("need at least one option\n");
		return -EINVAL;
	}

101 102 103
	if (parse_option_str(str, "debug"))
		set_bit(EFI_DBG, &efi.flags);

D
Dave Young 已提交
104 105 106
	if (parse_option_str(str, "noruntime"))
		disable_runtime = true;

107 108 109
	if (parse_option_str(str, "runtime"))
		disable_runtime = false;

110 111
	if (parse_option_str(str, "nosoftreserve"))
		set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags);
D
Dave Young 已提交
112 113 114 115 116

	return 0;
}
early_param("efi", parse_efi_cmdline);

P
Peter Jones 已提交
117
struct kobject *efi_kobj;
118 119 120 121

/*
 * Let's not leave out systab information that snuck into
 * the efivars driver
122 123
 * Note, do not add more fields in systab sysfs file as it breaks sysfs
 * one value per file rule!
124 125 126 127 128 129 130 131 132 133 134 135 136
 */
static ssize_t systab_show(struct kobject *kobj,
			   struct kobj_attribute *attr, char *buf)
{
	char *str = buf;

	if (!kobj || !buf)
		return -EINVAL;

	if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
		str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
	if (efi.acpi != EFI_INVALID_TABLE_ADDR)
		str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
137 138 139 140 141
	/*
	 * If both SMBIOS and SMBIOS3 entry points are implemented, the
	 * SMBIOS3 entry point shall be preferred, so we list it first to
	 * let applications stop parsing after the first match.
	 */
142 143
	if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
		str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
144 145
	if (efi.smbios != EFI_INVALID_TABLE_ADDR)
		str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
146

147
	if (IS_ENABLED(CONFIG_IA64) || IS_ENABLED(CONFIG_X86))
148 149
		str = efi_systab_show_arch(str);

150 151 152
	return str - buf;
}

153
static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
154

155 156 157 158 159 160
static ssize_t fw_platform_size_show(struct kobject *kobj,
				     struct kobj_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
}

161 162 163
extern __weak struct kobj_attribute efi_attr_fw_vendor;
extern __weak struct kobj_attribute efi_attr_runtime;
extern __weak struct kobj_attribute efi_attr_config_table;
164 165
static struct kobj_attribute efi_attr_fw_platform_size =
	__ATTR_RO(fw_platform_size);
166

167 168
static struct attribute *efi_subsys_attrs[] = {
	&efi_attr_systab.attr,
169
	&efi_attr_fw_platform_size.attr,
170 171 172 173
	&efi_attr_fw_vendor.attr,
	&efi_attr_runtime.attr,
	&efi_attr_config_table.attr,
	NULL,
174 175
};

176 177
umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr,
				   int n)
178
{
D
Daniel Kiper 已提交
179
	return attr->mode;
180 181
}

182
static const struct attribute_group efi_subsys_attr_group = {
183
	.attrs = efi_subsys_attrs,
184
	.is_visible = efi_attr_is_visible,
185 186 187 188 189 190 191 192 193
};

static struct efivars generic_efivars;
static struct efivar_operations generic_ops;

static int generic_ops_register(void)
{
	generic_ops.get_variable = efi.get_variable;
	generic_ops.get_next_variable = efi.get_next_variable;
194
	generic_ops.query_variable_store = efi_query_variable_store;
195

196 197 198 199
	if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) {
		generic_ops.set_variable = efi.set_variable;
		generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
	}
200 201 202 203 204 205 206 207
	return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
}

static void generic_ops_unregister(void)
{
	efivars_unregister(&generic_efivars);
}

208
#ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
209
#define EFIVAR_SSDT_NAME_MAX	16UL
210 211 212
static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
static int __init efivar_ssdt_setup(char *str)
{
213 214 215 216 217
	int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);

	if (ret)
		return ret;

218 219 220 221
	if (strlen(str) < sizeof(efivar_ssdt))
		memcpy(efivar_ssdt, str, strlen(str));
	else
		pr_warn("efivar_ssdt: name too long: %s\n", str);
222
	return 1;
223 224 225 226 227
}
__setup("efivar_ssdt=", efivar_ssdt_setup);

static __init int efivar_ssdt_load(void)
{
228 229 230 231
	unsigned long name_size = 256;
	efi_char16_t *name = NULL;
	efi_status_t status;
	efi_guid_t guid;
232

233 234 235
	if (!efivar_ssdt[0])
		return 0;

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	name = kzalloc(name_size, GFP_KERNEL);
	if (!name)
		return -ENOMEM;

	for (;;) {
		char utf8_name[EFIVAR_SSDT_NAME_MAX];
		unsigned long data_size = 0;
		void *data;
		int limit;

		status = efi.get_next_variable(&name_size, name, &guid);
		if (status == EFI_NOT_FOUND) {
			break;
		} else if (status == EFI_BUFFER_TOO_SMALL) {
			name = krealloc(name, name_size, GFP_KERNEL);
			if (!name)
				return -ENOMEM;
			continue;
254 255
		}

256 257 258 259
		limit = min(EFIVAR_SSDT_NAME_MAX, name_size);
		ucs2_as_utf8(utf8_name, name, limit - 1);
		if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
			continue;
260

261
		pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid);
262

263 264 265
		status = efi.get_variable(name, &guid, NULL, &data_size, NULL);
		if (status != EFI_BUFFER_TOO_SMALL || !data_size)
			return -EIO;
266

267 268 269
		data = kmalloc(data_size, GFP_KERNEL);
		if (!data)
			return -ENOMEM;
270

271 272 273 274 275
		status = efi.get_variable(name, &guid, NULL, &data_size, data);
		if (status == EFI_SUCCESS) {
			acpi_status ret = acpi_load_table(data, NULL);
			if (ret)
				pr_err("failed to load table: %u\n", ret);
276 277
			else
				continue;
278 279 280
		} else {
			pr_err("failed to get var data: 0x%lx\n", status);
		}
281 282
		kfree(data);
	}
283
	return 0;
284 285 286 287 288
}
#else
static inline int efivar_ssdt_load(void) { return 0; }
#endif

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
#ifdef CONFIG_DEBUG_FS

#define EFI_DEBUGFS_MAX_BLOBS 32

static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];

static void __init efi_debugfs_init(void)
{
	struct dentry *efi_debugfs;
	efi_memory_desc_t *md;
	char name[32];
	int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
	int i = 0;

	efi_debugfs = debugfs_create_dir("efi", NULL);
	if (IS_ERR_OR_NULL(efi_debugfs))
		return;

	for_each_efi_memory_desc(md) {
		switch (md->type) {
		case EFI_BOOT_SERVICES_CODE:
			snprintf(name, sizeof(name), "boot_services_code%d",
				 type_count[md->type]++);
			break;
		case EFI_BOOT_SERVICES_DATA:
			snprintf(name, sizeof(name), "boot_services_data%d",
				 type_count[md->type]++);
			break;
		default:
			continue;
		}

		if (i >= EFI_DEBUGFS_MAX_BLOBS) {
			pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
				EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
			break;
		}

		debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
		debugfs_blob[i].data = memremap(md->phys_addr,
						debugfs_blob[i].size,
						MEMREMAP_WB);
		if (!debugfs_blob[i].data)
			continue;

		debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
		i++;
	}
}
#else
static inline void efi_debugfs_init(void) {}
#endif

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
static void refresh_nv_rng_seed(struct work_struct *work)
{
	u8 seed[EFI_RANDOM_SEED_SIZE];

	get_random_bytes(seed, sizeof(seed));
	efi.set_variable(L"RandomSeed", &LINUX_EFI_RANDOM_SEED_TABLE_GUID,
			 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
			 EFI_VARIABLE_RUNTIME_ACCESS, sizeof(seed), seed);
	memzero_explicit(seed, sizeof(seed));
}
static int refresh_nv_rng_seed_notification(struct notifier_block *nb, unsigned long action, void *data)
{
	static DECLARE_WORK(work, refresh_nv_rng_seed);
	schedule_work(&work);
	return NOTIFY_DONE;
}
static struct notifier_block refresh_nv_rng_seed_nb = { .notifier_call = refresh_nv_rng_seed_notification };

360 361 362 363 364 365 366 367 368
/*
 * We register the efi subsystem with the firmware subsystem and the
 * efivars subsystem with the efi subsystem, if the system was booted with
 * EFI.
 */
static int __init efisubsys_init(void)
{
	int error;

369 370 371
	if (!efi_enabled(EFI_RUNTIME_SERVICES))
		efi.runtime_supported_mask = 0;

372 373 374
	if (!efi_enabled(EFI_BOOT))
		return 0;

375 376 377 378 379 380 381 382 383 384 385 386 387
	if (efi.runtime_supported_mask) {
		/*
		 * Since we process only one efi_runtime_service() at a time, an
		 * ordered workqueue (which creates only one execution context)
		 * should suffice for all our needs.
		 */
		efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
		if (!efi_rts_wq) {
			pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
			clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
			efi.runtime_supported_mask = 0;
			return 0;
		}
388 389
	}

390 391 392
	if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES))
		platform_device_register_simple("rtc-efi", 0, NULL, 0);

393 394 395 396
	/* We register the efi directory at /sys/firmware/efi */
	efi_kobj = kobject_create_and_add("efi", firmware_kobj);
	if (!efi_kobj) {
		pr_err("efi: Firmware registration failed.\n");
397
		destroy_workqueue(efi_rts_wq);
398 399 400
		return -ENOMEM;
	}

401 402
	if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
				      EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) {
403 404 405
		error = generic_ops_register();
		if (error)
			goto err_put;
406
		efivar_ssdt_load();
407 408
		platform_device_register_simple("efivars", 0, NULL, 0);
	}
409

410 411 412 413 414 415 416 417
	error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
	if (error) {
		pr_err("efi: Sysfs attribute export failed with error %d.\n",
		       error);
		goto err_unregister;
	}

	/* and the standard mountpoint for efivarfs */
418 419
	error = sysfs_create_mount_point(efi_kobj, "efivars");
	if (error) {
420 421 422 423
		pr_err("efivars: Subsystem registration failed.\n");
		goto err_remove_group;
	}

424 425 426
	if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
		efi_debugfs_init();

427 428 429 430 431
#ifdef CONFIG_EFI_COCO_SECRET
	if (efi.coco_secret != EFI_INVALID_TABLE_ADDR)
		platform_device_register_simple("efi_secret", 0, NULL, 0);
#endif

432 433 434
	if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
		execute_with_initialized_rng(&refresh_nv_rng_seed_nb);

435 436 437 438 439
	return 0;

err_remove_group:
	sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
err_unregister:
440 441
	if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
				      EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME))
442
		generic_ops_unregister();
443 444
err_put:
	kobject_put(efi_kobj);
445
	efi_kobj = NULL;
446
	destroy_workqueue(efi_rts_wq);
447 448 449 450
	return error;
}

subsys_initcall(efisubsys_init);
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
void __init efi_find_mirror(void)
{
	efi_memory_desc_t *md;
	u64 mirror_size = 0, total_size = 0;

	if (!efi_enabled(EFI_MEMMAP))
		return;

	for_each_efi_memory_desc(md) {
		unsigned long long start = md->phys_addr;
		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;

		total_size += size;
		if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
			memblock_mark_mirror(start, size);
			mirror_size += size;
		}
	}
	if (mirror_size)
		pr_info("Memory: %lldM/%lldM mirrored memory\n",
			mirror_size>>20, total_size>>20);
}

P
Peter Jones 已提交
475 476
/*
 * Find the efi memory descriptor for a given physical address.  Given a
477
 * physical address, determine if it exists within an EFI Memory Map entry,
P
Peter Jones 已提交
478 479 480
 * and if so, populate the supplied memory descriptor with the appropriate
 * data.
 */
481
int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
P
Peter Jones 已提交
482
{
483
	efi_memory_desc_t *md;
P
Peter Jones 已提交
484 485 486 487 488 489 490 491 492 493 494

	if (!efi_enabled(EFI_MEMMAP)) {
		pr_err_once("EFI_MEMMAP is not enabled.\n");
		return -EINVAL;
	}

	if (!out_md) {
		pr_err_once("out_md is null.\n");
		return -EINVAL;
        }

495
	for_each_efi_memory_desc(md) {
P
Peter Jones 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
		u64 size;
		u64 end;

		size = md->num_pages << EFI_PAGE_SHIFT;
		end = md->phys_addr + size;
		if (phys_addr >= md->phys_addr && phys_addr < end) {
			memcpy(out_md, md, sizeof(*out_md));
			return 0;
		}
	}
	return -ENOENT;
}

/*
 * Calculate the highest address of an efi memory descriptor.
 */
u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
{
	u64 size = md->num_pages << EFI_PAGE_SHIFT;
	u64 end = md->phys_addr + size;
	return end;
}
518

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}

/**
 * efi_mem_reserve - Reserve an EFI memory region
 * @addr: Physical address to reserve
 * @size: Size of reservation
 *
 * Mark a region as reserved from general kernel allocation and
 * prevent it being released by efi_free_boot_services().
 *
 * This function should be called drivers once they've parsed EFI
 * configuration tables to figure out where their data lives, e.g.
 * efi_esrt_init().
 */
void __init efi_mem_reserve(phys_addr_t addr, u64 size)
{
	if (!memblock_is_region_reserved(addr, size))
		memblock_reserve(addr, size);

	/*
	 * Some architectures (x86) reserve all boot services ranges
	 * until efi_free_boot_services() because of buggy firmware
	 * implementations. This means the above memblock_reserve() is
	 * superfluous on x86 and instead what it needs to do is
	 * ensure the @start, @size is not freed.
	 */
	efi_arch_mem_reserve(addr, size);
}

548
static const efi_config_table_type_t common_tables[] __initconst = {
549 550 551 552 553 554 555 556 557 558
	{ACPI_20_TABLE_GUID,			&efi.acpi20,		"ACPI 2.0"	},
	{ACPI_TABLE_GUID,			&efi.acpi,		"ACPI"		},
	{SMBIOS_TABLE_GUID,			&efi.smbios,		"SMBIOS"	},
	{SMBIOS3_TABLE_GUID,			&efi.smbios3,		"SMBIOS 3.0"	},
	{EFI_SYSTEM_RESOURCE_TABLE_GUID,	&efi.esrt,		"ESRT"		},
	{EFI_MEMORY_ATTRIBUTES_TABLE_GUID,	&efi_mem_attr_table,	"MEMATTR"	},
	{LINUX_EFI_RANDOM_SEED_TABLE_GUID,	&efi_rng_seed,		"RNG"		},
	{LINUX_EFI_TPM_EVENT_LOG_GUID,		&efi.tpm_log,		"TPMEventLog"	},
	{LINUX_EFI_TPM_FINAL_LOG_GUID,		&efi.tpm_final_log,	"TPMFinalLog"	},
	{LINUX_EFI_MEMRESERVE_TABLE_GUID,	&mem_reserve,		"MEMRESERVE"	},
559
	{LINUX_EFI_INITRD_MEDIA_GUID,		&initrd,		"INITRD"	},
560
	{EFI_RT_PROPERTIES_TABLE_GUID,		&rt_prop,		"RTPROP"	},
561
#ifdef CONFIG_EFI_RCI2_TABLE
562
	{DELLEMC_EFI_RCI2_TABLE_GUID,		&rci2_table_phys			},
563 564 565
#endif
#ifdef CONFIG_LOAD_UEFI_KEYS
	{LINUX_EFI_MOK_VARIABLE_TABLE_GUID,	&efi.mokvar_table,	"MOKvar"	},
566 567 568
#endif
#ifdef CONFIG_EFI_COCO_SECRET
	{LINUX_EFI_COCO_SECRET_AREA_GUID,	&efi.coco_secret,	"CocoSecret"	},
569 570 571
#endif
#ifdef CONFIG_EFI_GENERIC_STUB
	{LINUX_EFI_SCREEN_INFO_TABLE_GUID,	&screen_info_table			},
572
#endif
573
	{},
574 575
};

576
static __init int match_config_table(const efi_guid_t *guid,
577
				     unsigned long table,
578
				     const efi_config_table_type_t *table_types)
579 580 581
{
	int i;

582 583 584 585 586 587 588
	for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
		if (!efi_guidcmp(*guid, table_types[i].guid)) {
			*(table_types[i].ptr) = table;
			if (table_types[i].name[0])
				pr_cont("%s=0x%lx ",
					table_types[i].name, table);
			return 1;
589 590 591 592 593 594
		}
	}

	return 0;
}

595 596 597
int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
				   int count,
				   const efi_config_table_type_t *arch_tables)
598
{
599 600 601 602
	const efi_config_table_64_t *tbl64 = (void *)config_tables;
	const efi_config_table_32_t *tbl32 = (void *)config_tables;
	const efi_guid_t *guid;
	unsigned long table;
603
	int i;
604 605

	pr_info("");
606
	for (i = 0; i < count; i++) {
607 608 609 610 611 612 613 614 615
		if (!IS_ENABLED(CONFIG_X86)) {
			guid = &config_tables[i].guid;
			table = (unsigned long)config_tables[i].table;
		} else if (efi_enabled(EFI_64BIT)) {
			guid = &tbl64[i].guid;
			table = tbl64[i].table;

			if (IS_ENABLED(CONFIG_X86_32) &&
			    tbl64[i].table > U32_MAX) {
616 617 618 619 620
				pr_cont("\n");
				pr_err("Table located above 4GB, disabling EFI.\n");
				return -EINVAL;
			}
		} else {
621 622
			guid = &tbl32[i].guid;
			table = tbl32[i].table;
623 624
		}

625
		if (!match_config_table(guid, table, common_tables) && arch_tables)
626
			match_config_table(guid, table, arch_tables);
627 628
	}
	pr_cont("\n");
629
	set_bit(EFI_CONFIG_TABLES, &efi.flags);
630

631
	if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
632 633 634
		struct linux_efi_random_seed *seed;
		u32 size = 0;

635
		seed = early_memremap(efi_rng_seed, sizeof(*seed));
636
		if (seed != NULL) {
637
			size = min_t(u32, seed->size, SZ_1K); // sanity check
638 639 640 641 642
			early_memunmap(seed, sizeof(*seed));
		} else {
			pr_err("Could not map UEFI random seed!\n");
		}
		if (size > 0) {
643 644
			seed = early_memremap(efi_rng_seed,
					      sizeof(*seed) + size);
645
			if (seed != NULL) {
646
				add_bootloader_randomness(seed->bits, size);
647
				memzero_explicit(seed->bits, size);
648 649 650 651 652 653 654
				early_memunmap(seed, sizeof(*seed) + size);
			} else {
				pr_err("Could not map UEFI random seed!\n");
			}
		}
	}

655
	if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
656
		efi_memattr_init();
657

658 659
	efi_tpm_eventlog_init();

660 661
	if (mem_reserve != EFI_INVALID_TABLE_ADDR) {
		unsigned long prsv = mem_reserve;
662 663 664

		while (prsv) {
			struct linux_efi_memreserve *rsv;
665 666 667 668 669 670 671 672 673 674
			u8 *p;

			/*
			 * Just map a full page: that is what we will get
			 * anyway, and it permits us to map the entire entry
			 * before knowing its size.
			 */
			p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
					   PAGE_SIZE);
			if (p == NULL) {
675 676 677 678
				pr_err("Could not map UEFI memreserve entry!\n");
				return -ENOMEM;
			}

679 680 681
			rsv = (void *)(p + prsv % PAGE_SIZE);

			/* reserve the entry itself */
682 683
			memblock_reserve(prsv,
					 struct_size(rsv, entry, rsv->size));
684 685 686 687 688

			for (i = 0; i < atomic_read(&rsv->count); i++) {
				memblock_reserve(rsv->entry[i].base,
						 rsv->entry[i].size);
			}
689 690

			prsv = rsv->next;
691
			early_memunmap(p, PAGE_SIZE);
692 693 694
		}
	}

695 696 697 698 699 700 701 702 703 704
	if (rt_prop != EFI_INVALID_TABLE_ADDR) {
		efi_rt_properties_table_t *tbl;

		tbl = early_memremap(rt_prop, sizeof(*tbl));
		if (tbl) {
			efi.runtime_supported_mask &= tbl->runtime_services_supported;
			early_memunmap(tbl, sizeof(*tbl));
		}
	}

705 706 707 708 709 710 711 712 713 714 715 716
	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
	    initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) {
		struct linux_efi_initrd *tbl;

		tbl = early_memremap(initrd, sizeof(*tbl));
		if (tbl) {
			phys_initrd_start = tbl->base;
			phys_initrd_size = tbl->size;
			early_memunmap(tbl, sizeof(*tbl));
		}
	}

717 718
	return 0;
}
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr,
				   int min_major_version)
{
	if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) {
		pr_err("System table signature incorrect!\n");
		return -EINVAL;
	}

	if ((systab_hdr->revision >> 16) < min_major_version)
		pr_err("Warning: System table version %d.%02d, expected %d.00 or greater!\n",
		       systab_hdr->revision >> 16,
		       systab_hdr->revision & 0xffff,
		       min_major_version);

	return 0;
}

#ifndef CONFIG_IA64
static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor,
						size_t size)
{
	const efi_char16_t *ret;

	ret = early_memremap_ro(fw_vendor, size);
	if (!ret)
		pr_err("Could not map the firmware vendor!\n");
	return ret;
}

static void __init unmap_fw_vendor(const void *fw_vendor, size_t size)
{
	early_memunmap((void *)fw_vendor, size);
}
#else
#define map_fw_vendor(p, s)	__va(p)
#define unmap_fw_vendor(v, s)
#endif

void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
				     unsigned long fw_vendor)
{
	char vendor[100] = "unknown";
	const efi_char16_t *c16;
	size_t i;

	c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t));
	if (c16) {
		for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
			vendor[i] = c16[i];
		vendor[i] = '\0';

		unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t));
	}

	pr_info("EFI v%u.%.02u by %s\n",
		systab_hdr->revision >> 16,
		systab_hdr->revision & 0xffff,
		vendor);
778 779 780 781 782 783 784

	if (IS_ENABLED(CONFIG_X86_64) &&
	    systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION &&
	    !strcmp(vendor, "Apple")) {
		pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n");
		efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION;
	}
785 786
}

787
static __initdata char memory_type_name[][13] = {
788 789 790 791 792 793 794
	"Reserved",
	"Loader Code",
	"Loader Data",
	"Boot Code",
	"Boot Data",
	"Runtime Code",
	"Runtime Data",
795 796 797 798 799 800
	"Conventional",
	"Unusable",
	"ACPI Reclaim",
	"ACPI Mem NVS",
	"MMIO",
	"MMIO Port",
801
	"PAL Code",
802
	"Persistent",
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
};

char * __init efi_md_typeattr_format(char *buf, size_t size,
				     const efi_memory_desc_t *md)
{
	char *pos;
	int type_len;
	u64 attr;

	pos = buf;
	if (md->type >= ARRAY_SIZE(memory_type_name))
		type_len = snprintf(pos, size, "[type=%u", md->type);
	else
		type_len = snprintf(pos, size, "[%-*s",
				    (int)(sizeof(memory_type_name[0]) - 1),
				    memory_type_name[md->type]);
	if (type_len >= size)
		return buf;

	pos += type_len;
	size -= type_len;

	attr = md->attribute;
	if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
827 828
		     EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
		     EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
829
		     EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
830
		     EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
831 832 833
		snprintf(pos, size, "|attr=0x%016llx]",
			 (unsigned long long)attr);
	else
R
Robert Elliott 已提交
834
		snprintf(pos, size,
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
			 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
			 attr & EFI_MEMORY_RUNTIME		? "RUN" : "",
			 attr & EFI_MEMORY_MORE_RELIABLE	? "MR"  : "",
			 attr & EFI_MEMORY_CPU_CRYPTO   	? "CC"  : "",
			 attr & EFI_MEMORY_SP			? "SP"  : "",
			 attr & EFI_MEMORY_NV			? "NV"  : "",
			 attr & EFI_MEMORY_XP			? "XP"  : "",
			 attr & EFI_MEMORY_RP			? "RP"  : "",
			 attr & EFI_MEMORY_WP			? "WP"  : "",
			 attr & EFI_MEMORY_RO			? "RO"  : "",
			 attr & EFI_MEMORY_UCE			? "UCE" : "",
			 attr & EFI_MEMORY_WB			? "WB"  : "",
			 attr & EFI_MEMORY_WT			? "WT"  : "",
			 attr & EFI_MEMORY_WC			? "WC"  : "",
			 attr & EFI_MEMORY_UC			? "UC"  : "");
850 851
	return buf;
}
852

853 854 855 856 857
/*
 * IA64 has a funky EFI memory map that doesn't work the same way as
 * other architectures.
 */
#ifndef CONFIG_IA64
858 859 860 861 862 863 864 865
/*
 * efi_mem_attributes - lookup memmap attributes for physical address
 * @phys_addr: the physical address to lookup
 *
 * Search in the EFI memory map for the region covering
 * @phys_addr. Returns the EFI memory attributes if the region
 * was found in the memory map, 0 otherwise.
 */
866
u64 efi_mem_attributes(unsigned long phys_addr)
867 868 869 870 871 872
{
	efi_memory_desc_t *md;

	if (!efi_enabled(EFI_MEMMAP))
		return 0;

873
	for_each_efi_memory_desc(md) {
874 875 876 877 878 879 880
		if ((md->phys_addr <= phys_addr) &&
		    (phys_addr < (md->phys_addr +
		    (md->num_pages << EFI_PAGE_SHIFT))))
			return md->attribute;
	}
	return 0;
}
881

882 883 884 885 886 887
/*
 * efi_mem_type - lookup memmap type for physical address
 * @phys_addr: the physical address to lookup
 *
 * Search in the EFI memory map for the region covering @phys_addr.
 * Returns the EFI memory type if the region was found in the memory
888
 * map, -EINVAL otherwise.
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
 */
int efi_mem_type(unsigned long phys_addr)
{
	const efi_memory_desc_t *md;

	if (!efi_enabled(EFI_MEMMAP))
		return -ENOTSUPP;

	for_each_efi_memory_desc(md) {
		if ((md->phys_addr <= phys_addr) &&
		    (phys_addr < (md->phys_addr +
				  (md->num_pages << EFI_PAGE_SHIFT))))
			return md->type;
	}
	return -EINVAL;
}
#endif

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
int efi_status_to_err(efi_status_t status)
{
	int err;

	switch (status) {
	case EFI_SUCCESS:
		err = 0;
		break;
	case EFI_INVALID_PARAMETER:
		err = -EINVAL;
		break;
	case EFI_OUT_OF_RESOURCES:
		err = -ENOSPC;
		break;
	case EFI_DEVICE_ERROR:
		err = -EIO;
		break;
	case EFI_WRITE_PROTECTED:
		err = -EROFS;
		break;
	case EFI_SECURITY_VIOLATION:
		err = -EACCES;
		break;
	case EFI_NOT_FOUND:
		err = -ENOENT;
		break;
933 934 935
	case EFI_ABORTED:
		err = -EINTR;
		break;
936 937 938 939 940
	default:
		err = -EINVAL;
	}

	return err;
941
}
942
EXPORT_SYMBOL_GPL(efi_status_to_err);
943

944
static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
945
static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
946

947 948
static int __init efi_memreserve_map_root(void)
{
949
	if (mem_reserve == EFI_INVALID_TABLE_ADDR)
950 951
		return -ENODEV;

952
	efi_memreserve_root = memremap(mem_reserve,
953 954 955 956 957 958 959
				       sizeof(*efi_memreserve_root),
				       MEMREMAP_WB);
	if (WARN_ON_ONCE(!efi_memreserve_root))
		return -ENOMEM;
	return 0;
}

960 961 962
static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
{
	struct resource *res, *parent;
963
	int ret;
964 965 966 967 968 969 970 971 972 973 974 975

	res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
	if (!res)
		return -ENOMEM;

	res->name	= "reserved";
	res->flags	= IORESOURCE_MEM;
	res->start	= addr;
	res->end	= addr + size - 1;

	/* we expect a conflict with a 'System RAM' region */
	parent = request_resource_conflict(&iomem_resource, res);
976 977 978 979 980 981 982 983 984 985 986
	ret = parent ? request_resource(parent, res) : 0;

	/*
	 * Given that efi_mem_reserve_iomem() can be called at any
	 * time, only call memblock_reserve() if the architecture
	 * keeps the infrastructure around.
	 */
	if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
		memblock_reserve(addr, size);

	return ret;
987 988
}

989
int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
990
{
991
	struct linux_efi_memreserve *rsv;
992 993
	unsigned long prsv;
	int rc, index;
994

995
	if (efi_memreserve_root == (void *)ULONG_MAX)
996 997
		return -ENODEV;

998 999 1000 1001 1002 1003
	if (!efi_memreserve_root) {
		rc = efi_memreserve_map_root();
		if (rc)
			return rc;
	}

1004
	/* first try to find a slot in an existing linked list entry */
1005
	for (prsv = efi_memreserve_root->next; prsv; ) {
1006
		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
1007 1008 1009 1010 1011
		index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
		if (index < rsv->size) {
			rsv->entry[index].base = addr;
			rsv->entry[index].size = size;

1012
			memunmap(rsv);
1013
			return efi_mem_reserve_iomem(addr, size);
1014
		}
1015
		prsv = rsv->next;
1016
		memunmap(rsv);
1017 1018 1019 1020
	}

	/* no slot found - allocate a new linked list entry */
	rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
1021 1022 1023
	if (!rsv)
		return -ENOMEM;

1024 1025 1026 1027 1028 1029
	rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
	if (rc) {
		free_page((unsigned long)rsv);
		return rc;
	}

1030 1031 1032 1033 1034 1035 1036
	/*
	 * The memremap() call above assumes that a linux_efi_memreserve entry
	 * never crosses a page boundary, so let's ensure that this remains true
	 * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by
	 * using SZ_4K explicitly in the size calculation below.
	 */
	rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
1037 1038 1039
	atomic_set(&rsv->count, 1);
	rsv->entry[0].base = addr;
	rsv->entry[0].size = size;
1040 1041

	spin_lock(&efi_mem_reserve_persistent_lock);
1042 1043
	rsv->next = efi_memreserve_root->next;
	efi_memreserve_root->next = __pa(rsv);
1044 1045
	spin_unlock(&efi_mem_reserve_persistent_lock);

1046
	return efi_mem_reserve_iomem(addr, size);
1047
}
1048

1049 1050
static int __init efi_memreserve_root_init(void)
{
1051 1052 1053 1054
	if (efi_memreserve_root)
		return 0;
	if (efi_memreserve_map_root())
		efi_memreserve_root = (void *)ULONG_MAX;
1055 1056
	return 0;
}
1057
early_initcall(efi_memreserve_root_init);
1058

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
#ifdef CONFIG_KEXEC
static int update_efi_random_seed(struct notifier_block *nb,
				  unsigned long code, void *unused)
{
	struct linux_efi_random_seed *seed;
	u32 size = 0;

	if (!kexec_in_progress)
		return NOTIFY_DONE;

1069
	seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB);
1070
	if (seed != NULL) {
1071
		size = min(seed->size, EFI_RANDOM_SEED_SIZE);
1072 1073 1074 1075 1076
		memunmap(seed);
	} else {
		pr_err("Could not map UEFI random seed!\n");
	}
	if (size > 0) {
1077 1078
		seed = memremap(efi_rng_seed, sizeof(*seed) + size,
				MEMREMAP_WB);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
		if (seed != NULL) {
			seed->size = size;
			get_random_bytes(seed->bits, seed->size);
			memunmap(seed);
		} else {
			pr_err("Could not map UEFI random seed!\n");
		}
	}
	return NOTIFY_DONE;
}

static struct notifier_block efi_random_seed_nb = {
	.notifier_call = update_efi_random_seed,
};

1094
static int __init register_update_efi_random_seed(void)
1095
{
1096
	if (efi_rng_seed == EFI_INVALID_TABLE_ADDR)
1097 1098 1099 1100 1101
		return 0;
	return register_reboot_notifier(&efi_random_seed_nb);
}
late_initcall(register_update_efi_random_seed);
#endif