efi.c 11 KB

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