efi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/io.h>
  22. struct efi __read_mostly efi = {
  23. .mps = EFI_INVALID_TABLE_ADDR,
  24. .acpi = EFI_INVALID_TABLE_ADDR,
  25. .acpi20 = EFI_INVALID_TABLE_ADDR,
  26. .smbios = EFI_INVALID_TABLE_ADDR,
  27. .sal_systab = EFI_INVALID_TABLE_ADDR,
  28. .boot_info = EFI_INVALID_TABLE_ADDR,
  29. .hcdp = EFI_INVALID_TABLE_ADDR,
  30. .uga = EFI_INVALID_TABLE_ADDR,
  31. .uv_systab = EFI_INVALID_TABLE_ADDR,
  32. .fw_vendor = EFI_INVALID_TABLE_ADDR,
  33. .runtime = EFI_INVALID_TABLE_ADDR,
  34. .config_table = EFI_INVALID_TABLE_ADDR,
  35. };
  36. EXPORT_SYMBOL(efi);
  37. static struct kobject *efi_kobj;
  38. static struct kobject *efivars_kobj;
  39. /*
  40. * Let's not leave out systab information that snuck into
  41. * the efivars driver
  42. */
  43. static ssize_t systab_show(struct kobject *kobj,
  44. struct kobj_attribute *attr, char *buf)
  45. {
  46. char *str = buf;
  47. if (!kobj || !buf)
  48. return -EINVAL;
  49. if (efi.mps != EFI_INVALID_TABLE_ADDR)
  50. str += sprintf(str, "MPS=0x%lx\n", efi.mps);
  51. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  52. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  53. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  54. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  55. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  56. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  57. if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
  58. str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
  59. if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
  60. str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
  61. if (efi.uga != EFI_INVALID_TABLE_ADDR)
  62. str += sprintf(str, "UGA=0x%lx\n", efi.uga);
  63. return str - buf;
  64. }
  65. static struct kobj_attribute efi_attr_systab =
  66. __ATTR(systab, 0400, systab_show, NULL);
  67. #define EFI_FIELD(var) efi.var
  68. #define EFI_ATTR_SHOW(name) \
  69. static ssize_t name##_show(struct kobject *kobj, \
  70. struct kobj_attribute *attr, char *buf) \
  71. { \
  72. return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
  73. }
  74. EFI_ATTR_SHOW(fw_vendor);
  75. EFI_ATTR_SHOW(runtime);
  76. EFI_ATTR_SHOW(config_table);
  77. static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
  78. static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
  79. static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
  80. static struct attribute *efi_subsys_attrs[] = {
  81. &efi_attr_systab.attr,
  82. &efi_attr_fw_vendor.attr,
  83. &efi_attr_runtime.attr,
  84. &efi_attr_config_table.attr,
  85. NULL,
  86. };
  87. static umode_t efi_attr_is_visible(struct kobject *kobj,
  88. struct attribute *attr, int n)
  89. {
  90. umode_t mode = attr->mode;
  91. if (attr == &efi_attr_fw_vendor.attr)
  92. return (efi.fw_vendor == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
  93. else if (attr == &efi_attr_runtime.attr)
  94. return (efi.runtime == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
  95. else if (attr == &efi_attr_config_table.attr)
  96. return (efi.config_table == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
  97. return mode;
  98. }
  99. static struct attribute_group efi_subsys_attr_group = {
  100. .attrs = efi_subsys_attrs,
  101. .is_visible = efi_attr_is_visible,
  102. };
  103. static struct efivars generic_efivars;
  104. static struct efivar_operations generic_ops;
  105. static int generic_ops_register(void)
  106. {
  107. generic_ops.get_variable = efi.get_variable;
  108. generic_ops.set_variable = efi.set_variable;
  109. generic_ops.get_next_variable = efi.get_next_variable;
  110. generic_ops.query_variable_store = efi_query_variable_store;
  111. return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
  112. }
  113. static void generic_ops_unregister(void)
  114. {
  115. efivars_unregister(&generic_efivars);
  116. }
  117. /*
  118. * We register the efi subsystem with the firmware subsystem and the
  119. * efivars subsystem with the efi subsystem, if the system was booted with
  120. * EFI.
  121. */
  122. static int __init efisubsys_init(void)
  123. {
  124. int error;
  125. if (!efi_enabled(EFI_BOOT))
  126. return 0;
  127. /* We register the efi directory at /sys/firmware/efi */
  128. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  129. if (!efi_kobj) {
  130. pr_err("efi: Firmware registration failed.\n");
  131. return -ENOMEM;
  132. }
  133. error = generic_ops_register();
  134. if (error)
  135. goto err_put;
  136. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  137. if (error) {
  138. pr_err("efi: Sysfs attribute export failed with error %d.\n",
  139. error);
  140. goto err_unregister;
  141. }
  142. error = efi_runtime_map_init(efi_kobj);
  143. if (error)
  144. goto err_remove_group;
  145. /* and the standard mountpoint for efivarfs */
  146. efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
  147. if (!efivars_kobj) {
  148. pr_err("efivars: Subsystem registration failed.\n");
  149. error = -ENOMEM;
  150. goto err_remove_group;
  151. }
  152. return 0;
  153. err_remove_group:
  154. sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
  155. err_unregister:
  156. generic_ops_unregister();
  157. err_put:
  158. kobject_put(efi_kobj);
  159. return error;
  160. }
  161. subsys_initcall(efisubsys_init);
  162. /*
  163. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  164. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  165. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  166. */
  167. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  168. {
  169. struct efi_memory_map *map;
  170. void *p;
  171. map = efi.memmap;
  172. if (!map)
  173. return NULL;
  174. if (WARN_ON(!map->map))
  175. return NULL;
  176. for (p = map->map; p < map->map_end; p += map->desc_size) {
  177. efi_memory_desc_t *md = p;
  178. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  179. u64 end = md->phys_addr + size;
  180. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  181. md->type != EFI_BOOT_SERVICES_CODE &&
  182. md->type != EFI_BOOT_SERVICES_DATA)
  183. continue;
  184. if (!md->virt_addr)
  185. continue;
  186. if (phys_addr >= md->phys_addr && phys_addr < end) {
  187. phys_addr += md->virt_addr - md->phys_addr;
  188. return (__force void __iomem *)(unsigned long)phys_addr;
  189. }
  190. }
  191. return NULL;
  192. }
  193. static __initdata efi_config_table_type_t common_tables[] = {
  194. {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
  195. {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
  196. {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
  197. {MPS_TABLE_GUID, "MPS", &efi.mps},
  198. {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
  199. {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
  200. {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
  201. {NULL_GUID, NULL, 0},
  202. };
  203. static __init int match_config_table(efi_guid_t *guid,
  204. unsigned long table,
  205. efi_config_table_type_t *table_types)
  206. {
  207. u8 str[EFI_VARIABLE_GUID_LEN + 1];
  208. int i;
  209. if (table_types) {
  210. efi_guid_unparse(guid, str);
  211. for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
  212. efi_guid_unparse(&table_types[i].guid, str);
  213. if (!efi_guidcmp(*guid, table_types[i].guid)) {
  214. *(table_types[i].ptr) = table;
  215. pr_cont(" %s=0x%lx ",
  216. table_types[i].name, table);
  217. return 1;
  218. }
  219. }
  220. }
  221. return 0;
  222. }
  223. int __init efi_config_init(efi_config_table_type_t *arch_tables)
  224. {
  225. void *config_tables, *tablep;
  226. int i, sz;
  227. if (efi_enabled(EFI_64BIT))
  228. sz = sizeof(efi_config_table_64_t);
  229. else
  230. sz = sizeof(efi_config_table_32_t);
  231. /*
  232. * Let's see what config tables the firmware passed to us.
  233. */
  234. config_tables = early_memremap(efi.systab->tables,
  235. efi.systab->nr_tables * sz);
  236. if (config_tables == NULL) {
  237. pr_err("Could not map Configuration table!\n");
  238. return -ENOMEM;
  239. }
  240. tablep = config_tables;
  241. pr_info("");
  242. for (i = 0; i < efi.systab->nr_tables; i++) {
  243. efi_guid_t guid;
  244. unsigned long table;
  245. if (efi_enabled(EFI_64BIT)) {
  246. u64 table64;
  247. guid = ((efi_config_table_64_t *)tablep)->guid;
  248. table64 = ((efi_config_table_64_t *)tablep)->table;
  249. table = table64;
  250. #ifndef CONFIG_64BIT
  251. if (table64 >> 32) {
  252. pr_cont("\n");
  253. pr_err("Table located above 4GB, disabling EFI.\n");
  254. early_iounmap(config_tables,
  255. efi.systab->nr_tables * sz);
  256. return -EINVAL;
  257. }
  258. #endif
  259. } else {
  260. guid = ((efi_config_table_32_t *)tablep)->guid;
  261. table = ((efi_config_table_32_t *)tablep)->table;
  262. }
  263. if (!match_config_table(&guid, table, common_tables))
  264. match_config_table(&guid, table, arch_tables);
  265. tablep += sz;
  266. }
  267. pr_cont("\n");
  268. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  269. return 0;
  270. }