efi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * efi.c - EFI subsystem
  3. *
  4. * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
  5. * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
  6. * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
  7. *
  8. * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
  9. * allowing the efivarfs to be mounted or the efivars module to be loaded.
  10. * The existance of /sys/firmware/efi may also be used by userspace to
  11. * determine that the system supports EFI.
  12. *
  13. * This file is released under the GPLv2.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kobject.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/efi.h>
  21. #include <linux/of.h>
  22. #include <linux/of_fdt.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. .mps = EFI_INVALID_TABLE_ADDR,
  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. .sal_systab = EFI_INVALID_TABLE_ADDR,
  41. .boot_info = EFI_INVALID_TABLE_ADDR,
  42. .hcdp = EFI_INVALID_TABLE_ADDR,
  43. .uga = EFI_INVALID_TABLE_ADDR,
  44. .uv_systab = EFI_INVALID_TABLE_ADDR,
  45. .fw_vendor = EFI_INVALID_TABLE_ADDR,
  46. .runtime = EFI_INVALID_TABLE_ADDR,
  47. .config_table = EFI_INVALID_TABLE_ADDR,
  48. .esrt = EFI_INVALID_TABLE_ADDR,
  49. .properties_table = EFI_INVALID_TABLE_ADDR,
  50. .mem_attr_table = EFI_INVALID_TABLE_ADDR,
  51. .rng_seed = EFI_INVALID_TABLE_ADDR,
  52. .tpm_log = EFI_INVALID_TABLE_ADDR
  53. };
  54. EXPORT_SYMBOL(efi);
  55. static unsigned long *efi_tables[] = {
  56. &efi.mps,
  57. &efi.acpi,
  58. &efi.acpi20,
  59. &efi.smbios,
  60. &efi.smbios3,
  61. &efi.sal_systab,
  62. &efi.boot_info,
  63. &efi.hcdp,
  64. &efi.uga,
  65. &efi.uv_systab,
  66. &efi.fw_vendor,
  67. &efi.runtime,
  68. &efi.config_table,
  69. &efi.esrt,
  70. &efi.properties_table,
  71. &efi.mem_attr_table,
  72. };
  73. struct mm_struct efi_mm = {
  74. .mm_rb = RB_ROOT,
  75. .mm_users = ATOMIC_INIT(2),
  76. .mm_count = ATOMIC_INIT(1),
  77. .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
  78. .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
  79. .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
  80. .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0},
  81. };
  82. struct workqueue_struct *efi_rts_wq;
  83. static bool disable_runtime;
  84. static int __init setup_noefi(char *arg)
  85. {
  86. disable_runtime = true;
  87. return 0;
  88. }
  89. early_param("noefi", setup_noefi);
  90. bool efi_runtime_disabled(void)
  91. {
  92. return disable_runtime;
  93. }
  94. static int __init parse_efi_cmdline(char *str)
  95. {
  96. if (!str) {
  97. pr_warn("need at least one option\n");
  98. return -EINVAL;
  99. }
  100. if (parse_option_str(str, "debug"))
  101. set_bit(EFI_DBG, &efi.flags);
  102. if (parse_option_str(str, "noruntime"))
  103. disable_runtime = true;
  104. return 0;
  105. }
  106. early_param("efi", parse_efi_cmdline);
  107. struct kobject *efi_kobj;
  108. /*
  109. * Let's not leave out systab information that snuck into
  110. * the efivars driver
  111. * Note, do not add more fields in systab sysfs file as it breaks sysfs
  112. * one value per file rule!
  113. */
  114. static ssize_t systab_show(struct kobject *kobj,
  115. struct kobj_attribute *attr, char *buf)
  116. {
  117. char *str = buf;
  118. if (!kobj || !buf)
  119. return -EINVAL;
  120. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  121. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  122. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  123. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  124. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  125. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  126. /*
  127. * If both SMBIOS and SMBIOS3 entry points are implemented, the
  128. * SMBIOS3 entry point shall be preferred, so we list it first to
  129. * let applications stop parsing after the first match.
  130. */
  131. if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
  132. str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
  133. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  134. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  135. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  136. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  137. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  138. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  139. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  140. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  141. return str - buf;
  142. }
  143. static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
  144. #define EFI_FIELD(var) efi.var
  145. #define EFI_ATTR_SHOW(name) \
  146. static ssize_t name##_show(struct kobject *kobj, \
  147. struct kobj_attribute *attr, char *buf) \
  148. { \
  149. return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
  150. }
  151. EFI_ATTR_SHOW(fw_vendor);
  152. EFI_ATTR_SHOW(runtime);
  153. EFI_ATTR_SHOW(config_table);
  154. static ssize_t fw_platform_size_show(struct kobject *kobj,
  155. struct kobj_attribute *attr, char *buf)
  156. {
  157. return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
  158. }
  159. static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
  160. static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
  161. static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
  162. static struct kobj_attribute efi_attr_fw_platform_size =
  163. __ATTR_RO(fw_platform_size);
  164. static struct attribute *efi_subsys_attrs[] = {
  165. &efi_attr_systab.attr,
  166. &efi_attr_fw_vendor.attr,
  167. &efi_attr_runtime.attr,
  168. &efi_attr_config_table.attr,
  169. &efi_attr_fw_platform_size.attr,
  170. NULL,
  171. };
  172. static umode_t efi_attr_is_visible(struct kobject *kobj,
  173. struct attribute *attr, int n)
  174. {
  175. if (attr == &efi_attr_fw_vendor.attr) {
  176. if (efi_enabled(EFI_PARAVIRT) ||
  177. efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
  178. return 0;
  179. } else if (attr == &efi_attr_runtime.attr) {
  180. if (efi.runtime == EFI_INVALID_TABLE_ADDR)
  181. return 0;
  182. } else if (attr == &efi_attr_config_table.attr) {
  183. if (efi.config_table == EFI_INVALID_TABLE_ADDR)
  184. return 0;
  185. }
  186. return attr->mode;
  187. }
  188. static const struct attribute_group efi_subsys_attr_group = {
  189. .attrs = efi_subsys_attrs,
  190. .is_visible = efi_attr_is_visible,
  191. };
  192. static struct efivars generic_efivars;
  193. static struct efivar_operations generic_ops;
  194. static int generic_ops_register(void)
  195. {
  196. generic_ops.get_variable = efi.get_variable;
  197. generic_ops.set_variable = efi.set_variable;
  198. generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
  199. generic_ops.get_next_variable = efi.get_next_variable;
  200. generic_ops.query_variable_store = efi_query_variable_store;
  201. return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  202. }
  203. static void generic_ops_unregister(void)
  204. {
  205. efivars_unregister(&generic_efivars);
  206. }
  207. #if IS_ENABLED(CONFIG_ACPI)
  208. #define EFIVAR_SSDT_NAME_MAX 16
  209. static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
  210. static int __init efivar_ssdt_setup(char *str)
  211. {
  212. if (kernel_is_locked_down("ACPI tables"))
  213. return -EPERM;
  214. if (strlen(str) < sizeof(efivar_ssdt))
  215. memcpy(efivar_ssdt, str, strlen(str));
  216. else
  217. pr_warn("efivar_ssdt: name too long: %s\n", str);
  218. return 0;
  219. }
  220. __setup("efivar_ssdt=", efivar_ssdt_setup);
  221. static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor,
  222. unsigned long name_size, void *data)
  223. {
  224. struct efivar_entry *entry;
  225. struct list_head *list = data;
  226. char utf8_name[EFIVAR_SSDT_NAME_MAX];
  227. int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size);
  228. ucs2_as_utf8(utf8_name, name, limit - 1);
  229. if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
  230. return 0;
  231. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  232. if (!entry)
  233. return 0;
  234. memcpy(entry->var.VariableName, name, name_size);
  235. memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t));
  236. efivar_entry_add(entry, list);
  237. return 0;
  238. }
  239. static __init int efivar_ssdt_load(void)
  240. {
  241. LIST_HEAD(entries);
  242. struct efivar_entry *entry, *aux;
  243. unsigned long size;
  244. void *data;
  245. int ret;
  246. if (!efivar_ssdt[0])
  247. return 0;
  248. ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries);
  249. list_for_each_entry_safe(entry, aux, &entries, list) {
  250. pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt,
  251. &entry->var.VendorGuid);
  252. list_del(&entry->list);
  253. ret = efivar_entry_size(entry, &size);
  254. if (ret) {
  255. pr_err("failed to get var size\n");
  256. goto free_entry;
  257. }
  258. data = kmalloc(size, GFP_KERNEL);
  259. if (!data) {
  260. ret = -ENOMEM;
  261. goto free_entry;
  262. }
  263. ret = efivar_entry_get(entry, NULL, &size, data);
  264. if (ret) {
  265. pr_err("failed to get var data\n");
  266. goto free_data;
  267. }
  268. ret = acpi_load_table(data);
  269. if (ret) {
  270. pr_err("failed to load table: %d\n", ret);
  271. goto free_data;
  272. }
  273. goto free_entry;
  274. free_data:
  275. kfree(data);
  276. free_entry:
  277. kfree(entry);
  278. }
  279. return ret;
  280. }
  281. #else
  282. static inline int efivar_ssdt_load(void) { return 0; }
  283. #endif
  284. /*
  285. * We register the efi subsystem with the firmware subsystem and the
  286. * efivars subsystem with the efi subsystem, if the system was booted with
  287. * EFI.
  288. */
  289. static int __init efisubsys_init(void)
  290. {
  291. int error;
  292. if (!efi_enabled(EFI_BOOT))
  293. return 0;
  294. /*
  295. * Since we process only one efi_runtime_service() at a time, an
  296. * ordered workqueue (which creates only one execution context)
  297. * should suffice all our needs.
  298. */
  299. efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
  300. if (!efi_rts_wq) {
  301. pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
  302. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  303. return 0;
  304. }
  305. /* We register the efi directory at /sys/firmware/efi */
  306. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  307. if (!efi_kobj) {
  308. pr_err("efi: Firmware registration failed.\n");
  309. return -ENOMEM;
  310. }
  311. error = generic_ops_register();
  312. if (error)
  313. goto err_put;
  314. if (efi_enabled(EFI_RUNTIME_SERVICES))
  315. efivar_ssdt_load();
  316. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  317. if (error) {
  318. pr_err("efi: Sysfs attribute export failed with error %d.\n",
  319. error);
  320. goto err_unregister;
  321. }
  322. error = efi_runtime_map_init(efi_kobj);
  323. if (error)
  324. goto err_remove_group;
  325. /* and the standard mountpoint for efivarfs */
  326. error = sysfs_create_mount_point(efi_kobj, "efivars");
  327. if (error) {
  328. pr_err("efivars: Subsystem registration failed.\n");
  329. goto err_remove_group;
  330. }
  331. return 0;
  332. err_remove_group:
  333. sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
  334. err_unregister:
  335. generic_ops_unregister();
  336. err_put:
  337. kobject_put(efi_kobj);
  338. return error;
  339. }
  340. subsys_initcall(efisubsys_init);
  341. /*
  342. * Find the efi memory descriptor for a given physical address. Given a
  343. * physical address, determine if it exists within an EFI Memory Map entry,
  344. * and if so, populate the supplied memory descriptor with the appropriate
  345. * data.
  346. */
  347. int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
  348. {
  349. efi_memory_desc_t *md;
  350. if (!efi_enabled(EFI_MEMMAP)) {
  351. pr_err_once("EFI_MEMMAP is not enabled.\n");
  352. return -EINVAL;
  353. }
  354. if (!out_md) {
  355. pr_err_once("out_md is null.\n");
  356. return -EINVAL;
  357. }
  358. for_each_efi_memory_desc(md) {
  359. u64 size;
  360. u64 end;
  361. size = md->num_pages << EFI_PAGE_SHIFT;
  362. end = md->phys_addr + size;
  363. if (phys_addr >= md->phys_addr && phys_addr < end) {
  364. memcpy(out_md, md, sizeof(*out_md));
  365. return 0;
  366. }
  367. }
  368. return -ENOENT;
  369. }
  370. /*
  371. * Calculate the highest address of an efi memory descriptor.
  372. */
  373. u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
  374. {
  375. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  376. u64 end = md->phys_addr + size;
  377. return end;
  378. }
  379. void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
  380. /**
  381. * efi_mem_reserve - Reserve an EFI memory region
  382. * @addr: Physical address to reserve
  383. * @size: Size of reservation
  384. *
  385. * Mark a region as reserved from general kernel allocation and
  386. * prevent it being released by efi_free_boot_services().
  387. *
  388. * This function should be called drivers once they've parsed EFI
  389. * configuration tables to figure out where their data lives, e.g.
  390. * efi_esrt_init().
  391. */
  392. void __init efi_mem_reserve(phys_addr_t addr, u64 size)
  393. {
  394. if (!memblock_is_region_reserved(addr, size))
  395. memblock_reserve(addr, size);
  396. /*
  397. * Some architectures (x86) reserve all boot services ranges
  398. * until efi_free_boot_services() because of buggy firmware
  399. * implementations. This means the above memblock_reserve() is
  400. * superfluous on x86 and instead what it needs to do is
  401. * ensure the @start, @size is not freed.
  402. */
  403. efi_arch_mem_reserve(addr, size);
  404. }
  405. static __initdata efi_config_table_type_t common_tables[] = {
  406. {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
  407. {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
  408. {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
  409. {MPS_TABLE_GUID, "MPS", &efi.mps},
  410. {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
  411. {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
  412. {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
  413. {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
  414. {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
  415. {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
  416. {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
  417. {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
  418. {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log},
  419. {NULL_GUID, NULL, NULL},
  420. };
  421. static __init int match_config_table(efi_guid_t *guid,
  422. unsigned long table,
  423. efi_config_table_type_t *table_types)
  424. {
  425. int i;
  426. if (table_types) {
  427. for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
  428. if (!efi_guidcmp(*guid, table_types[i].guid)) {
  429. *(table_types[i].ptr) = table;
  430. if (table_types[i].name)
  431. pr_cont(" %s=0x%lx ",
  432. table_types[i].name, table);
  433. return 1;
  434. }
  435. }
  436. }
  437. return 0;
  438. }
  439. int __init efi_config_parse_tables(void *config_tables, int count, int sz,
  440. efi_config_table_type_t *arch_tables)
  441. {
  442. void *tablep;
  443. int i;
  444. tablep = config_tables;
  445. pr_info("");
  446. for (i = 0; i < count; i++) {
  447. efi_guid_t guid;
  448. unsigned long table;
  449. if (efi_enabled(EFI_64BIT)) {
  450. u64 table64;
  451. guid = ((efi_config_table_64_t *)tablep)->guid;
  452. table64 = ((efi_config_table_64_t *)tablep)->table;
  453. table = table64;
  454. #ifndef CONFIG_64BIT
  455. if (table64 >> 32) {
  456. pr_cont("\n");
  457. pr_err("Table located above 4GB, disabling EFI.\n");
  458. return -EINVAL;
  459. }
  460. #endif
  461. } else {
  462. guid = ((efi_config_table_32_t *)tablep)->guid;
  463. table = ((efi_config_table_32_t *)tablep)->table;
  464. }
  465. if (!match_config_table(&guid, table, common_tables))
  466. match_config_table(&guid, table, arch_tables);
  467. tablep += sz;
  468. }
  469. pr_cont("\n");
  470. set_bit(EFI_CONFIG_TABLES, &efi.flags);
  471. if (efi.rng_seed != EFI_INVALID_TABLE_ADDR) {
  472. struct linux_efi_random_seed *seed;
  473. u32 size = 0;
  474. seed = early_memremap(efi.rng_seed, sizeof(*seed));
  475. if (seed != NULL) {
  476. size = seed->size;
  477. early_memunmap(seed, sizeof(*seed));
  478. } else {
  479. pr_err("Could not map UEFI random seed!\n");
  480. }
  481. if (size > 0) {
  482. seed = early_memremap(efi.rng_seed,
  483. sizeof(*seed) + size);
  484. if (seed != NULL) {
  485. pr_notice("seeding entropy pool\n");
  486. add_device_randomness(seed->bits, seed->size);
  487. early_memunmap(seed, sizeof(*seed) + size);
  488. } else {
  489. pr_err("Could not map UEFI random seed!\n");
  490. }
  491. }
  492. }
  493. if (efi_enabled(EFI_MEMMAP))
  494. efi_memattr_init();
  495. efi_tpm_eventlog_init();
  496. /* Parse the EFI Properties table if it exists */
  497. if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
  498. efi_properties_table_t *tbl;
  499. tbl = early_memremap(efi.properties_table, sizeof(*tbl));
  500. if (tbl == NULL) {
  501. pr_err("Could not map Properties table!\n");
  502. return -ENOMEM;
  503. }
  504. if (tbl->memory_protection_attribute &
  505. EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
  506. set_bit(EFI_NX_PE_DATA, &efi.flags);
  507. early_memunmap(tbl, sizeof(*tbl));
  508. }
  509. return 0;
  510. }
  511. int __init efi_config_init(efi_config_table_type_t *arch_tables)
  512. {
  513. void *config_tables;
  514. int sz, ret;
  515. if (efi_enabled(EFI_64BIT))
  516. sz = sizeof(efi_config_table_64_t);
  517. else
  518. sz = sizeof(efi_config_table_32_t);
  519. /*
  520. * Let's see what config tables the firmware passed to us.
  521. */
  522. config_tables = early_memremap(efi.systab->tables,
  523. efi.systab->nr_tables * sz);
  524. if (config_tables == NULL) {
  525. pr_err("Could not map Configuration table!\n");
  526. return -ENOMEM;
  527. }
  528. ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
  529. arch_tables);
  530. early_memunmap(config_tables, efi.systab->nr_tables * sz);
  531. return ret;
  532. }
  533. #ifdef CONFIG_EFI_VARS_MODULE
  534. static int __init efi_load_efivars(void)
  535. {
  536. struct platform_device *pdev;
  537. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  538. return 0;
  539. pdev = platform_device_register_simple("efivars", 0, NULL, 0);
  540. return PTR_ERR_OR_ZERO(pdev);
  541. }
  542. device_initcall(efi_load_efivars);
  543. #endif
  544. #ifdef CONFIG_EFI_PARAMS_FROM_FDT
  545. #define UEFI_PARAM(name, prop, field) \
  546. { \
  547. { name }, \
  548. { prop }, \
  549. offsetof(struct efi_fdt_params, field), \
  550. FIELD_SIZEOF(struct efi_fdt_params, field) \
  551. }
  552. struct params {
  553. const char name[32];
  554. const char propname[32];
  555. int offset;
  556. int size;
  557. };
  558. static __initdata struct params fdt_params[] = {
  559. UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
  560. UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
  561. UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
  562. UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
  563. UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver),
  564. UEFI_PARAM("Secure Boot Enabled", "linux,uefi-secure-boot", secure_boot)
  565. };
  566. static __initdata struct params xen_fdt_params[] = {
  567. UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
  568. UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
  569. UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
  570. UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
  571. UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
  572. };
  573. #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
  574. static __initdata struct {
  575. const char *uname;
  576. const char *subnode;
  577. struct params *params;
  578. } dt_params[] = {
  579. { "hypervisor", "uefi", xen_fdt_params },
  580. { "chosen", NULL, fdt_params },
  581. };
  582. struct param_info {
  583. int found;
  584. void *params;
  585. const char *missing;
  586. };
  587. static int __init __find_uefi_params(unsigned long node,
  588. struct param_info *info,
  589. struct params *params)
  590. {
  591. const void *prop;
  592. void *dest;
  593. u64 val;
  594. int i, len;
  595. for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
  596. prop = of_get_flat_dt_prop(node, params[i].propname, &len);
  597. if (!prop) {
  598. info->missing = params[i].name;
  599. return 0;
  600. }
  601. dest = info->params + params[i].offset;
  602. info->found++;
  603. val = of_read_number(prop, len / sizeof(u32));
  604. if (params[i].size == sizeof(u32))
  605. *(u32 *)dest = val;
  606. else
  607. *(u64 *)dest = val;
  608. if (efi_enabled(EFI_DBG))
  609. pr_info(" %s: 0x%0*llx\n", params[i].name,
  610. params[i].size * 2, val);
  611. }
  612. return 1;
  613. }
  614. static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
  615. int depth, void *data)
  616. {
  617. struct param_info *info = data;
  618. int i;
  619. for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
  620. const char *subnode = dt_params[i].subnode;
  621. if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
  622. info->missing = dt_params[i].params[0].name;
  623. continue;
  624. }
  625. if (subnode) {
  626. int err = of_get_flat_dt_subnode_by_name(node, subnode);
  627. if (err < 0)
  628. return 0;
  629. node = err;
  630. }
  631. return __find_uefi_params(node, info, dt_params[i].params);
  632. }
  633. return 0;
  634. }
  635. int __init efi_get_fdt_params(struct efi_fdt_params *params)
  636. {
  637. struct param_info info;
  638. int ret;
  639. pr_info("Getting EFI parameters from FDT:\n");
  640. info.found = 0;
  641. info.params = params;
  642. ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
  643. if (!info.found)
  644. pr_info("UEFI not found.\n");
  645. else if (!ret)
  646. pr_err("Can't find '%s' in device tree!\n",
  647. info.missing);
  648. return ret;
  649. }
  650. #endif /* CONFIG_EFI_PARAMS_FROM_FDT */
  651. static __initdata char memory_type_name[][20] = {
  652. "Reserved",
  653. "Loader Code",
  654. "Loader Data",
  655. "Boot Code",
  656. "Boot Data",
  657. "Runtime Code",
  658. "Runtime Data",
  659. "Conventional Memory",
  660. "Unusable Memory",
  661. "ACPI Reclaim Memory",
  662. "ACPI Memory NVS",
  663. "Memory Mapped I/O",
  664. "MMIO Port Space",
  665. "PAL Code",
  666. "Persistent Memory",
  667. };
  668. char * __init efi_md_typeattr_format(char *buf, size_t size,
  669. const efi_memory_desc_t *md)
  670. {
  671. char *pos;
  672. int type_len;
  673. u64 attr;
  674. pos = buf;
  675. if (md->type >= ARRAY_SIZE(memory_type_name))
  676. type_len = snprintf(pos, size, "[type=%u", md->type);
  677. else
  678. type_len = snprintf(pos, size, "[%-*s",
  679. (int)(sizeof(memory_type_name[0]) - 1),
  680. memory_type_name[md->type]);
  681. if (type_len >= size)
  682. return buf;
  683. pos += type_len;
  684. size -= type_len;
  685. attr = md->attribute;
  686. if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
  687. EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
  688. EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
  689. EFI_MEMORY_NV |
  690. EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
  691. snprintf(pos, size, "|attr=0x%016llx]",
  692. (unsigned long long)attr);
  693. else
  694. snprintf(pos, size,
  695. "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
  696. attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
  697. attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
  698. attr & EFI_MEMORY_NV ? "NV" : "",
  699. attr & EFI_MEMORY_XP ? "XP" : "",
  700. attr & EFI_MEMORY_RP ? "RP" : "",
  701. attr & EFI_MEMORY_WP ? "WP" : "",
  702. attr & EFI_MEMORY_RO ? "RO" : "",
  703. attr & EFI_MEMORY_UCE ? "UCE" : "",
  704. attr & EFI_MEMORY_WB ? "WB" : "",
  705. attr & EFI_MEMORY_WT ? "WT" : "",
  706. attr & EFI_MEMORY_WC ? "WC" : "",
  707. attr & EFI_MEMORY_UC ? "UC" : "");
  708. return buf;
  709. }
  710. /*
  711. * IA64 has a funky EFI memory map that doesn't work the same way as
  712. * other architectures.
  713. */
  714. #ifndef CONFIG_IA64
  715. /*
  716. * efi_mem_attributes - lookup memmap attributes for physical address
  717. * @phys_addr: the physical address to lookup
  718. *
  719. * Search in the EFI memory map for the region covering
  720. * @phys_addr. Returns the EFI memory attributes if the region
  721. * was found in the memory map, 0 otherwise.
  722. */
  723. u64 efi_mem_attributes(unsigned long phys_addr)
  724. {
  725. efi_memory_desc_t *md;
  726. if (!efi_enabled(EFI_MEMMAP))
  727. return 0;
  728. for_each_efi_memory_desc(md) {
  729. if ((md->phys_addr <= phys_addr) &&
  730. (phys_addr < (md->phys_addr +
  731. (md->num_pages << EFI_PAGE_SHIFT))))
  732. return md->attribute;
  733. }
  734. return 0;
  735. }
  736. /*
  737. * efi_mem_type - lookup memmap type for physical address
  738. * @phys_addr: the physical address to lookup
  739. *
  740. * Search in the EFI memory map for the region covering @phys_addr.
  741. * Returns the EFI memory type if the region was found in the memory
  742. * map, EFI_RESERVED_TYPE (zero) otherwise.
  743. */
  744. int efi_mem_type(unsigned long phys_addr)
  745. {
  746. const efi_memory_desc_t *md;
  747. if (!efi_enabled(EFI_MEMMAP))
  748. return -ENOTSUPP;
  749. for_each_efi_memory_desc(md) {
  750. if ((md->phys_addr <= phys_addr) &&
  751. (phys_addr < (md->phys_addr +
  752. (md->num_pages << EFI_PAGE_SHIFT))))
  753. return md->type;
  754. }
  755. return -EINVAL;
  756. }
  757. #endif
  758. int efi_status_to_err(efi_status_t status)
  759. {
  760. int err;
  761. switch (status) {
  762. case EFI_SUCCESS:
  763. err = 0;
  764. break;
  765. case EFI_INVALID_PARAMETER:
  766. err = -EINVAL;
  767. break;
  768. case EFI_OUT_OF_RESOURCES:
  769. err = -ENOSPC;
  770. break;
  771. case EFI_DEVICE_ERROR:
  772. err = -EIO;
  773. break;
  774. case EFI_WRITE_PROTECTED:
  775. err = -EROFS;
  776. break;
  777. case EFI_SECURITY_VIOLATION:
  778. err = -EACCES;
  779. break;
  780. case EFI_NOT_FOUND:
  781. err = -ENOENT;
  782. break;
  783. case EFI_ABORTED:
  784. err = -EINTR;
  785. break;
  786. default:
  787. err = -EINVAL;
  788. }
  789. return err;
  790. }
  791. bool efi_is_table_address(unsigned long phys_addr)
  792. {
  793. unsigned int i;
  794. if (phys_addr == EFI_INVALID_TABLE_ADDR)
  795. return false;
  796. for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
  797. if (*(efi_tables[i]) == phys_addr)
  798. return true;
  799. return false;
  800. }
  801. #ifdef CONFIG_KEXEC
  802. static int update_efi_random_seed(struct notifier_block *nb,
  803. unsigned long code, void *unused)
  804. {
  805. struct linux_efi_random_seed *seed;
  806. u32 size = 0;
  807. if (!kexec_in_progress)
  808. return NOTIFY_DONE;
  809. seed = memremap(efi.rng_seed, sizeof(*seed), MEMREMAP_WB);
  810. if (seed != NULL) {
  811. size = min(seed->size, EFI_RANDOM_SEED_SIZE);
  812. memunmap(seed);
  813. } else {
  814. pr_err("Could not map UEFI random seed!\n");
  815. }
  816. if (size > 0) {
  817. seed = memremap(efi.rng_seed, sizeof(*seed) + size,
  818. MEMREMAP_WB);
  819. if (seed != NULL) {
  820. seed->size = size;
  821. get_random_bytes(seed->bits, seed->size);
  822. memunmap(seed);
  823. } else {
  824. pr_err("Could not map UEFI random seed!\n");
  825. }
  826. }
  827. return NOTIFY_DONE;
  828. }
  829. static struct notifier_block efi_random_seed_nb = {
  830. .notifier_call = update_efi_random_seed,
  831. };
  832. static int register_update_efi_random_seed(void)
  833. {
  834. if (efi.rng_seed == EFI_INVALID_TABLE_ADDR)
  835. return 0;
  836. return register_reboot_notifier(&efi_random_seed_nb);
  837. }
  838. late_initcall(register_update_efi_random_seed);
  839. #endif