efi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/acpi.h>
  27. #include <linux/ucs2_string.h>
  28. #include <linux/memblock.h>
  29. #include <asm/early_ioremap.h>
  30. struct efi __read_mostly efi = {
  31. .mps = EFI_INVALID_TABLE_ADDR,
  32. .acpi = EFI_INVALID_TABLE_ADDR,
  33. .acpi20 = EFI_INVALID_TABLE_ADDR,
  34. .smbios = EFI_INVALID_TABLE_ADDR,
  35. .smbios3 = EFI_INVALID_TABLE_ADDR,
  36. .sal_systab = EFI_INVALID_TABLE_ADDR,
  37. .boot_info = EFI_INVALID_TABLE_ADDR,
  38. .hcdp = EFI_INVALID_TABLE_ADDR,
  39. .uga = EFI_INVALID_TABLE_ADDR,
  40. .uv_systab = EFI_INVALID_TABLE_ADDR,
  41. .fw_vendor = EFI_INVALID_TABLE_ADDR,
  42. .runtime = EFI_INVALID_TABLE_ADDR,
  43. .config_table = EFI_INVALID_TABLE_ADDR,
  44. .esrt = EFI_INVALID_TABLE_ADDR,
  45. .properties_table = EFI_INVALID_TABLE_ADDR,
  46. .mem_attr_table = EFI_INVALID_TABLE_ADDR,
  47. };
  48. EXPORT_SYMBOL(efi);
  49. static bool disable_runtime;
  50. static int __init setup_noefi(char *arg)
  51. {
  52. disable_runtime = true;
  53. return 0;
  54. }
  55. early_param("noefi", setup_noefi);
  56. bool efi_runtime_disabled(void)
  57. {
  58. return disable_runtime;
  59. }
  60. static int __init parse_efi_cmdline(char *str)
  61. {
  62. if (!str) {
  63. pr_warn("need at least one option\n");
  64. return -EINVAL;
  65. }
  66. if (parse_option_str(str, "debug"))
  67. set_bit(EFI_DBG, &efi.flags);
  68. if (parse_option_str(str, "noruntime"))
  69. disable_runtime = true;
  70. return 0;
  71. }
  72. early_param("efi", parse_efi_cmdline);
  73. struct kobject *efi_kobj;
  74. /*
  75. * Let's not leave out systab information that snuck into
  76. * the efivars driver
  77. */
  78. static ssize_t systab_show(struct kobject *kobj,
  79. struct kobj_attribute *attr, char *buf)
  80. {
  81. char *str = buf;
  82. if (!kobj || !buf)
  83. return -EINVAL;
  84. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  85. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  86. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  87. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  88. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  89. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  90. /*
  91. * If both SMBIOS and SMBIOS3 entry points are implemented, the
  92. * SMBIOS3 entry point shall be preferred, so we list it first to
  93. * let applications stop parsing after the first match.
  94. */
  95. if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
  96. str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
  97. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  98. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  99. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  100. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  101. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  102. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  103. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  104. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  105. return str - buf;
  106. }
  107. static struct kobj_attribute efi_attr_systab =
  108. __ATTR(systab, 0400, systab_show, NULL);
  109. #define EFI_FIELD(var) efi.var
  110. #define EFI_ATTR_SHOW(name) \
  111. static ssize_t name##_show(struct kobject *kobj, \
  112. struct kobj_attribute *attr, char *buf) \
  113. { \
  114. return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
  115. }
  116. EFI_ATTR_SHOW(fw_vendor);
  117. EFI_ATTR_SHOW(runtime);
  118. EFI_ATTR_SHOW(config_table);
  119. static ssize_t fw_platform_size_show(struct kobject *kobj,
  120. struct kobj_attribute *attr, char *buf)
  121. {
  122. return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
  123. }
  124. static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
  125. static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
  126. static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
  127. static struct kobj_attribute efi_attr_fw_platform_size =
  128. __ATTR_RO(fw_platform_size);
  129. static struct attribute *efi_subsys_attrs[] = {
  130. &efi_attr_systab.attr,
  131. &efi_attr_fw_vendor.attr,
  132. &efi_attr_runtime.attr,
  133. &efi_attr_config_table.attr,
  134. &efi_attr_fw_platform_size.attr,
  135. NULL,
  136. };
  137. static umode_t efi_attr_is_visible(struct kobject *kobj,
  138. struct attribute *attr, int n)
  139. {
  140. if (attr == &efi_attr_fw_vendor.attr) {
  141. if (efi_enabled(EFI_PARAVIRT) ||
  142. efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
  143. return 0;
  144. } else if (attr == &efi_attr_runtime.attr) {
  145. if (efi.runtime == EFI_INVALID_TABLE_ADDR)
  146. return 0;
  147. } else if (attr == &efi_attr_config_table.attr) {
  148. if (efi.config_table == EFI_INVALID_TABLE_ADDR)
  149. return 0;
  150. }
  151. return attr->mode;
  152. }
  153. static struct attribute_group efi_subsys_attr_group = {
  154. .attrs = efi_subsys_attrs,
  155. .is_visible = efi_attr_is_visible,
  156. };
  157. static struct efivars generic_efivars;
  158. static struct efivar_operations generic_ops;
  159. static int generic_ops_register(void)
  160. {
  161. generic_ops.get_variable = efi.get_variable;
  162. generic_ops.set_variable = efi.set_variable;
  163. generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
  164. generic_ops.get_next_variable = efi.get_next_variable;
  165. generic_ops.query_variable_store = efi_query_variable_store;
  166. return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  167. }
  168. static void generic_ops_unregister(void)
  169. {
  170. efivars_unregister(&generic_efivars);
  171. }
  172. #if IS_ENABLED(CONFIG_ACPI)
  173. #define EFIVAR_SSDT_NAME_MAX 16
  174. static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
  175. static int __init efivar_ssdt_setup(char *str)
  176. {
  177. if (strlen(str) < sizeof(efivar_ssdt))
  178. memcpy(efivar_ssdt, str, strlen(str));
  179. else
  180. pr_warn("efivar_ssdt: name too long: %s\n", str);
  181. return 0;
  182. }
  183. __setup("efivar_ssdt=", efivar_ssdt_setup);
  184. static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor,
  185. unsigned long name_size, void *data)
  186. {
  187. struct efivar_entry *entry;
  188. struct list_head *list = data;
  189. char utf8_name[EFIVAR_SSDT_NAME_MAX];
  190. int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size);
  191. ucs2_as_utf8(utf8_name, name, limit - 1);
  192. if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
  193. return 0;
  194. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  195. if (!entry)
  196. return 0;
  197. memcpy(entry->var.VariableName, name, name_size);
  198. memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t));
  199. efivar_entry_add(entry, list);
  200. return 0;
  201. }
  202. static __init int efivar_ssdt_load(void)
  203. {
  204. LIST_HEAD(entries);
  205. struct efivar_entry *entry, *aux;
  206. unsigned long size;
  207. void *data;
  208. int ret;
  209. ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries);
  210. list_for_each_entry_safe(entry, aux, &entries, list) {
  211. pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt,
  212. &entry->var.VendorGuid);
  213. list_del(&entry->list);
  214. ret = efivar_entry_size(entry, &size);
  215. if (ret) {
  216. pr_err("failed to get var size\n");
  217. goto free_entry;
  218. }
  219. data = kmalloc(size, GFP_KERNEL);
  220. if (!data) {
  221. ret = -ENOMEM;
  222. goto free_entry;
  223. }
  224. ret = efivar_entry_get(entry, NULL, &size, data);
  225. if (ret) {
  226. pr_err("failed to get var data\n");
  227. goto free_data;
  228. }
  229. ret = acpi_load_table(data);
  230. if (ret) {
  231. pr_err("failed to load table: %d\n", ret);
  232. goto free_data;
  233. }
  234. goto free_entry;
  235. free_data:
  236. kfree(data);
  237. free_entry:
  238. kfree(entry);
  239. }
  240. return ret;
  241. }
  242. #else
  243. static inline int efivar_ssdt_load(void) { return 0; }
  244. #endif
  245. /*
  246. * We register the efi subsystem with the firmware subsystem and the
  247. * efivars subsystem with the efi subsystem, if the system was booted with
  248. * EFI.
  249. */
  250. static int __init efisubsys_init(void)
  251. {
  252. int error;
  253. if (!efi_enabled(EFI_BOOT))
  254. return 0;
  255. /* We register the efi directory at /sys/firmware/efi */
  256. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  257. if (!efi_kobj) {
  258. pr_err("efi: Firmware registration failed.\n");
  259. return -ENOMEM;
  260. }
  261. error = generic_ops_register();
  262. if (error)
  263. goto err_put;
  264. if (efi_enabled(EFI_RUNTIME_SERVICES))
  265. efivar_ssdt_load();
  266. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  267. if (error) {
  268. pr_err("efi: Sysfs attribute export failed with error %d.\n",
  269. error);
  270. goto err_unregister;
  271. }
  272. error = efi_runtime_map_init(efi_kobj);
  273. if (error)
  274. goto err_remove_group;
  275. /* and the standard mountpoint for efivarfs */
  276. error = sysfs_create_mount_point(efi_kobj, "efivars");
  277. if (error) {
  278. pr_err("efivars: Subsystem registration failed.\n");
  279. goto err_remove_group;
  280. }
  281. return 0;
  282. err_remove_group:
  283. sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
  284. err_unregister:
  285. generic_ops_unregister();
  286. err_put:
  287. kobject_put(efi_kobj);
  288. return error;
  289. }
  290. subsys_initcall(efisubsys_init);
  291. /*
  292. * Find the efi memory descriptor for a given physical address. Given a
  293. * physical address, determine if it exists within an EFI Memory Map entry,
  294. * and if so, populate the supplied memory descriptor with the appropriate
  295. * data.
  296. */
  297. int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
  298. {
  299. efi_memory_desc_t *md;
  300. if (!efi_enabled(EFI_MEMMAP)) {
  301. pr_err_once("EFI_MEMMAP is not enabled.\n");
  302. return -EINVAL;
  303. }
  304. if (!out_md) {
  305. pr_err_once("out_md is null.\n");
  306. return -EINVAL;
  307. }
  308. for_each_efi_memory_desc(md) {
  309. u64 size;
  310. u64 end;
  311. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  312. md->type != EFI_BOOT_SERVICES_DATA &&
  313. md->type != EFI_RUNTIME_SERVICES_DATA) {
  314. continue;
  315. }
  316. size = md->num_pages << EFI_PAGE_SHIFT;
  317. end = md->phys_addr + size;
  318. if (phys_addr >= md->phys_addr && phys_addr < end) {
  319. memcpy(out_md, md, sizeof(*out_md));
  320. return 0;
  321. }
  322. }
  323. pr_err_once("requested map not found.\n");
  324. return -ENOENT;
  325. }
  326. /*
  327. * Calculate the highest address of an efi memory descriptor.
  328. */
  329. u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
  330. {
  331. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  332. u64 end = md->phys_addr + size;
  333. return end;
  334. }
  335. void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
  336. /**
  337. * efi_mem_reserve - Reserve an EFI memory region
  338. * @addr: Physical address to reserve
  339. * @size: Size of reservation
  340. *
  341. * Mark a region as reserved from general kernel allocation and
  342. * prevent it being released by efi_free_boot_services().
  343. *
  344. * This function should be called drivers once they've parsed EFI
  345. * configuration tables to figure out where their data lives, e.g.
  346. * efi_esrt_init().
  347. */
  348. void __init efi_mem_reserve(phys_addr_t addr, u64 size)
  349. {
  350. if (!memblock_is_region_reserved(addr, size))
  351. memblock_reserve(addr, size);
  352. /*
  353. * Some architectures (x86) reserve all boot services ranges
  354. * until efi_free_boot_services() because of buggy firmware
  355. * implementations. This means the above memblock_reserve() is
  356. * superfluous on x86 and instead what it needs to do is
  357. * ensure the @start, @size is not freed.
  358. */
  359. efi_arch_mem_reserve(addr, size);
  360. }
  361. static __initdata efi_config_table_type_t common_tables[] = {
  362. {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
  363. {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
  364. {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
  365. {MPS_TABLE_GUID, "MPS", &efi.mps},
  366. {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
  367. {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
  368. {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
  369. {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
  370. {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
  371. {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
  372. {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
  373. {NULL_GUID, NULL, NULL},
  374. };
  375. static __init int match_config_table(efi_guid_t *guid,
  376. unsigned long table,
  377. efi_config_table_type_t *table_types)
  378. {
  379. int i;
  380. if (table_types) {
  381. for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
  382. if (!efi_guidcmp(*guid, table_types[i].guid)) {
  383. *(table_types[i].ptr) = table;
  384. if (table_types[i].name)
  385. pr_cont(" %s=0x%lx ",
  386. table_types[i].name, table);
  387. return 1;
  388. }
  389. }
  390. }
  391. return 0;
  392. }
  393. int __init efi_config_parse_tables(void *config_tables, int count, int sz,
  394. efi_config_table_type_t *arch_tables)
  395. {
  396. void *tablep;
  397. int i;
  398. tablep = config_tables;
  399. pr_info("");
  400. for (i = 0; i < count; i++) {
  401. efi_guid_t guid;
  402. unsigned long table;
  403. if (efi_enabled(EFI_64BIT)) {
  404. u64 table64;
  405. guid = ((efi_config_table_64_t *)tablep)->guid;
  406. table64 = ((efi_config_table_64_t *)tablep)->table;
  407. table = table64;
  408. #ifndef CONFIG_64BIT
  409. if (table64 >> 32) {
  410. pr_cont("\n");
  411. pr_err("Table located above 4GB, disabling EFI.\n");
  412. return -EINVAL;
  413. }
  414. #endif
  415. } else {
  416. guid = ((efi_config_table_32_t *)tablep)->guid;
  417. table = ((efi_config_table_32_t *)tablep)->table;
  418. }
  419. if (!match_config_table(&guid, table, common_tables))
  420. match_config_table(&guid, table, arch_tables);
  421. tablep += sz;
  422. }
  423. pr_cont("\n");
  424. set_bit(EFI_CONFIG_TABLES, &efi.flags);
  425. /* Parse the EFI Properties table if it exists */
  426. if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
  427. efi_properties_table_t *tbl;
  428. tbl = early_memremap(efi.properties_table, sizeof(*tbl));
  429. if (tbl == NULL) {
  430. pr_err("Could not map Properties table!\n");
  431. return -ENOMEM;
  432. }
  433. if (tbl->memory_protection_attribute &
  434. EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
  435. set_bit(EFI_NX_PE_DATA, &efi.flags);
  436. early_memunmap(tbl, sizeof(*tbl));
  437. }
  438. return 0;
  439. }
  440. int __init efi_config_init(efi_config_table_type_t *arch_tables)
  441. {
  442. void *config_tables;
  443. int sz, ret;
  444. if (efi_enabled(EFI_64BIT))
  445. sz = sizeof(efi_config_table_64_t);
  446. else
  447. sz = sizeof(efi_config_table_32_t);
  448. /*
  449. * Let's see what config tables the firmware passed to us.
  450. */
  451. config_tables = early_memremap(efi.systab->tables,
  452. efi.systab->nr_tables * sz);
  453. if (config_tables == NULL) {
  454. pr_err("Could not map Configuration table!\n");
  455. return -ENOMEM;
  456. }
  457. ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
  458. arch_tables);
  459. early_memunmap(config_tables, efi.systab->nr_tables * sz);
  460. return ret;
  461. }
  462. #ifdef CONFIG_EFI_VARS_MODULE
  463. static int __init efi_load_efivars(void)
  464. {
  465. struct platform_device *pdev;
  466. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  467. return 0;
  468. pdev = platform_device_register_simple("efivars", 0, NULL, 0);
  469. return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
  470. }
  471. device_initcall(efi_load_efivars);
  472. #endif
  473. #ifdef CONFIG_EFI_PARAMS_FROM_FDT
  474. #define UEFI_PARAM(name, prop, field) \
  475. { \
  476. { name }, \
  477. { prop }, \
  478. offsetof(struct efi_fdt_params, field), \
  479. FIELD_SIZEOF(struct efi_fdt_params, field) \
  480. }
  481. struct params {
  482. const char name[32];
  483. const char propname[32];
  484. int offset;
  485. int size;
  486. };
  487. static __initdata struct params fdt_params[] = {
  488. UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
  489. UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
  490. UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
  491. UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
  492. UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
  493. };
  494. static __initdata struct params xen_fdt_params[] = {
  495. UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
  496. UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
  497. UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
  498. UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
  499. UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
  500. };
  501. #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
  502. static __initdata struct {
  503. const char *uname;
  504. const char *subnode;
  505. struct params *params;
  506. } dt_params[] = {
  507. { "hypervisor", "uefi", xen_fdt_params },
  508. { "chosen", NULL, fdt_params },
  509. };
  510. struct param_info {
  511. int found;
  512. void *params;
  513. const char *missing;
  514. };
  515. static int __init __find_uefi_params(unsigned long node,
  516. struct param_info *info,
  517. struct params *params)
  518. {
  519. const void *prop;
  520. void *dest;
  521. u64 val;
  522. int i, len;
  523. for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
  524. prop = of_get_flat_dt_prop(node, params[i].propname, &len);
  525. if (!prop) {
  526. info->missing = params[i].name;
  527. return 0;
  528. }
  529. dest = info->params + params[i].offset;
  530. info->found++;
  531. val = of_read_number(prop, len / sizeof(u32));
  532. if (params[i].size == sizeof(u32))
  533. *(u32 *)dest = val;
  534. else
  535. *(u64 *)dest = val;
  536. if (efi_enabled(EFI_DBG))
  537. pr_info(" %s: 0x%0*llx\n", params[i].name,
  538. params[i].size * 2, val);
  539. }
  540. return 1;
  541. }
  542. static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
  543. int depth, void *data)
  544. {
  545. struct param_info *info = data;
  546. int i;
  547. for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
  548. const char *subnode = dt_params[i].subnode;
  549. if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
  550. info->missing = dt_params[i].params[0].name;
  551. continue;
  552. }
  553. if (subnode) {
  554. int err = of_get_flat_dt_subnode_by_name(node, subnode);
  555. if (err < 0)
  556. return 0;
  557. node = err;
  558. }
  559. return __find_uefi_params(node, info, dt_params[i].params);
  560. }
  561. return 0;
  562. }
  563. int __init efi_get_fdt_params(struct efi_fdt_params *params)
  564. {
  565. struct param_info info;
  566. int ret;
  567. pr_info("Getting EFI parameters from FDT:\n");
  568. info.found = 0;
  569. info.params = params;
  570. ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
  571. if (!info.found)
  572. pr_info("UEFI not found.\n");
  573. else if (!ret)
  574. pr_err("Can't find '%s' in device tree!\n",
  575. info.missing);
  576. return ret;
  577. }
  578. #endif /* CONFIG_EFI_PARAMS_FROM_FDT */
  579. static __initdata char memory_type_name[][20] = {
  580. "Reserved",
  581. "Loader Code",
  582. "Loader Data",
  583. "Boot Code",
  584. "Boot Data",
  585. "Runtime Code",
  586. "Runtime Data",
  587. "Conventional Memory",
  588. "Unusable Memory",
  589. "ACPI Reclaim Memory",
  590. "ACPI Memory NVS",
  591. "Memory Mapped I/O",
  592. "MMIO Port Space",
  593. "PAL Code",
  594. "Persistent Memory",
  595. };
  596. char * __init efi_md_typeattr_format(char *buf, size_t size,
  597. const efi_memory_desc_t *md)
  598. {
  599. char *pos;
  600. int type_len;
  601. u64 attr;
  602. pos = buf;
  603. if (md->type >= ARRAY_SIZE(memory_type_name))
  604. type_len = snprintf(pos, size, "[type=%u", md->type);
  605. else
  606. type_len = snprintf(pos, size, "[%-*s",
  607. (int)(sizeof(memory_type_name[0]) - 1),
  608. memory_type_name[md->type]);
  609. if (type_len >= size)
  610. return buf;
  611. pos += type_len;
  612. size -= type_len;
  613. attr = md->attribute;
  614. if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
  615. EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
  616. EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
  617. EFI_MEMORY_NV |
  618. EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
  619. snprintf(pos, size, "|attr=0x%016llx]",
  620. (unsigned long long)attr);
  621. else
  622. snprintf(pos, size,
  623. "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
  624. attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
  625. attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
  626. attr & EFI_MEMORY_NV ? "NV" : "",
  627. attr & EFI_MEMORY_XP ? "XP" : "",
  628. attr & EFI_MEMORY_RP ? "RP" : "",
  629. attr & EFI_MEMORY_WP ? "WP" : "",
  630. attr & EFI_MEMORY_RO ? "RO" : "",
  631. attr & EFI_MEMORY_UCE ? "UCE" : "",
  632. attr & EFI_MEMORY_WB ? "WB" : "",
  633. attr & EFI_MEMORY_WT ? "WT" : "",
  634. attr & EFI_MEMORY_WC ? "WC" : "",
  635. attr & EFI_MEMORY_UC ? "UC" : "");
  636. return buf;
  637. }
  638. /*
  639. * efi_mem_attributes - lookup memmap attributes for physical address
  640. * @phys_addr: the physical address to lookup
  641. *
  642. * Search in the EFI memory map for the region covering
  643. * @phys_addr. Returns the EFI memory attributes if the region
  644. * was found in the memory map, 0 otherwise.
  645. *
  646. * Despite being marked __weak, most architectures should *not*
  647. * override this function. It is __weak solely for the benefit
  648. * of ia64 which has a funky EFI memory map that doesn't work
  649. * the same way as other architectures.
  650. */
  651. u64 __weak efi_mem_attributes(unsigned long phys_addr)
  652. {
  653. efi_memory_desc_t *md;
  654. if (!efi_enabled(EFI_MEMMAP))
  655. return 0;
  656. for_each_efi_memory_desc(md) {
  657. if ((md->phys_addr <= phys_addr) &&
  658. (phys_addr < (md->phys_addr +
  659. (md->num_pages << EFI_PAGE_SHIFT))))
  660. return md->attribute;
  661. }
  662. return 0;
  663. }
  664. int efi_status_to_err(efi_status_t status)
  665. {
  666. int err;
  667. switch (status) {
  668. case EFI_SUCCESS:
  669. err = 0;
  670. break;
  671. case EFI_INVALID_PARAMETER:
  672. err = -EINVAL;
  673. break;
  674. case EFI_OUT_OF_RESOURCES:
  675. err = -ENOSPC;
  676. break;
  677. case EFI_DEVICE_ERROR:
  678. err = -EIO;
  679. break;
  680. case EFI_WRITE_PROTECTED:
  681. err = -EROFS;
  682. break;
  683. case EFI_SECURITY_VIOLATION:
  684. err = -EACCES;
  685. break;
  686. case EFI_NOT_FOUND:
  687. err = -ENOENT;
  688. break;
  689. case EFI_ABORTED:
  690. err = -EINTR;
  691. break;
  692. default:
  693. err = -EINVAL;
  694. }
  695. return err;
  696. }