efi.c 13 KB

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