efi.c 26 KB

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