efi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. struct efi __read_mostly efi = {
  25. .mps = EFI_INVALID_TABLE_ADDR,
  26. .acpi = EFI_INVALID_TABLE_ADDR,
  27. .acpi20 = EFI_INVALID_TABLE_ADDR,
  28. .smbios = EFI_INVALID_TABLE_ADDR,
  29. .sal_systab = EFI_INVALID_TABLE_ADDR,
  30. .boot_info = EFI_INVALID_TABLE_ADDR,
  31. .hcdp = EFI_INVALID_TABLE_ADDR,
  32. .uga = EFI_INVALID_TABLE_ADDR,
  33. .uv_systab = EFI_INVALID_TABLE_ADDR,
  34. .fw_vendor = EFI_INVALID_TABLE_ADDR,
  35. .runtime = EFI_INVALID_TABLE_ADDR,
  36. .config_table = EFI_INVALID_TABLE_ADDR,
  37. };
  38. EXPORT_SYMBOL(efi);
  39. static struct kobject *efi_kobj;
  40. static struct kobject *efivars_kobj;
  41. /*
  42. * Let's not leave out systab information that snuck into
  43. * the efivars driver
  44. */
  45. static ssize_t systab_show(struct kobject *kobj,
  46. struct kobj_attribute *attr, char *buf)
  47. {
  48. char *str = buf;
  49. if (!kobj || !buf)
  50. return -EINVAL;
  51. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  52. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  53. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  54. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  55. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  56. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  57. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  58. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  59. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  60. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  61. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  62. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  63. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  64. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  65. return str - buf;
  66. }
  67. static struct kobj_attribute efi_attr_systab =
  68. __ATTR(systab, 0400, systab_show, NULL);
  69. #define EFI_FIELD(var) efi.var
  70. #define EFI_ATTR_SHOW(name) \
  71. static ssize_t name##_show(struct kobject *kobj, \
  72. struct kobj_attribute *attr, char *buf) \
  73. { \
  74. return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
  75. }
  76. EFI_ATTR_SHOW(fw_vendor);
  77. EFI_ATTR_SHOW(runtime);
  78. EFI_ATTR_SHOW(config_table);
  79. static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
  80. static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
  81. static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
  82. static struct attribute *efi_subsys_attrs[] = {
  83. &efi_attr_systab.attr,
  84. &efi_attr_fw_vendor.attr,
  85. &efi_attr_runtime.attr,
  86. &efi_attr_config_table.attr,
  87. NULL,
  88. };
  89. static umode_t efi_attr_is_visible(struct kobject *kobj,
  90. struct attribute *attr, int n)
  91. {
  92. umode_t mode = attr->mode;
  93. if (attr == &efi_attr_fw_vendor.attr)
  94. return (efi.fw_vendor == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
  95. else if (attr == &efi_attr_runtime.attr)
  96. return (efi.runtime == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
  97. else if (attr == &efi_attr_config_table.attr)
  98. return (efi.config_table == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
  99. return mode;
  100. }
  101. static struct attribute_group efi_subsys_attr_group = {
  102. .attrs = efi_subsys_attrs,
  103. .is_visible = efi_attr_is_visible,
  104. };
  105. static struct efivars generic_efivars;
  106. static struct efivar_operations generic_ops;
  107. static int generic_ops_register(void)
  108. {
  109. generic_ops.get_variable = efi.get_variable;
  110. generic_ops.set_variable = efi.set_variable;
  111. generic_ops.get_next_variable = efi.get_next_variable;
  112. generic_ops.query_variable_store = efi_query_variable_store;
  113. return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  114. }
  115. static void generic_ops_unregister(void)
  116. {
  117. efivars_unregister(&generic_efivars);
  118. }
  119. /*
  120. * We register the efi subsystem with the firmware subsystem and the
  121. * efivars subsystem with the efi subsystem, if the system was booted with
  122. * EFI.
  123. */
  124. static int __init efisubsys_init(void)
  125. {
  126. int error;
  127. if (!efi_enabled(EFI_BOOT))
  128. return 0;
  129. /* We register the efi directory at /sys/firmware/efi */
  130. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  131. if (!efi_kobj) {
  132. pr_err("efi: Firmware registration failed.\n");
  133. return -ENOMEM;
  134. }
  135. error = generic_ops_register();
  136. if (error)
  137. goto err_put;
  138. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  139. if (error) {
  140. pr_err("efi: Sysfs attribute export failed with error %d.\n",
  141. error);
  142. goto err_unregister;
  143. }
  144. error = efi_runtime_map_init(efi_kobj);
  145. if (error)
  146. goto err_remove_group;
  147. /* and the standard mountpoint for efivarfs */
  148. efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
  149. if (!efivars_kobj) {
  150. pr_err("efivars: Subsystem registration failed.\n");
  151. error = -ENOMEM;
  152. goto err_remove_group;
  153. }
  154. return 0;
  155. err_remove_group:
  156. sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
  157. err_unregister:
  158. generic_ops_unregister();
  159. err_put:
  160. kobject_put(efi_kobj);
  161. return error;
  162. }
  163. subsys_initcall(efisubsys_init);
  164. /*
  165. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  166. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  167. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  168. */
  169. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  170. {
  171. struct efi_memory_map *map;
  172. void *p;
  173. map = efi.memmap;
  174. if (!map)
  175. return NULL;
  176. if (WARN_ON(!map->map))
  177. return NULL;
  178. for (p = map->map; p < map->map_end; p += map->desc_size) {
  179. efi_memory_desc_t *md = p;
  180. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  181. u64 end = md->phys_addr + size;
  182. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  183. md->type != EFI_BOOT_SERVICES_CODE &&
  184. md->type != EFI_BOOT_SERVICES_DATA)
  185. continue;
  186. if (!md->virt_addr)
  187. continue;
  188. if (phys_addr >= md->phys_addr && phys_addr < end) {
  189. phys_addr += md->virt_addr - md->phys_addr;
  190. return (__force void __iomem *)(unsigned long)phys_addr;
  191. }
  192. }
  193. return NULL;
  194. }
  195. static __initdata efi_config_table_type_t common_tables[] = {
  196. {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
  197. {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
  198. {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
  199. {MPS_TABLE_GUID, "MPS", &efi.mps},
  200. {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
  201. {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
  202. {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
  203. {NULL_GUID, NULL, NULL},
  204. };
  205. static __init int match_config_table(efi_guid_t *guid,
  206. unsigned long table,
  207. efi_config_table_type_t *table_types)
  208. {
  209. u8 str[EFI_VARIABLE_GUID_LEN + 1];
  210. int i;
  211. if (table_types) {
  212. efi_guid_unparse(guid, str);
  213. for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
  214. efi_guid_unparse(&table_types[i].guid, str);
  215. if (!efi_guidcmp(*guid, table_types[i].guid)) {
  216. *(table_types[i].ptr) = table;
  217. pr_cont(" %s=0x%lx ",
  218. table_types[i].name, table);
  219. return 1;
  220. }
  221. }
  222. }
  223. return 0;
  224. }
  225. int __init efi_config_init(efi_config_table_type_t *arch_tables)
  226. {
  227. void *config_tables, *tablep;
  228. int i, sz;
  229. if (efi_enabled(EFI_64BIT))
  230. sz = sizeof(efi_config_table_64_t);
  231. else
  232. sz = sizeof(efi_config_table_32_t);
  233. /*
  234. * Let's see what config tables the firmware passed to us.
  235. */
  236. config_tables = early_memremap(efi.systab->tables,
  237. efi.systab->nr_tables * sz);
  238. if (config_tables == NULL) {
  239. pr_err("Could not map Configuration table!\n");
  240. return -ENOMEM;
  241. }
  242. tablep = config_tables;
  243. pr_info("");
  244. for (i = 0; i < efi.systab->nr_tables; i++) {
  245. efi_guid_t guid;
  246. unsigned long table;
  247. if (efi_enabled(EFI_64BIT)) {
  248. u64 table64;
  249. guid = ((efi_config_table_64_t *)tablep)->guid;
  250. table64 = ((efi_config_table_64_t *)tablep)->table;
  251. table = table64;
  252. #ifndef CONFIG_64BIT
  253. if (table64 >> 32) {
  254. pr_cont("\n");
  255. pr_err("Table located above 4GB, disabling EFI.\n");
  256. early_iounmap(config_tables,
  257. efi.systab->nr_tables * sz);
  258. return -EINVAL;
  259. }
  260. #endif
  261. } else {
  262. guid = ((efi_config_table_32_t *)tablep)->guid;
  263. table = ((efi_config_table_32_t *)tablep)->table;
  264. }
  265. if (!match_config_table(&guid, table, common_tables))
  266. match_config_table(&guid, table, arch_tables);
  267. tablep += sz;
  268. }
  269. pr_cont("\n");
  270. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  271. set_bit(EFI_CONFIG_TABLES, &efi.flags);
  272. return 0;
  273. }
  274. #ifdef CONFIG_EFI_PARAMS_FROM_FDT
  275. #define UEFI_PARAM(name, prop, field) \
  276. { \
  277. { name }, \
  278. { prop }, \
  279. offsetof(struct efi_fdt_params, field), \
  280. FIELD_SIZEOF(struct efi_fdt_params, field) \
  281. }
  282. static __initdata struct {
  283. const char name[32];
  284. const char propname[32];
  285. int offset;
  286. int size;
  287. } dt_params[] = {
  288. UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
  289. UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
  290. UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
  291. UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
  292. UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
  293. };
  294. struct param_info {
  295. int verbose;
  296. int found;
  297. void *params;
  298. };
  299. static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
  300. int depth, void *data)
  301. {
  302. struct param_info *info = data;
  303. const void *prop;
  304. void *dest;
  305. u64 val;
  306. int i, len;
  307. if (depth != 1 ||
  308. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  309. return 0;
  310. for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
  311. prop = of_get_flat_dt_prop(node, dt_params[i].propname, &len);
  312. if (!prop)
  313. return 0;
  314. dest = info->params + dt_params[i].offset;
  315. info->found++;
  316. val = of_read_number(prop, len / sizeof(u32));
  317. if (dt_params[i].size == sizeof(u32))
  318. *(u32 *)dest = val;
  319. else
  320. *(u64 *)dest = val;
  321. if (info->verbose)
  322. pr_info(" %s: 0x%0*llx\n", dt_params[i].name,
  323. dt_params[i].size * 2, val);
  324. }
  325. return 1;
  326. }
  327. int __init efi_get_fdt_params(struct efi_fdt_params *params, int verbose)
  328. {
  329. struct param_info info;
  330. int ret;
  331. pr_info("Getting EFI parameters from FDT:\n");
  332. info.verbose = verbose;
  333. info.found = 0;
  334. info.params = params;
  335. ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
  336. if (!info.found)
  337. pr_info("UEFI not found.\n");
  338. else if (!ret)
  339. pr_err("Can't find '%s' in device tree!\n",
  340. dt_params[info.found].name);
  341. return ret;
  342. }
  343. #endif /* CONFIG_EFI_PARAMS_FROM_FDT */