efi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * efi.c - EFI subsystem
  4. *
  5. * Copyright (C) 2001,2003,2004 Dell <[email protected]>
  6. * Copyright (C) 2004 Intel Corporation <[email protected]>
  7. * Copyright (C) 2013 Tom Gundersen <[email protected]>
  8. *
  9. * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
  10. * allowing the efivarfs to be mounted or the efivars module to be loaded.
  11. * The existance of /sys/firmware/efi may also be used by userspace to
  12. * determine that the system supports EFI.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kobject.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/device.h>
  20. #include <linux/efi.h>
  21. #include <linux/of.h>
  22. #include <linux/initrd.h>
  23. #include <linux/io.h>
  24. #include <linux/kexec.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/random.h>
  27. #include <linux/reboot.h>
  28. #include <linux/slab.h>
  29. #include <linux/acpi.h>
  30. #include <linux/ucs2_string.h>
  31. #include <linux/memblock.h>
  32. #include <linux/security.h>
  33. #include <asm/early_ioremap.h>
  34. struct efi __read_mostly efi = {
  35. .runtime_supported_mask = EFI_RT_SUPPORTED_ALL,
  36. .acpi = EFI_INVALID_TABLE_ADDR,
  37. .acpi20 = EFI_INVALID_TABLE_ADDR,
  38. .smbios = EFI_INVALID_TABLE_ADDR,
  39. .smbios3 = EFI_INVALID_TABLE_ADDR,
  40. .esrt = EFI_INVALID_TABLE_ADDR,
  41. .tpm_log = EFI_INVALID_TABLE_ADDR,
  42. .tpm_final_log = EFI_INVALID_TABLE_ADDR,
  43. #ifdef CONFIG_LOAD_UEFI_KEYS
  44. .mokvar_table = EFI_INVALID_TABLE_ADDR,
  45. #endif
  46. #ifdef CONFIG_EFI_COCO_SECRET
  47. .coco_secret = EFI_INVALID_TABLE_ADDR,
  48. #endif
  49. };
  50. EXPORT_SYMBOL(efi);
  51. unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;
  52. static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
  53. static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
  54. static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR;
  55. struct mm_struct efi_mm = {
  56. .mm_mt = MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock),
  57. .mm_users = ATOMIC_INIT(2),
  58. .mm_count = ATOMIC_INIT(1),
  59. .write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq),
  60. MMAP_LOCK_INITIALIZER(efi_mm)
  61. .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
  62. .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
  63. .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0},
  64. };
  65. struct workqueue_struct *efi_rts_wq;
  66. static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME);
  67. static int __init setup_noefi(char *arg)
  68. {
  69. disable_runtime = true;
  70. return 0;
  71. }
  72. early_param("noefi", setup_noefi);
  73. bool efi_runtime_disabled(void)
  74. {
  75. return disable_runtime;
  76. }
  77. bool __pure __efi_soft_reserve_enabled(void)
  78. {
  79. return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE);
  80. }
  81. static int __init parse_efi_cmdline(char *str)
  82. {
  83. if (!str) {
  84. pr_warn("need at least one option\n");
  85. return -EINVAL;
  86. }
  87. if (parse_option_str(str, "debug"))
  88. set_bit(EFI_DBG, &efi.flags);
  89. if (parse_option_str(str, "noruntime"))
  90. disable_runtime = true;
  91. if (parse_option_str(str, "runtime"))
  92. disable_runtime = false;
  93. if (parse_option_str(str, "nosoftreserve"))
  94. set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags);
  95. return 0;
  96. }
  97. early_param("efi", parse_efi_cmdline);
  98. struct kobject *efi_kobj;
  99. /*
  100. * Let's not leave out systab information that snuck into
  101. * the efivars driver
  102. * Note, do not add more fields in systab sysfs file as it breaks sysfs
  103. * one value per file rule!
  104. */
  105. static ssize_t systab_show(struct kobject *kobj,
  106. struct kobj_attribute *attr, char *buf)
  107. {
  108. char *str = buf;
  109. if (!kobj || !buf)
  110. return -EINVAL;
  111. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  112. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  113. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  114. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  115. /*
  116. * If both SMBIOS and SMBIOS3 entry points are implemented, the
  117. * SMBIOS3 entry point shall be preferred, so we list it first to
  118. * let applications stop parsing after the first match.
  119. */
  120. if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
  121. str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
  122. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  123. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  124. if (IS_ENABLED(CONFIG_IA64) || IS_ENABLED(CONFIG_X86))
  125. str = efi_systab_show_arch(str);
  126. return str - buf;
  127. }
  128. static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
  129. static ssize_t fw_platform_size_show(struct kobject *kobj,
  130. struct kobj_attribute *attr, char *buf)
  131. {
  132. return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
  133. }
  134. extern __weak struct kobj_attribute efi_attr_fw_vendor;
  135. extern __weak struct kobj_attribute efi_attr_runtime;
  136. extern __weak struct kobj_attribute efi_attr_config_table;
  137. static struct kobj_attribute efi_attr_fw_platform_size =
  138. __ATTR_RO(fw_platform_size);
  139. static struct attribute *efi_subsys_attrs[] = {
  140. &efi_attr_systab.attr,
  141. &efi_attr_fw_platform_size.attr,
  142. &efi_attr_fw_vendor.attr,
  143. &efi_attr_runtime.attr,
  144. &efi_attr_config_table.attr,
  145. NULL,
  146. };
  147. umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  148. int n)
  149. {
  150. return attr->mode;
  151. }
  152. static const struct attribute_group efi_subsys_attr_group = {
  153. .attrs = efi_subsys_attrs,
  154. .is_visible = efi_attr_is_visible,
  155. };
  156. static struct efivars generic_efivars;
  157. static struct efivar_operations generic_ops;
  158. static int generic_ops_register(void)
  159. {
  160. generic_ops.get_variable = efi.get_variable;
  161. generic_ops.get_next_variable = efi.get_next_variable;
  162. generic_ops.query_variable_store = efi_query_variable_store;
  163. if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) {
  164. generic_ops.set_variable = efi.set_variable;
  165. generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
  166. }
  167. return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  168. }
  169. static void generic_ops_unregister(void)
  170. {
  171. efivars_unregister(&generic_efivars);
  172. }
  173. #ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
  174. #define EFIVAR_SSDT_NAME_MAX 16UL
  175. static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
  176. static int __init efivar_ssdt_setup(char *str)
  177. {
  178. int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
  179. if (ret)
  180. return ret;
  181. if (strlen(str) < sizeof(efivar_ssdt))
  182. memcpy(efivar_ssdt, str, strlen(str));
  183. else
  184. pr_warn("efivar_ssdt: name too long: %s\n", str);
  185. return 1;
  186. }
  187. __setup("efivar_ssdt=", efivar_ssdt_setup);
  188. static __init int efivar_ssdt_load(void)
  189. {
  190. unsigned long name_size = 256;
  191. efi_char16_t *name = NULL;
  192. efi_status_t status;
  193. efi_guid_t guid;
  194. if (!efivar_ssdt[0])
  195. return 0;
  196. name = kzalloc(name_size, GFP_KERNEL);
  197. if (!name)
  198. return -ENOMEM;
  199. for (;;) {
  200. char utf8_name[EFIVAR_SSDT_NAME_MAX];
  201. unsigned long data_size = 0;
  202. void *data;
  203. int limit;
  204. status = efi.get_next_variable(&name_size, name, &guid);
  205. if (status == EFI_NOT_FOUND) {
  206. break;
  207. } else if (status == EFI_BUFFER_TOO_SMALL) {
  208. efi_char16_t *name_tmp =
  209. krealloc(name, name_size, GFP_KERNEL);
  210. if (!name_tmp) {
  211. kfree(name);
  212. return -ENOMEM;
  213. }
  214. name = name_tmp;
  215. continue;
  216. }
  217. limit = min(EFIVAR_SSDT_NAME_MAX, name_size);
  218. ucs2_as_utf8(utf8_name, name, limit - 1);
  219. if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
  220. continue;
  221. pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid);
  222. status = efi.get_variable(name, &guid, NULL, &data_size, NULL);
  223. if (status != EFI_BUFFER_TOO_SMALL || !data_size)
  224. return -EIO;
  225. data = kmalloc(data_size, GFP_KERNEL);
  226. if (!data)
  227. return -ENOMEM;
  228. status = efi.get_variable(name, &guid, NULL, &data_size, data);
  229. if (status == EFI_SUCCESS) {
  230. acpi_status ret = acpi_load_table(data, NULL);
  231. if (ret)
  232. pr_err("failed to load table: %u\n", ret);
  233. else
  234. continue;
  235. } else {
  236. pr_err("failed to get var data: 0x%lx\n", status);
  237. }
  238. kfree(data);
  239. }
  240. return 0;
  241. }
  242. #else
  243. static inline int efivar_ssdt_load(void) { return 0; }
  244. #endif
  245. #ifdef CONFIG_DEBUG_FS
  246. #define EFI_DEBUGFS_MAX_BLOBS 32
  247. static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];
  248. static void __init efi_debugfs_init(void)
  249. {
  250. struct dentry *efi_debugfs;
  251. efi_memory_desc_t *md;
  252. char name[32];
  253. int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
  254. int i = 0;
  255. efi_debugfs = debugfs_create_dir("efi", NULL);
  256. if (IS_ERR_OR_NULL(efi_debugfs))
  257. return;
  258. for_each_efi_memory_desc(md) {
  259. switch (md->type) {
  260. case EFI_BOOT_SERVICES_CODE:
  261. snprintf(name, sizeof(name), "boot_services_code%d",
  262. type_count[md->type]++);
  263. break;
  264. case EFI_BOOT_SERVICES_DATA:
  265. snprintf(name, sizeof(name), "boot_services_data%d",
  266. type_count[md->type]++);
  267. break;
  268. default:
  269. continue;
  270. }
  271. if (i >= EFI_DEBUGFS_MAX_BLOBS) {
  272. pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
  273. EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
  274. break;
  275. }
  276. debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
  277. debugfs_blob[i].data = memremap(md->phys_addr,
  278. debugfs_blob[i].size,
  279. MEMREMAP_WB);
  280. if (!debugfs_blob[i].data)
  281. continue;
  282. debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
  283. i++;
  284. }
  285. }
  286. #else
  287. static inline void efi_debugfs_init(void) {}
  288. #endif
  289. /*
  290. * We register the efi subsystem with the firmware subsystem and the
  291. * efivars subsystem with the efi subsystem, if the system was booted with
  292. * EFI.
  293. */
  294. static int __init efisubsys_init(void)
  295. {
  296. int error;
  297. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  298. efi.runtime_supported_mask = 0;
  299. if (!efi_enabled(EFI_BOOT))
  300. return 0;
  301. if (efi.runtime_supported_mask) {
  302. /*
  303. * Since we process only one efi_runtime_service() at a time, an
  304. * ordered workqueue (which creates only one execution context)
  305. * should suffice for all our needs.
  306. */
  307. efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
  308. if (!efi_rts_wq) {
  309. pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
  310. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  311. efi.runtime_supported_mask = 0;
  312. return 0;
  313. }
  314. }
  315. if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES))
  316. platform_device_register_simple("rtc-efi", 0, NULL, 0);
  317. /* We register the efi directory at /sys/firmware/efi */
  318. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  319. if (!efi_kobj) {
  320. pr_err("efi: Firmware registration failed.\n");
  321. error = -ENOMEM;
  322. goto err_destroy_wq;
  323. }
  324. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
  325. EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) {
  326. error = generic_ops_register();
  327. if (error)
  328. goto err_put;
  329. efivar_ssdt_load();
  330. platform_device_register_simple("efivars", 0, NULL, 0);
  331. }
  332. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  333. if (error) {
  334. pr_err("efi: Sysfs attribute export failed with error %d.\n",
  335. error);
  336. goto err_unregister;
  337. }
  338. error = efi_runtime_map_init(efi_kobj);
  339. if (error)
  340. goto err_remove_group;
  341. /* and the standard mountpoint for efivarfs */
  342. error = sysfs_create_mount_point(efi_kobj, "efivars");
  343. if (error) {
  344. pr_err("efivars: Subsystem registration failed.\n");
  345. goto err_remove_group;
  346. }
  347. if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
  348. efi_debugfs_init();
  349. #ifdef CONFIG_EFI_COCO_SECRET
  350. if (efi.coco_secret != EFI_INVALID_TABLE_ADDR)
  351. platform_device_register_simple("efi_secret", 0, NULL, 0);
  352. #endif
  353. return 0;
  354. err_remove_group:
  355. sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
  356. err_unregister:
  357. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
  358. EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME))
  359. generic_ops_unregister();
  360. err_put:
  361. kobject_put(efi_kobj);
  362. err_destroy_wq:
  363. if (efi_rts_wq)
  364. destroy_workqueue(efi_rts_wq);
  365. return error;
  366. }
  367. subsys_initcall(efisubsys_init);
  368. void __init efi_find_mirror(void)
  369. {
  370. efi_memory_desc_t *md;
  371. u64 mirror_size = 0, total_size = 0;
  372. if (!efi_enabled(EFI_MEMMAP))
  373. return;
  374. for_each_efi_memory_desc(md) {
  375. unsigned long long start = md->phys_addr;
  376. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  377. total_size += size;
  378. if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
  379. memblock_mark_mirror(start, size);
  380. mirror_size += size;
  381. }
  382. }
  383. if (mirror_size)
  384. pr_info("Memory: %lldM/%lldM mirrored memory\n",
  385. mirror_size>>20, total_size>>20);
  386. }
  387. /*
  388. * Find the efi memory descriptor for a given physical address. Given a
  389. * physical address, determine if it exists within an EFI Memory Map entry,
  390. * and if so, populate the supplied memory descriptor with the appropriate
  391. * data.
  392. */
  393. int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
  394. {
  395. efi_memory_desc_t *md;
  396. if (!efi_enabled(EFI_MEMMAP)) {
  397. pr_err_once("EFI_MEMMAP is not enabled.\n");
  398. return -EINVAL;
  399. }
  400. if (!out_md) {
  401. pr_err_once("out_md is null.\n");
  402. return -EINVAL;
  403. }
  404. for_each_efi_memory_desc(md) {
  405. u64 size;
  406. u64 end;
  407. size = md->num_pages << EFI_PAGE_SHIFT;
  408. end = md->phys_addr + size;
  409. if (phys_addr >= md->phys_addr && phys_addr < end) {
  410. memcpy(out_md, md, sizeof(*out_md));
  411. return 0;
  412. }
  413. }
  414. return -ENOENT;
  415. }
  416. /*
  417. * Calculate the highest address of an efi memory descriptor.
  418. */
  419. u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
  420. {
  421. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  422. u64 end = md->phys_addr + size;
  423. return end;
  424. }
  425. void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
  426. /**
  427. * efi_mem_reserve - Reserve an EFI memory region
  428. * @addr: Physical address to reserve
  429. * @size: Size of reservation
  430. *
  431. * Mark a region as reserved from general kernel allocation and
  432. * prevent it being released by efi_free_boot_services().
  433. *
  434. * This function should be called drivers once they've parsed EFI
  435. * configuration tables to figure out where their data lives, e.g.
  436. * efi_esrt_init().
  437. */
  438. void __init efi_mem_reserve(phys_addr_t addr, u64 size)
  439. {
  440. if (!memblock_is_region_reserved(addr, size))
  441. memblock_reserve(addr, size);
  442. /*
  443. * Some architectures (x86) reserve all boot services ranges
  444. * until efi_free_boot_services() because of buggy firmware
  445. * implementations. This means the above memblock_reserve() is
  446. * superfluous on x86 and instead what it needs to do is
  447. * ensure the @start, @size is not freed.
  448. */
  449. efi_arch_mem_reserve(addr, size);
  450. }
  451. static const efi_config_table_type_t common_tables[] __initconst = {
  452. {ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" },
  453. {ACPI_TABLE_GUID, &efi.acpi, "ACPI" },
  454. {SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" },
  455. {SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" },
  456. {EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" },
  457. {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" },
  458. {LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" },
  459. {LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" },
  460. {LINUX_EFI_TPM_FINAL_LOG_GUID, &efi.tpm_final_log, "TPMFinalLog" },
  461. {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" },
  462. {LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" },
  463. {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" },
  464. #ifdef CONFIG_EFI_RCI2_TABLE
  465. {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys },
  466. #endif
  467. #ifdef CONFIG_LOAD_UEFI_KEYS
  468. {LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" },
  469. #endif
  470. #ifdef CONFIG_EFI_COCO_SECRET
  471. {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" },
  472. #endif
  473. {},
  474. };
  475. static __init int match_config_table(const efi_guid_t *guid,
  476. unsigned long table,
  477. const efi_config_table_type_t *table_types)
  478. {
  479. int i;
  480. for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
  481. if (!efi_guidcmp(*guid, table_types[i].guid)) {
  482. *(table_types[i].ptr) = table;
  483. if (table_types[i].name[0])
  484. pr_cont("%s=0x%lx ",
  485. table_types[i].name, table);
  486. return 1;
  487. }
  488. }
  489. return 0;
  490. }
  491. int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
  492. int count,
  493. const efi_config_table_type_t *arch_tables)
  494. {
  495. const efi_config_table_64_t *tbl64 = (void *)config_tables;
  496. const efi_config_table_32_t *tbl32 = (void *)config_tables;
  497. const efi_guid_t *guid;
  498. unsigned long table;
  499. int i;
  500. pr_info("");
  501. for (i = 0; i < count; i++) {
  502. if (!IS_ENABLED(CONFIG_X86)) {
  503. guid = &config_tables[i].guid;
  504. table = (unsigned long)config_tables[i].table;
  505. } else if (efi_enabled(EFI_64BIT)) {
  506. guid = &tbl64[i].guid;
  507. table = tbl64[i].table;
  508. if (IS_ENABLED(CONFIG_X86_32) &&
  509. tbl64[i].table > U32_MAX) {
  510. pr_cont("\n");
  511. pr_err("Table located above 4GB, disabling EFI.\n");
  512. return -EINVAL;
  513. }
  514. } else {
  515. guid = &tbl32[i].guid;
  516. table = tbl32[i].table;
  517. }
  518. if (!match_config_table(guid, table, common_tables) && arch_tables)
  519. match_config_table(guid, table, arch_tables);
  520. }
  521. pr_cont("\n");
  522. set_bit(EFI_CONFIG_TABLES, &efi.flags);
  523. if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
  524. struct linux_efi_random_seed *seed;
  525. u32 size = 0;
  526. seed = early_memremap(efi_rng_seed, sizeof(*seed));
  527. if (seed != NULL) {
  528. size = min_t(u32, seed->size, SZ_1K); // sanity check
  529. early_memunmap(seed, sizeof(*seed));
  530. } else {
  531. pr_err("Could not map UEFI random seed!\n");
  532. }
  533. if (size > 0) {
  534. seed = early_memremap(efi_rng_seed,
  535. sizeof(*seed) + size);
  536. if (seed != NULL) {
  537. add_bootloader_randomness(seed->bits, size);
  538. memzero_explicit(seed->bits, size);
  539. early_memunmap(seed, sizeof(*seed) + size);
  540. } else {
  541. pr_err("Could not map UEFI random seed!\n");
  542. }
  543. }
  544. }
  545. if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
  546. efi_memattr_init();
  547. efi_tpm_eventlog_init();
  548. if (mem_reserve != EFI_INVALID_TABLE_ADDR) {
  549. unsigned long prsv = mem_reserve;
  550. while (prsv) {
  551. struct linux_efi_memreserve *rsv;
  552. u8 *p;
  553. /*
  554. * Just map a full page: that is what we will get
  555. * anyway, and it permits us to map the entire entry
  556. * before knowing its size.
  557. */
  558. p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
  559. PAGE_SIZE);
  560. if (p == NULL) {
  561. pr_err("Could not map UEFI memreserve entry!\n");
  562. return -ENOMEM;
  563. }
  564. rsv = (void *)(p + prsv % PAGE_SIZE);
  565. /* reserve the entry itself */
  566. memblock_reserve(prsv,
  567. struct_size(rsv, entry, rsv->size));
  568. for (i = 0; i < atomic_read(&rsv->count); i++) {
  569. memblock_reserve(rsv->entry[i].base,
  570. rsv->entry[i].size);
  571. }
  572. prsv = rsv->next;
  573. early_memunmap(p, PAGE_SIZE);
  574. }
  575. }
  576. if (rt_prop != EFI_INVALID_TABLE_ADDR) {
  577. efi_rt_properties_table_t *tbl;
  578. tbl = early_memremap(rt_prop, sizeof(*tbl));
  579. if (tbl) {
  580. efi.runtime_supported_mask &= tbl->runtime_services_supported;
  581. early_memunmap(tbl, sizeof(*tbl));
  582. }
  583. }
  584. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
  585. initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) {
  586. struct linux_efi_initrd *tbl;
  587. tbl = early_memremap(initrd, sizeof(*tbl));
  588. if (tbl) {
  589. phys_initrd_start = tbl->base;
  590. phys_initrd_size = tbl->size;
  591. early_memunmap(tbl, sizeof(*tbl));
  592. }
  593. }
  594. return 0;
  595. }
  596. int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr,
  597. int min_major_version)
  598. {
  599. if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  600. pr_err("System table signature incorrect!\n");
  601. return -EINVAL;
  602. }
  603. if ((systab_hdr->revision >> 16) < min_major_version)
  604. pr_err("Warning: System table version %d.%02d, expected %d.00 or greater!\n",
  605. systab_hdr->revision >> 16,
  606. systab_hdr->revision & 0xffff,
  607. min_major_version);
  608. return 0;
  609. }
  610. #ifndef CONFIG_IA64
  611. static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor,
  612. size_t size)
  613. {
  614. const efi_char16_t *ret;
  615. ret = early_memremap_ro(fw_vendor, size);
  616. if (!ret)
  617. pr_err("Could not map the firmware vendor!\n");
  618. return ret;
  619. }
  620. static void __init unmap_fw_vendor(const void *fw_vendor, size_t size)
  621. {
  622. early_memunmap((void *)fw_vendor, size);
  623. }
  624. #else
  625. #define map_fw_vendor(p, s) __va(p)
  626. #define unmap_fw_vendor(v, s)
  627. #endif
  628. void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
  629. unsigned long fw_vendor)
  630. {
  631. char vendor[100] = "unknown";
  632. const efi_char16_t *c16;
  633. size_t i;
  634. c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t));
  635. if (c16) {
  636. for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
  637. vendor[i] = c16[i];
  638. vendor[i] = '\0';
  639. unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t));
  640. }
  641. pr_info("EFI v%u.%.02u by %s\n",
  642. systab_hdr->revision >> 16,
  643. systab_hdr->revision & 0xffff,
  644. vendor);
  645. if (IS_ENABLED(CONFIG_X86_64) &&
  646. systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION &&
  647. !strcmp(vendor, "Apple")) {
  648. pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n");
  649. efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION;
  650. }
  651. }
  652. static __initdata char memory_type_name[][13] = {
  653. "Reserved",
  654. "Loader Code",
  655. "Loader Data",
  656. "Boot Code",
  657. "Boot Data",
  658. "Runtime Code",
  659. "Runtime Data",
  660. "Conventional",
  661. "Unusable",
  662. "ACPI Reclaim",
  663. "ACPI Mem NVS",
  664. "MMIO",
  665. "MMIO Port",
  666. "PAL Code",
  667. "Persistent",
  668. };
  669. char * __init efi_md_typeattr_format(char *buf, size_t size,
  670. const efi_memory_desc_t *md)
  671. {
  672. char *pos;
  673. int type_len;
  674. u64 attr;
  675. pos = buf;
  676. if (md->type >= ARRAY_SIZE(memory_type_name))
  677. type_len = snprintf(pos, size, "[type=%u", md->type);
  678. else
  679. type_len = snprintf(pos, size, "[%-*s",
  680. (int)(sizeof(memory_type_name[0]) - 1),
  681. memory_type_name[md->type]);
  682. if (type_len >= size)
  683. return buf;
  684. pos += type_len;
  685. size -= type_len;
  686. attr = md->attribute;
  687. if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
  688. EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
  689. EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
  690. EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
  691. EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
  692. snprintf(pos, size, "|attr=0x%016llx]",
  693. (unsigned long long)attr);
  694. else
  695. snprintf(pos, size,
  696. "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
  697. attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
  698. attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
  699. attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "",
  700. attr & EFI_MEMORY_SP ? "SP" : "",
  701. attr & EFI_MEMORY_NV ? "NV" : "",
  702. attr & EFI_MEMORY_XP ? "XP" : "",
  703. attr & EFI_MEMORY_RP ? "RP" : "",
  704. attr & EFI_MEMORY_WP ? "WP" : "",
  705. attr & EFI_MEMORY_RO ? "RO" : "",
  706. attr & EFI_MEMORY_UCE ? "UCE" : "",
  707. attr & EFI_MEMORY_WB ? "WB" : "",
  708. attr & EFI_MEMORY_WT ? "WT" : "",
  709. attr & EFI_MEMORY_WC ? "WC" : "",
  710. attr & EFI_MEMORY_UC ? "UC" : "");
  711. return buf;
  712. }
  713. /*
  714. * IA64 has a funky EFI memory map that doesn't work the same way as
  715. * other architectures.
  716. */
  717. #ifndef CONFIG_IA64
  718. /*
  719. * efi_mem_attributes - lookup memmap attributes for physical address
  720. * @phys_addr: the physical address to lookup
  721. *
  722. * Search in the EFI memory map for the region covering
  723. * @phys_addr. Returns the EFI memory attributes if the region
  724. * was found in the memory map, 0 otherwise.
  725. */
  726. u64 efi_mem_attributes(unsigned long phys_addr)
  727. {
  728. efi_memory_desc_t *md;
  729. if (!efi_enabled(EFI_MEMMAP))
  730. return 0;
  731. for_each_efi_memory_desc(md) {
  732. if ((md->phys_addr <= phys_addr) &&
  733. (phys_addr < (md->phys_addr +
  734. (md->num_pages << EFI_PAGE_SHIFT))))
  735. return md->attribute;
  736. }
  737. return 0;
  738. }
  739. /*
  740. * efi_mem_type - lookup memmap type for physical address
  741. * @phys_addr: the physical address to lookup
  742. *
  743. * Search in the EFI memory map for the region covering @phys_addr.
  744. * Returns the EFI memory type if the region was found in the memory
  745. * map, -EINVAL otherwise.
  746. */
  747. int efi_mem_type(unsigned long phys_addr)
  748. {
  749. const efi_memory_desc_t *md;
  750. if (!efi_enabled(EFI_MEMMAP))
  751. return -ENOTSUPP;
  752. for_each_efi_memory_desc(md) {
  753. if ((md->phys_addr <= phys_addr) &&
  754. (phys_addr < (md->phys_addr +
  755. (md->num_pages << EFI_PAGE_SHIFT))))
  756. return md->type;
  757. }
  758. return -EINVAL;
  759. }
  760. #endif
  761. int efi_status_to_err(efi_status_t status)
  762. {
  763. int err;
  764. switch (status) {
  765. case EFI_SUCCESS:
  766. err = 0;
  767. break;
  768. case EFI_INVALID_PARAMETER:
  769. err = -EINVAL;
  770. break;
  771. case EFI_OUT_OF_RESOURCES:
  772. err = -ENOSPC;
  773. break;
  774. case EFI_DEVICE_ERROR:
  775. err = -EIO;
  776. break;
  777. case EFI_WRITE_PROTECTED:
  778. err = -EROFS;
  779. break;
  780. case EFI_SECURITY_VIOLATION:
  781. err = -EACCES;
  782. break;
  783. case EFI_NOT_FOUND:
  784. err = -ENOENT;
  785. break;
  786. case EFI_ABORTED:
  787. err = -EINTR;
  788. break;
  789. default:
  790. err = -EINVAL;
  791. }
  792. return err;
  793. }
  794. EXPORT_SYMBOL_GPL(efi_status_to_err);
  795. static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
  796. static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
  797. static int __init efi_memreserve_map_root(void)
  798. {
  799. if (mem_reserve == EFI_INVALID_TABLE_ADDR)
  800. return -ENODEV;
  801. efi_memreserve_root = memremap(mem_reserve,
  802. sizeof(*efi_memreserve_root),
  803. MEMREMAP_WB);
  804. if (WARN_ON_ONCE(!efi_memreserve_root))
  805. return -ENOMEM;
  806. return 0;
  807. }
  808. static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
  809. {
  810. struct resource *res, *parent;
  811. int ret;
  812. res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
  813. if (!res)
  814. return -ENOMEM;
  815. res->name = "reserved";
  816. res->flags = IORESOURCE_MEM;
  817. res->start = addr;
  818. res->end = addr + size - 1;
  819. /* we expect a conflict with a 'System RAM' region */
  820. parent = request_resource_conflict(&iomem_resource, res);
  821. ret = parent ? request_resource(parent, res) : 0;
  822. /*
  823. * Given that efi_mem_reserve_iomem() can be called at any
  824. * time, only call memblock_reserve() if the architecture
  825. * keeps the infrastructure around.
  826. */
  827. if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
  828. memblock_reserve(addr, size);
  829. return ret;
  830. }
  831. int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
  832. {
  833. struct linux_efi_memreserve *rsv;
  834. unsigned long prsv;
  835. int rc, index;
  836. if (efi_memreserve_root == (void *)ULONG_MAX)
  837. return -ENODEV;
  838. if (!efi_memreserve_root) {
  839. rc = efi_memreserve_map_root();
  840. if (rc)
  841. return rc;
  842. }
  843. /* first try to find a slot in an existing linked list entry */
  844. for (prsv = efi_memreserve_root->next; prsv; ) {
  845. rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
  846. if (!rsv)
  847. return -ENOMEM;
  848. index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
  849. if (index < rsv->size) {
  850. rsv->entry[index].base = addr;
  851. rsv->entry[index].size = size;
  852. memunmap(rsv);
  853. return efi_mem_reserve_iomem(addr, size);
  854. }
  855. prsv = rsv->next;
  856. memunmap(rsv);
  857. }
  858. /* no slot found - allocate a new linked list entry */
  859. rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
  860. if (!rsv)
  861. return -ENOMEM;
  862. rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
  863. if (rc) {
  864. free_page((unsigned long)rsv);
  865. return rc;
  866. }
  867. /*
  868. * The memremap() call above assumes that a linux_efi_memreserve entry
  869. * never crosses a page boundary, so let's ensure that this remains true
  870. * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by
  871. * using SZ_4K explicitly in the size calculation below.
  872. */
  873. rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
  874. atomic_set(&rsv->count, 1);
  875. rsv->entry[0].base = addr;
  876. rsv->entry[0].size = size;
  877. spin_lock(&efi_mem_reserve_persistent_lock);
  878. rsv->next = efi_memreserve_root->next;
  879. efi_memreserve_root->next = __pa(rsv);
  880. spin_unlock(&efi_mem_reserve_persistent_lock);
  881. return efi_mem_reserve_iomem(addr, size);
  882. }
  883. static int __init efi_memreserve_root_init(void)
  884. {
  885. if (efi_memreserve_root)
  886. return 0;
  887. if (efi_memreserve_map_root())
  888. efi_memreserve_root = (void *)ULONG_MAX;
  889. return 0;
  890. }
  891. early_initcall(efi_memreserve_root_init);
  892. #ifdef CONFIG_KEXEC
  893. static int update_efi_random_seed(struct notifier_block *nb,
  894. unsigned long code, void *unused)
  895. {
  896. struct linux_efi_random_seed *seed;
  897. u32 size = 0;
  898. if (!kexec_in_progress)
  899. return NOTIFY_DONE;
  900. seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB);
  901. if (seed != NULL) {
  902. size = min(seed->size, EFI_RANDOM_SEED_SIZE);
  903. memunmap(seed);
  904. } else {
  905. pr_err("Could not map UEFI random seed!\n");
  906. }
  907. if (size > 0) {
  908. seed = memremap(efi_rng_seed, sizeof(*seed) + size,
  909. MEMREMAP_WB);
  910. if (seed != NULL) {
  911. seed->size = size;
  912. get_random_bytes(seed->bits, seed->size);
  913. memunmap(seed);
  914. } else {
  915. pr_err("Could not map UEFI random seed!\n");
  916. }
  917. }
  918. return NOTIFY_DONE;
  919. }
  920. static struct notifier_block efi_random_seed_nb = {
  921. .notifier_call = update_efi_random_seed,
  922. };
  923. static int __init register_update_efi_random_seed(void)
  924. {
  925. if (efi_rng_seed == EFI_INVALID_TABLE_ADDR)
  926. return 0;
  927. return register_reboot_notifier(&efi_random_seed_nb);
  928. }
  929. late_initcall(register_update_efi_random_seed);
  930. #endif