efi.c 15 KB

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