efi.c 7.1 KB

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