forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/firmware/efi/efi.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * efi.c - EFI subsystem
34 *
....@@ -9,8 +10,6 @@
910 * allowing the efivarfs to be mounted or the efivars module to be loaded.
1011 * The existance of /sys/firmware/efi may also be used by userspace to
1112 * determine that the system supports EFI.
12
- *
13
- * This file is released under the GPLv2.
1413 */
1514
1615 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -18,10 +17,10 @@
1817 #include <linux/kobject.h>
1918 #include <linux/module.h>
2019 #include <linux/init.h>
20
+#include <linux/debugfs.h>
2121 #include <linux/device.h>
2222 #include <linux/efi.h>
2323 #include <linux/of.h>
24
-#include <linux/of_fdt.h>
2524 #include <linux/io.h>
2625 #include <linux/kexec.h>
2726 #include <linux/platform_device.h>
....@@ -31,55 +30,35 @@
3130 #include <linux/acpi.h>
3231 #include <linux/ucs2_string.h>
3332 #include <linux/memblock.h>
33
+#include <linux/security.h>
3434
3535 #include <asm/early_ioremap.h>
3636
3737 struct efi __read_mostly efi = {
38
- .mps = EFI_INVALID_TABLE_ADDR,
38
+ .runtime_supported_mask = EFI_RT_SUPPORTED_ALL,
3939 .acpi = EFI_INVALID_TABLE_ADDR,
4040 .acpi20 = EFI_INVALID_TABLE_ADDR,
4141 .smbios = EFI_INVALID_TABLE_ADDR,
4242 .smbios3 = EFI_INVALID_TABLE_ADDR,
43
- .sal_systab = EFI_INVALID_TABLE_ADDR,
44
- .boot_info = EFI_INVALID_TABLE_ADDR,
45
- .hcdp = EFI_INVALID_TABLE_ADDR,
46
- .uga = EFI_INVALID_TABLE_ADDR,
47
- .uv_systab = EFI_INVALID_TABLE_ADDR,
48
- .fw_vendor = EFI_INVALID_TABLE_ADDR,
49
- .runtime = EFI_INVALID_TABLE_ADDR,
50
- .config_table = EFI_INVALID_TABLE_ADDR,
5143 .esrt = EFI_INVALID_TABLE_ADDR,
52
- .properties_table = EFI_INVALID_TABLE_ADDR,
53
- .mem_attr_table = EFI_INVALID_TABLE_ADDR,
54
- .rng_seed = EFI_INVALID_TABLE_ADDR,
55
- .tpm_log = EFI_INVALID_TABLE_ADDR
44
+ .tpm_log = EFI_INVALID_TABLE_ADDR,
45
+ .tpm_final_log = EFI_INVALID_TABLE_ADDR,
46
+#ifdef CONFIG_LOAD_UEFI_KEYS
47
+ .mokvar_table = EFI_INVALID_TABLE_ADDR,
48
+#endif
5649 };
5750 EXPORT_SYMBOL(efi);
5851
59
-static unsigned long *efi_tables[] = {
60
- &efi.mps,
61
- &efi.acpi,
62
- &efi.acpi20,
63
- &efi.smbios,
64
- &efi.smbios3,
65
- &efi.sal_systab,
66
- &efi.boot_info,
67
- &efi.hcdp,
68
- &efi.uga,
69
- &efi.uv_systab,
70
- &efi.fw_vendor,
71
- &efi.runtime,
72
- &efi.config_table,
73
- &efi.esrt,
74
- &efi.properties_table,
75
- &efi.mem_attr_table,
76
-};
52
+unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;
53
+static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
54
+static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
7755
7856 struct mm_struct efi_mm = {
7957 .mm_rb = RB_ROOT,
8058 .mm_users = ATOMIC_INIT(2),
8159 .mm_count = ATOMIC_INIT(1),
82
- .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
60
+ .write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq),
61
+ MMAP_LOCK_INITIALIZER(efi_mm)
8362 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
8463 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
8564 .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0},
....@@ -100,6 +79,11 @@
10079 return disable_runtime;
10180 }
10281
82
+bool __pure __efi_soft_reserve_enabled(void)
83
+{
84
+ return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE);
85
+}
86
+
10387 static int __init parse_efi_cmdline(char *str)
10488 {
10589 if (!str) {
....@@ -112,6 +96,9 @@
11296
11397 if (parse_option_str(str, "noruntime"))
11498 disable_runtime = true;
99
+
100
+ if (parse_option_str(str, "nosoftreserve"))
101
+ set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags);
115102
116103 return 0;
117104 }
....@@ -133,8 +120,6 @@
133120 if (!kobj || !buf)
134121 return -EINVAL;
135122
136
- if (efi.mps != EFI_INVALID_TABLE_ADDR)
137
- str += sprintf(str, "MPS=0x%lx\n", efi.mps);
138123 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
139124 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
140125 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
....@@ -148,30 +133,14 @@
148133 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
149134 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
150135 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
151
- if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
152
- str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
153
- if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
154
- str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
155
- if (efi.uga != EFI_INVALID_TABLE_ADDR)
156
- str += sprintf(str, "UGA=0x%lx\n", efi.uga);
136
+
137
+ if (IS_ENABLED(CONFIG_IA64) || IS_ENABLED(CONFIG_X86))
138
+ str = efi_systab_show_arch(str);
157139
158140 return str - buf;
159141 }
160142
161143 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
162
-
163
-#define EFI_FIELD(var) efi.var
164
-
165
-#define EFI_ATTR_SHOW(name) \
166
-static ssize_t name##_show(struct kobject *kobj, \
167
- struct kobj_attribute *attr, char *buf) \
168
-{ \
169
- return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
170
-}
171
-
172
-EFI_ATTR_SHOW(fw_vendor);
173
-EFI_ATTR_SHOW(runtime);
174
-EFI_ATTR_SHOW(config_table);
175144
176145 static ssize_t fw_platform_size_show(struct kobject *kobj,
177146 struct kobj_attribute *attr, char *buf)
....@@ -179,36 +148,24 @@
179148 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
180149 }
181150
182
-static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
183
-static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
184
-static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
151
+extern __weak struct kobj_attribute efi_attr_fw_vendor;
152
+extern __weak struct kobj_attribute efi_attr_runtime;
153
+extern __weak struct kobj_attribute efi_attr_config_table;
185154 static struct kobj_attribute efi_attr_fw_platform_size =
186155 __ATTR_RO(fw_platform_size);
187156
188157 static struct attribute *efi_subsys_attrs[] = {
189158 &efi_attr_systab.attr,
159
+ &efi_attr_fw_platform_size.attr,
190160 &efi_attr_fw_vendor.attr,
191161 &efi_attr_runtime.attr,
192162 &efi_attr_config_table.attr,
193
- &efi_attr_fw_platform_size.attr,
194163 NULL,
195164 };
196165
197
-static umode_t efi_attr_is_visible(struct kobject *kobj,
198
- struct attribute *attr, int n)
166
+umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr,
167
+ int n)
199168 {
200
- if (attr == &efi_attr_fw_vendor.attr) {
201
- if (efi_enabled(EFI_PARAVIRT) ||
202
- efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
203
- return 0;
204
- } else if (attr == &efi_attr_runtime.attr) {
205
- if (efi.runtime == EFI_INVALID_TABLE_ADDR)
206
- return 0;
207
- } else if (attr == &efi_attr_config_table.attr) {
208
- if (efi.config_table == EFI_INVALID_TABLE_ADDR)
209
- return 0;
210
- }
211
-
212169 return attr->mode;
213170 }
214171
....@@ -223,11 +180,13 @@
223180 static int generic_ops_register(void)
224181 {
225182 generic_ops.get_variable = efi.get_variable;
226
- generic_ops.set_variable = efi.set_variable;
227
- generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
228183 generic_ops.get_next_variable = efi.get_next_variable;
229184 generic_ops.query_variable_store = efi_query_variable_store;
230185
186
+ if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) {
187
+ generic_ops.set_variable = efi.set_variable;
188
+ generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
189
+ }
231190 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
232191 }
233192
....@@ -241,11 +200,16 @@
241200 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
242201 static int __init efivar_ssdt_setup(char *str)
243202 {
203
+ int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
204
+
205
+ if (ret)
206
+ return ret;
207
+
244208 if (strlen(str) < sizeof(efivar_ssdt))
245209 memcpy(efivar_ssdt, str, strlen(str));
246210 else
247211 pr_warn("efivar_ssdt: name too long: %s\n", str);
248
- return 0;
212
+ return 1;
249213 }
250214 __setup("efivar_ssdt=", efivar_ssdt_setup);
251215
....@@ -310,7 +274,7 @@
310274 goto free_data;
311275 }
312276
313
- ret = acpi_load_table(data);
277
+ ret = acpi_load_table(data, NULL);
314278 if (ret) {
315279 pr_err("failed to load table: %d\n", ret);
316280 goto free_data;
....@@ -331,6 +295,59 @@
331295 static inline int efivar_ssdt_load(void) { return 0; }
332296 #endif
333297
298
+#ifdef CONFIG_DEBUG_FS
299
+
300
+#define EFI_DEBUGFS_MAX_BLOBS 32
301
+
302
+static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];
303
+
304
+static void __init efi_debugfs_init(void)
305
+{
306
+ struct dentry *efi_debugfs;
307
+ efi_memory_desc_t *md;
308
+ char name[32];
309
+ int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
310
+ int i = 0;
311
+
312
+ efi_debugfs = debugfs_create_dir("efi", NULL);
313
+ if (IS_ERR_OR_NULL(efi_debugfs))
314
+ return;
315
+
316
+ for_each_efi_memory_desc(md) {
317
+ switch (md->type) {
318
+ case EFI_BOOT_SERVICES_CODE:
319
+ snprintf(name, sizeof(name), "boot_services_code%d",
320
+ type_count[md->type]++);
321
+ break;
322
+ case EFI_BOOT_SERVICES_DATA:
323
+ snprintf(name, sizeof(name), "boot_services_data%d",
324
+ type_count[md->type]++);
325
+ break;
326
+ default:
327
+ continue;
328
+ }
329
+
330
+ if (i >= EFI_DEBUGFS_MAX_BLOBS) {
331
+ pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
332
+ EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
333
+ break;
334
+ }
335
+
336
+ debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
337
+ debugfs_blob[i].data = memremap(md->phys_addr,
338
+ debugfs_blob[i].size,
339
+ MEMREMAP_WB);
340
+ if (!debugfs_blob[i].data)
341
+ continue;
342
+
343
+ debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
344
+ i++;
345
+ }
346
+}
347
+#else
348
+static inline void efi_debugfs_init(void) {}
349
+#endif
350
+
334351 /*
335352 * We register the efi subsystem with the firmware subsystem and the
336353 * efivars subsystem with the efi subsystem, if the system was booted with
....@@ -340,35 +357,46 @@
340357 {
341358 int error;
342359
360
+ if (!efi_enabled(EFI_RUNTIME_SERVICES))
361
+ efi.runtime_supported_mask = 0;
362
+
343363 if (!efi_enabled(EFI_BOOT))
344364 return 0;
345365
346
- /*
347
- * Since we process only one efi_runtime_service() at a time, an
348
- * ordered workqueue (which creates only one execution context)
349
- * should suffice all our needs.
350
- */
351
- efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
352
- if (!efi_rts_wq) {
353
- pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
354
- clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
355
- return 0;
366
+ if (efi.runtime_supported_mask) {
367
+ /*
368
+ * Since we process only one efi_runtime_service() at a time, an
369
+ * ordered workqueue (which creates only one execution context)
370
+ * should suffice for all our needs.
371
+ */
372
+ efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
373
+ if (!efi_rts_wq) {
374
+ pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
375
+ clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
376
+ efi.runtime_supported_mask = 0;
377
+ return 0;
378
+ }
356379 }
380
+
381
+ if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES))
382
+ platform_device_register_simple("rtc-efi", 0, NULL, 0);
357383
358384 /* We register the efi directory at /sys/firmware/efi */
359385 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
360386 if (!efi_kobj) {
361387 pr_err("efi: Firmware registration failed.\n");
362
- destroy_workqueue(efi_rts_wq);
363
- return -ENOMEM;
388
+ error = -ENOMEM;
389
+ goto err_destroy_wq;
364390 }
365391
366
- error = generic_ops_register();
367
- if (error)
368
- goto err_put;
369
-
370
- if (efi_enabled(EFI_RUNTIME_SERVICES))
392
+ if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
393
+ EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) {
394
+ error = generic_ops_register();
395
+ if (error)
396
+ goto err_put;
371397 efivar_ssdt_load();
398
+ platform_device_register_simple("efivars", 0, NULL, 0);
399
+ }
372400
373401 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
374402 if (error) {
....@@ -388,15 +416,23 @@
388416 goto err_remove_group;
389417 }
390418
419
+ if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
420
+ efi_debugfs_init();
421
+
391422 return 0;
392423
393424 err_remove_group:
394425 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
395426 err_unregister:
396
- generic_ops_unregister();
427
+ if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
428
+ EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME))
429
+ generic_ops_unregister();
397430 err_put:
398431 kobject_put(efi_kobj);
399
- destroy_workqueue(efi_rts_wq);
432
+err_destroy_wq:
433
+ if (efi_rts_wq)
434
+ destroy_workqueue(efi_rts_wq);
435
+
400436 return error;
401437 }
402438
....@@ -475,98 +511,99 @@
475511 efi_arch_mem_reserve(addr, size);
476512 }
477513
478
-static __initdata efi_config_table_type_t common_tables[] = {
479
- {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
480
- {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
481
- {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
482
- {MPS_TABLE_GUID, "MPS", &efi.mps},
483
- {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
484
- {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
485
- {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
486
- {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
487
- {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
488
- {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
489
- {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
490
- {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
491
- {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log},
492
- {NULL_GUID, NULL, NULL},
514
+static const efi_config_table_type_t common_tables[] __initconst = {
515
+ {ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" },
516
+ {ACPI_TABLE_GUID, &efi.acpi, "ACPI" },
517
+ {SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" },
518
+ {SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" },
519
+ {EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" },
520
+ {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" },
521
+ {LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" },
522
+ {LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" },
523
+ {LINUX_EFI_TPM_FINAL_LOG_GUID, &efi.tpm_final_log, "TPMFinalLog" },
524
+ {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" },
525
+ {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" },
526
+#ifdef CONFIG_EFI_RCI2_TABLE
527
+ {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys },
528
+#endif
529
+#ifdef CONFIG_LOAD_UEFI_KEYS
530
+ {LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" },
531
+#endif
532
+ {},
493533 };
494534
495
-static __init int match_config_table(efi_guid_t *guid,
535
+static __init int match_config_table(const efi_guid_t *guid,
496536 unsigned long table,
497
- efi_config_table_type_t *table_types)
537
+ const efi_config_table_type_t *table_types)
498538 {
499539 int i;
500540
501
- if (table_types) {
502
- for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
503
- if (!efi_guidcmp(*guid, table_types[i].guid)) {
504
- *(table_types[i].ptr) = table;
505
- if (table_types[i].name)
506
- pr_cont(" %s=0x%lx ",
507
- table_types[i].name, table);
508
- return 1;
509
- }
541
+ for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
542
+ if (!efi_guidcmp(*guid, table_types[i].guid)) {
543
+ *(table_types[i].ptr) = table;
544
+ if (table_types[i].name[0])
545
+ pr_cont("%s=0x%lx ",
546
+ table_types[i].name, table);
547
+ return 1;
510548 }
511549 }
512550
513551 return 0;
514552 }
515553
516
-int __init efi_config_parse_tables(void *config_tables, int count, int sz,
517
- efi_config_table_type_t *arch_tables)
554
+int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
555
+ int count,
556
+ const efi_config_table_type_t *arch_tables)
518557 {
519
- void *tablep;
558
+ const efi_config_table_64_t *tbl64 = (void *)config_tables;
559
+ const efi_config_table_32_t *tbl32 = (void *)config_tables;
560
+ const efi_guid_t *guid;
561
+ unsigned long table;
520562 int i;
521563
522
- tablep = config_tables;
523564 pr_info("");
524565 for (i = 0; i < count; i++) {
525
- efi_guid_t guid;
526
- unsigned long table;
566
+ if (!IS_ENABLED(CONFIG_X86)) {
567
+ guid = &config_tables[i].guid;
568
+ table = (unsigned long)config_tables[i].table;
569
+ } else if (efi_enabled(EFI_64BIT)) {
570
+ guid = &tbl64[i].guid;
571
+ table = tbl64[i].table;
527572
528
- if (efi_enabled(EFI_64BIT)) {
529
- u64 table64;
530
- guid = ((efi_config_table_64_t *)tablep)->guid;
531
- table64 = ((efi_config_table_64_t *)tablep)->table;
532
- table = table64;
533
-#ifndef CONFIG_64BIT
534
- if (table64 >> 32) {
573
+ if (IS_ENABLED(CONFIG_X86_32) &&
574
+ tbl64[i].table > U32_MAX) {
535575 pr_cont("\n");
536576 pr_err("Table located above 4GB, disabling EFI.\n");
537577 return -EINVAL;
538578 }
539
-#endif
540579 } else {
541
- guid = ((efi_config_table_32_t *)tablep)->guid;
542
- table = ((efi_config_table_32_t *)tablep)->table;
580
+ guid = &tbl32[i].guid;
581
+ table = tbl32[i].table;
543582 }
544583
545
- if (!match_config_table(&guid, table, common_tables))
546
- match_config_table(&guid, table, arch_tables);
547
-
548
- tablep += sz;
584
+ if (!match_config_table(guid, table, common_tables) && arch_tables)
585
+ match_config_table(guid, table, arch_tables);
549586 }
550587 pr_cont("\n");
551588 set_bit(EFI_CONFIG_TABLES, &efi.flags);
552589
553
- if (efi.rng_seed != EFI_INVALID_TABLE_ADDR) {
590
+ if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
554591 struct linux_efi_random_seed *seed;
555592 u32 size = 0;
556593
557
- seed = early_memremap(efi.rng_seed, sizeof(*seed));
594
+ seed = early_memremap(efi_rng_seed, sizeof(*seed));
558595 if (seed != NULL) {
559
- size = seed->size;
596
+ size = min_t(u32, seed->size, SZ_1K); // sanity check
560597 early_memunmap(seed, sizeof(*seed));
561598 } else {
562599 pr_err("Could not map UEFI random seed!\n");
563600 }
564601 if (size > 0) {
565
- seed = early_memremap(efi.rng_seed,
602
+ seed = early_memremap(efi_rng_seed,
566603 sizeof(*seed) + size);
567604 if (seed != NULL) {
568
- pr_notice("seeding entropy pool\n");
569
- add_device_randomness(seed->bits, seed->size);
605
+ add_bootloader_randomness(seed->bits, size);
606
+ memzero_explicit(seed->bits, size);
570607 early_memunmap(seed, sizeof(*seed) + size);
571608 } else {
572609 pr_err("Could not map UEFI random seed!\n");
....@@ -579,202 +616,122 @@
579616
580617 efi_tpm_eventlog_init();
581618
582
- /* Parse the EFI Properties table if it exists */
583
- if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
584
- efi_properties_table_t *tbl;
619
+ if (mem_reserve != EFI_INVALID_TABLE_ADDR) {
620
+ unsigned long prsv = mem_reserve;
585621
586
- tbl = early_memremap(efi.properties_table, sizeof(*tbl));
587
- if (tbl == NULL) {
588
- pr_err("Could not map Properties table!\n");
589
- return -ENOMEM;
622
+ while (prsv) {
623
+ struct linux_efi_memreserve *rsv;
624
+ u8 *p;
625
+
626
+ /*
627
+ * Just map a full page: that is what we will get
628
+ * anyway, and it permits us to map the entire entry
629
+ * before knowing its size.
630
+ */
631
+ p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
632
+ PAGE_SIZE);
633
+ if (p == NULL) {
634
+ pr_err("Could not map UEFI memreserve entry!\n");
635
+ return -ENOMEM;
636
+ }
637
+
638
+ rsv = (void *)(p + prsv % PAGE_SIZE);
639
+
640
+ /* reserve the entry itself */
641
+ memblock_reserve(prsv,
642
+ struct_size(rsv, entry, rsv->size));
643
+
644
+ for (i = 0; i < atomic_read(&rsv->count); i++) {
645
+ memblock_reserve(rsv->entry[i].base,
646
+ rsv->entry[i].size);
647
+ }
648
+
649
+ prsv = rsv->next;
650
+ early_memunmap(p, PAGE_SIZE);
590651 }
652
+ }
591653
592
- if (tbl->memory_protection_attribute &
593
- EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
594
- set_bit(EFI_NX_PE_DATA, &efi.flags);
654
+ if (rt_prop != EFI_INVALID_TABLE_ADDR) {
655
+ efi_rt_properties_table_t *tbl;
595656
596
- early_memunmap(tbl, sizeof(*tbl));
657
+ tbl = early_memremap(rt_prop, sizeof(*tbl));
658
+ if (tbl) {
659
+ efi.runtime_supported_mask &= tbl->runtime_services_supported;
660
+ early_memunmap(tbl, sizeof(*tbl));
661
+ }
597662 }
598663
599664 return 0;
600665 }
601666
602
-int __init efi_config_init(efi_config_table_type_t *arch_tables)
667
+int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr,
668
+ int min_major_version)
603669 {
604
- void *config_tables;
605
- int sz, ret;
606
-
607
- if (efi_enabled(EFI_64BIT))
608
- sz = sizeof(efi_config_table_64_t);
609
- else
610
- sz = sizeof(efi_config_table_32_t);
611
-
612
- /*
613
- * Let's see what config tables the firmware passed to us.
614
- */
615
- config_tables = early_memremap(efi.systab->tables,
616
- efi.systab->nr_tables * sz);
617
- if (config_tables == NULL) {
618
- pr_err("Could not map Configuration table!\n");
619
- return -ENOMEM;
670
+ if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) {
671
+ pr_err("System table signature incorrect!\n");
672
+ return -EINVAL;
620673 }
621674
622
- ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
623
- arch_tables);
675
+ if ((systab_hdr->revision >> 16) < min_major_version)
676
+ pr_err("Warning: System table version %d.%02d, expected %d.00 or greater!\n",
677
+ systab_hdr->revision >> 16,
678
+ systab_hdr->revision & 0xffff,
679
+ min_major_version);
624680
625
- early_memunmap(config_tables, efi.systab->nr_tables * sz);
681
+ return 0;
682
+}
683
+
684
+#ifndef CONFIG_IA64
685
+static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor,
686
+ size_t size)
687
+{
688
+ const efi_char16_t *ret;
689
+
690
+ ret = early_memremap_ro(fw_vendor, size);
691
+ if (!ret)
692
+ pr_err("Could not map the firmware vendor!\n");
626693 return ret;
627694 }
628695
629
-#ifdef CONFIG_EFI_VARS_MODULE
630
-static int __init efi_load_efivars(void)
696
+static void __init unmap_fw_vendor(const void *fw_vendor, size_t size)
631697 {
632
- struct platform_device *pdev;
633
-
634
- if (!efi_enabled(EFI_RUNTIME_SERVICES))
635
- return 0;
636
-
637
- pdev = platform_device_register_simple("efivars", 0, NULL, 0);
638
- return PTR_ERR_OR_ZERO(pdev);
698
+ early_memunmap((void *)fw_vendor, size);
639699 }
640
-device_initcall(efi_load_efivars);
700
+#else
701
+#define map_fw_vendor(p, s) __va(p)
702
+#define unmap_fw_vendor(v, s)
641703 #endif
642704
643
-#ifdef CONFIG_EFI_PARAMS_FROM_FDT
705
+void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
706
+ unsigned long fw_vendor)
707
+{
708
+ char vendor[100] = "unknown";
709
+ const efi_char16_t *c16;
710
+ size_t i;
644711
645
-#define UEFI_PARAM(name, prop, field) \
646
- { \
647
- { name }, \
648
- { prop }, \
649
- offsetof(struct efi_fdt_params, field), \
650
- FIELD_SIZEOF(struct efi_fdt_params, field) \
712
+ c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t));
713
+ if (c16) {
714
+ for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
715
+ vendor[i] = c16[i];
716
+ vendor[i] = '\0';
717
+
718
+ unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t));
651719 }
652720
653
-struct params {
654
- const char name[32];
655
- const char propname[32];
656
- int offset;
657
- int size;
658
-};
721
+ pr_info("EFI v%u.%.02u by %s\n",
722
+ systab_hdr->revision >> 16,
723
+ systab_hdr->revision & 0xffff,
724
+ vendor);
659725
660
-static __initdata struct params fdt_params[] = {
661
- UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
662
- UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
663
- UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
664
- UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
665
- UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
666
-};
667
-
668
-static __initdata struct params xen_fdt_params[] = {
669
- UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
670
- UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
671
- UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
672
- UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
673
- UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
674
-};
675
-
676
-#define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
677
-
678
-static __initdata struct {
679
- const char *uname;
680
- const char *subnode;
681
- struct params *params;
682
-} dt_params[] = {
683
- { "hypervisor", "uefi", xen_fdt_params },
684
- { "chosen", NULL, fdt_params },
685
-};
686
-
687
-struct param_info {
688
- int found;
689
- void *params;
690
- const char *missing;
691
-};
692
-
693
-static int __init __find_uefi_params(unsigned long node,
694
- struct param_info *info,
695
- struct params *params)
696
-{
697
- const void *prop;
698
- void *dest;
699
- u64 val;
700
- int i, len;
701
-
702
- for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
703
- prop = of_get_flat_dt_prop(node, params[i].propname, &len);
704
- if (!prop) {
705
- info->missing = params[i].name;
706
- return 0;
707
- }
708
-
709
- dest = info->params + params[i].offset;
710
- info->found++;
711
-
712
- val = of_read_number(prop, len / sizeof(u32));
713
-
714
- if (params[i].size == sizeof(u32))
715
- *(u32 *)dest = val;
716
- else
717
- *(u64 *)dest = val;
718
-
719
- if (efi_enabled(EFI_DBG))
720
- pr_info(" %s: 0x%0*llx\n", params[i].name,
721
- params[i].size * 2, val);
726
+ if (IS_ENABLED(CONFIG_X86_64) &&
727
+ systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION &&
728
+ !strcmp(vendor, "Apple")) {
729
+ pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n");
730
+ efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION;
722731 }
723
-
724
- return 1;
725732 }
726733
727
-static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
728
- int depth, void *data)
729
-{
730
- struct param_info *info = data;
731
- int i;
732
-
733
- for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
734
- const char *subnode = dt_params[i].subnode;
735
-
736
- if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
737
- info->missing = dt_params[i].params[0].name;
738
- continue;
739
- }
740
-
741
- if (subnode) {
742
- int err = of_get_flat_dt_subnode_by_name(node, subnode);
743
-
744
- if (err < 0)
745
- return 0;
746
-
747
- node = err;
748
- }
749
-
750
- return __find_uefi_params(node, info, dt_params[i].params);
751
- }
752
-
753
- return 0;
754
-}
755
-
756
-int __init efi_get_fdt_params(struct efi_fdt_params *params)
757
-{
758
- struct param_info info;
759
- int ret;
760
-
761
- pr_info("Getting EFI parameters from FDT:\n");
762
-
763
- info.found = 0;
764
- info.params = params;
765
-
766
- ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
767
- if (!info.found)
768
- pr_info("UEFI not found.\n");
769
- else if (!ret)
770
- pr_err("Can't find '%s' in device tree!\n",
771
- info.missing);
772
-
773
- return ret;
774
-}
775
-#endif /* CONFIG_EFI_PARAMS_FROM_FDT */
776
-
777
-static __initdata char memory_type_name[][20] = {
734
+static __initdata char memory_type_name[][13] = {
778735 "Reserved",
779736 "Loader Code",
780737 "Loader Data",
....@@ -782,14 +739,14 @@
782739 "Boot Data",
783740 "Runtime Code",
784741 "Runtime Data",
785
- "Conventional Memory",
786
- "Unusable Memory",
787
- "ACPI Reclaim Memory",
788
- "ACPI Memory NVS",
789
- "Memory Mapped I/O",
790
- "MMIO Port Space",
742
+ "Conventional",
743
+ "Unusable",
744
+ "ACPI Reclaim",
745
+ "ACPI Mem NVS",
746
+ "MMIO",
747
+ "MMIO Port",
791748 "PAL Code",
792
- "Persistent Memory",
749
+ "Persistent",
793750 };
794751
795752 char * __init efi_md_typeattr_format(char *buf, size_t size,
....@@ -816,25 +773,27 @@
816773 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
817774 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
818775 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
819
- EFI_MEMORY_NV |
776
+ EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
820777 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
821778 snprintf(pos, size, "|attr=0x%016llx]",
822779 (unsigned long long)attr);
823780 else
824781 snprintf(pos, size,
825
- "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
826
- attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
827
- attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
828
- attr & EFI_MEMORY_NV ? "NV" : "",
829
- attr & EFI_MEMORY_XP ? "XP" : "",
830
- attr & EFI_MEMORY_RP ? "RP" : "",
831
- attr & EFI_MEMORY_WP ? "WP" : "",
832
- attr & EFI_MEMORY_RO ? "RO" : "",
833
- attr & EFI_MEMORY_UCE ? "UCE" : "",
834
- attr & EFI_MEMORY_WB ? "WB" : "",
835
- attr & EFI_MEMORY_WT ? "WT" : "",
836
- attr & EFI_MEMORY_WC ? "WC" : "",
837
- attr & EFI_MEMORY_UC ? "UC" : "");
782
+ "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
783
+ attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
784
+ attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
785
+ attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "",
786
+ attr & EFI_MEMORY_SP ? "SP" : "",
787
+ attr & EFI_MEMORY_NV ? "NV" : "",
788
+ attr & EFI_MEMORY_XP ? "XP" : "",
789
+ attr & EFI_MEMORY_RP ? "RP" : "",
790
+ attr & EFI_MEMORY_WP ? "WP" : "",
791
+ attr & EFI_MEMORY_RO ? "RO" : "",
792
+ attr & EFI_MEMORY_UCE ? "UCE" : "",
793
+ attr & EFI_MEMORY_WB ? "WB" : "",
794
+ attr & EFI_MEMORY_WT ? "WT" : "",
795
+ attr & EFI_MEMORY_WC ? "WC" : "",
796
+ attr & EFI_MEMORY_UC ? "UC" : "");
838797 return buf;
839798 }
840799
....@@ -873,7 +832,7 @@
873832 *
874833 * Search in the EFI memory map for the region covering @phys_addr.
875834 * Returns the EFI memory type if the region was found in the memory
876
- * map, EFI_RESERVED_TYPE (zero) otherwise.
835
+ * map, -EINVAL otherwise.
877836 */
878837 int efi_mem_type(unsigned long phys_addr)
879838 {
....@@ -928,19 +887,122 @@
928887 return err;
929888 }
930889
931
-bool efi_is_table_address(unsigned long phys_addr)
890
+static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
891
+static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
892
+
893
+static int __init efi_memreserve_map_root(void)
932894 {
933
- unsigned int i;
895
+ if (mem_reserve == EFI_INVALID_TABLE_ADDR)
896
+ return -ENODEV;
934897
935
- if (phys_addr == EFI_INVALID_TABLE_ADDR)
936
- return false;
937
-
938
- for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
939
- if (*(efi_tables[i]) == phys_addr)
940
- return true;
941
-
942
- return false;
898
+ efi_memreserve_root = memremap(mem_reserve,
899
+ sizeof(*efi_memreserve_root),
900
+ MEMREMAP_WB);
901
+ if (WARN_ON_ONCE(!efi_memreserve_root))
902
+ return -ENOMEM;
903
+ return 0;
943904 }
905
+
906
+static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
907
+{
908
+ struct resource *res, *parent;
909
+ int ret;
910
+
911
+ res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
912
+ if (!res)
913
+ return -ENOMEM;
914
+
915
+ res->name = "reserved";
916
+ res->flags = IORESOURCE_MEM;
917
+ res->start = addr;
918
+ res->end = addr + size - 1;
919
+
920
+ /* we expect a conflict with a 'System RAM' region */
921
+ parent = request_resource_conflict(&iomem_resource, res);
922
+ ret = parent ? request_resource(parent, res) : 0;
923
+
924
+ /*
925
+ * Given that efi_mem_reserve_iomem() can be called at any
926
+ * time, only call memblock_reserve() if the architecture
927
+ * keeps the infrastructure around.
928
+ */
929
+ if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
930
+ memblock_reserve(addr, size);
931
+
932
+ return ret;
933
+}
934
+
935
+int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
936
+{
937
+ struct linux_efi_memreserve *rsv;
938
+ unsigned long prsv;
939
+ int rc, index;
940
+
941
+ if (efi_memreserve_root == (void *)ULONG_MAX)
942
+ return -ENODEV;
943
+
944
+ if (!efi_memreserve_root) {
945
+ rc = efi_memreserve_map_root();
946
+ if (rc)
947
+ return rc;
948
+ }
949
+
950
+ /* first try to find a slot in an existing linked list entry */
951
+ for (prsv = efi_memreserve_root->next; prsv; ) {
952
+ rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
953
+ if (!rsv)
954
+ return -ENOMEM;
955
+ index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
956
+ if (index < rsv->size) {
957
+ rsv->entry[index].base = addr;
958
+ rsv->entry[index].size = size;
959
+
960
+ memunmap(rsv);
961
+ return efi_mem_reserve_iomem(addr, size);
962
+ }
963
+ prsv = rsv->next;
964
+ memunmap(rsv);
965
+ }
966
+
967
+ /* no slot found - allocate a new linked list entry */
968
+ rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
969
+ if (!rsv)
970
+ return -ENOMEM;
971
+
972
+ rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
973
+ if (rc) {
974
+ free_page((unsigned long)rsv);
975
+ return rc;
976
+ }
977
+
978
+ /*
979
+ * The memremap() call above assumes that a linux_efi_memreserve entry
980
+ * never crosses a page boundary, so let's ensure that this remains true
981
+ * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by
982
+ * using SZ_4K explicitly in the size calculation below.
983
+ */
984
+ rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
985
+ atomic_set(&rsv->count, 1);
986
+ rsv->entry[0].base = addr;
987
+ rsv->entry[0].size = size;
988
+
989
+ spin_lock(&efi_mem_reserve_persistent_lock);
990
+ rsv->next = efi_memreserve_root->next;
991
+ efi_memreserve_root->next = __pa(rsv);
992
+ spin_unlock(&efi_mem_reserve_persistent_lock);
993
+
994
+ return efi_mem_reserve_iomem(addr, size);
995
+}
996
+
997
+static int __init efi_memreserve_root_init(void)
998
+{
999
+ if (efi_memreserve_root)
1000
+ return 0;
1001
+ if (efi_memreserve_map_root())
1002
+ efi_memreserve_root = (void *)ULONG_MAX;
1003
+ return 0;
1004
+}
1005
+early_initcall(efi_memreserve_root_init);
9441006
9451007 #ifdef CONFIG_KEXEC
9461008 static int update_efi_random_seed(struct notifier_block *nb,
....@@ -952,7 +1014,7 @@
9521014 if (!kexec_in_progress)
9531015 return NOTIFY_DONE;
9541016
955
- seed = memremap(efi.rng_seed, sizeof(*seed), MEMREMAP_WB);
1017
+ seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB);
9561018 if (seed != NULL) {
9571019 size = min(seed->size, EFI_RANDOM_SEED_SIZE);
9581020 memunmap(seed);
....@@ -960,7 +1022,7 @@
9601022 pr_err("Could not map UEFI random seed!\n");
9611023 }
9621024 if (size > 0) {
963
- seed = memremap(efi.rng_seed, sizeof(*seed) + size,
1025
+ seed = memremap(efi_rng_seed, sizeof(*seed) + size,
9641026 MEMREMAP_WB);
9651027 if (seed != NULL) {
9661028 seed->size = size;
....@@ -977,9 +1039,9 @@
9771039 .notifier_call = update_efi_random_seed,
9781040 };
9791041
980
-static int register_update_efi_random_seed(void)
1042
+static int __init register_update_efi_random_seed(void)
9811043 {
982
- if (efi.rng_seed == EFI_INVALID_TABLE_ADDR)
1044
+ if (efi_rng_seed == EFI_INVALID_TABLE_ADDR)
9831045 return 0;
9841046 return register_reboot_notifier(&efi_random_seed_nb);
9851047 }