efi.c 30 KB

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