runtime-map.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/drivers/efi/runtime-map.c
  4. * Copyright (C) 2013 Red Hat, Inc., Dave Young <[email protected]>
  5. */
  6. #include <linux/string.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/efi.h>
  11. #include <linux/slab.h>
  12. #include <asm/setup.h>
  13. struct efi_runtime_map_entry {
  14. efi_memory_desc_t md;
  15. struct kobject kobj; /* kobject for each entry */
  16. };
  17. static struct efi_runtime_map_entry **map_entries;
  18. struct map_attribute {
  19. struct attribute attr;
  20. ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
  21. };
  22. static inline struct map_attribute *to_map_attr(struct attribute *attr)
  23. {
  24. return container_of(attr, struct map_attribute, attr);
  25. }
  26. static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
  27. {
  28. return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
  29. }
  30. #define EFI_RUNTIME_FIELD(var) entry->md.var
  31. #define EFI_RUNTIME_U64_ATTR_SHOW(name) \
  32. static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
  33. { \
  34. return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
  35. }
  36. EFI_RUNTIME_U64_ATTR_SHOW(phys_addr);
  37. EFI_RUNTIME_U64_ATTR_SHOW(virt_addr);
  38. EFI_RUNTIME_U64_ATTR_SHOW(num_pages);
  39. EFI_RUNTIME_U64_ATTR_SHOW(attribute);
  40. static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
  41. {
  42. return container_of(kobj, struct efi_runtime_map_entry, kobj);
  43. }
  44. static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
  45. char *buf)
  46. {
  47. struct efi_runtime_map_entry *entry = to_map_entry(kobj);
  48. struct map_attribute *map_attr = to_map_attr(attr);
  49. return map_attr->show(entry, buf);
  50. }
  51. static struct map_attribute map_type_attr = __ATTR_RO_MODE(type, 0400);
  52. static struct map_attribute map_phys_addr_attr = __ATTR_RO_MODE(phys_addr, 0400);
  53. static struct map_attribute map_virt_addr_attr = __ATTR_RO_MODE(virt_addr, 0400);
  54. static struct map_attribute map_num_pages_attr = __ATTR_RO_MODE(num_pages, 0400);
  55. static struct map_attribute map_attribute_attr = __ATTR_RO_MODE(attribute, 0400);
  56. /*
  57. * These are default attributes that are added for every memmap entry.
  58. */
  59. static struct attribute *def_attrs[] = {
  60. &map_type_attr.attr,
  61. &map_phys_addr_attr.attr,
  62. &map_virt_addr_attr.attr,
  63. &map_num_pages_attr.attr,
  64. &map_attribute_attr.attr,
  65. NULL
  66. };
  67. ATTRIBUTE_GROUPS(def);
  68. static const struct sysfs_ops map_attr_ops = {
  69. .show = map_attr_show,
  70. };
  71. static void map_release(struct kobject *kobj)
  72. {
  73. struct efi_runtime_map_entry *entry;
  74. entry = to_map_entry(kobj);
  75. kfree(entry);
  76. }
  77. static struct kobj_type __refdata map_ktype = {
  78. .sysfs_ops = &map_attr_ops,
  79. .default_groups = def_groups,
  80. .release = map_release,
  81. };
  82. static struct kset *map_kset;
  83. static struct efi_runtime_map_entry *
  84. add_sysfs_runtime_map_entry(struct kobject *kobj, int nr,
  85. efi_memory_desc_t *md)
  86. {
  87. int ret;
  88. struct efi_runtime_map_entry *entry;
  89. if (!map_kset) {
  90. map_kset = kset_create_and_add("runtime-map", NULL, kobj);
  91. if (!map_kset)
  92. return ERR_PTR(-ENOMEM);
  93. }
  94. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  95. if (!entry) {
  96. kset_unregister(map_kset);
  97. map_kset = NULL;
  98. return ERR_PTR(-ENOMEM);
  99. }
  100. memcpy(&entry->md, md, sizeof(efi_memory_desc_t));
  101. kobject_init(&entry->kobj, &map_ktype);
  102. entry->kobj.kset = map_kset;
  103. ret = kobject_add(&entry->kobj, NULL, "%d", nr);
  104. if (ret) {
  105. kobject_put(&entry->kobj);
  106. kset_unregister(map_kset);
  107. map_kset = NULL;
  108. return ERR_PTR(ret);
  109. }
  110. return entry;
  111. }
  112. int efi_get_runtime_map_size(void)
  113. {
  114. return efi.memmap.nr_map * efi.memmap.desc_size;
  115. }
  116. int efi_get_runtime_map_desc_size(void)
  117. {
  118. return efi.memmap.desc_size;
  119. }
  120. int efi_runtime_map_copy(void *buf, size_t bufsz)
  121. {
  122. size_t sz = efi_get_runtime_map_size();
  123. if (sz > bufsz)
  124. sz = bufsz;
  125. memcpy(buf, efi.memmap.map, sz);
  126. return 0;
  127. }
  128. int __init efi_runtime_map_init(struct kobject *efi_kobj)
  129. {
  130. int i, j, ret = 0;
  131. struct efi_runtime_map_entry *entry;
  132. efi_memory_desc_t *md;
  133. if (!efi_enabled(EFI_MEMMAP))
  134. return 0;
  135. map_entries = kcalloc(efi.memmap.nr_map, sizeof(entry), GFP_KERNEL);
  136. if (!map_entries) {
  137. ret = -ENOMEM;
  138. goto out;
  139. }
  140. i = 0;
  141. for_each_efi_memory_desc(md) {
  142. entry = add_sysfs_runtime_map_entry(efi_kobj, i, md);
  143. if (IS_ERR(entry)) {
  144. ret = PTR_ERR(entry);
  145. goto out_add_entry;
  146. }
  147. *(map_entries + i++) = entry;
  148. }
  149. return 0;
  150. out_add_entry:
  151. for (j = i - 1; j >= 0; j--) {
  152. entry = *(map_entries + j);
  153. kobject_put(&entry->kobj);
  154. }
  155. out:
  156. return ret;
  157. }