dmi-sysfs.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * dmi-sysfs.c
  3. *
  4. * This module exports the DMI tables read-only to userspace through the
  5. * sysfs file system.
  6. *
  7. * Data is currently found below
  8. * /sys/firmware/dmi/...
  9. *
  10. * DMI attributes are presented in attribute files with names
  11. * formatted using %d-%d, so that the first integer indicates the
  12. * structure type (0-255), and the second field is the instance of that
  13. * entry.
  14. *
  15. * Copyright 2011 Google, Inc.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kobject.h>
  22. #include <linux/dmi.h>
  23. #include <linux/capability.h>
  24. #include <linux/slab.h>
  25. #include <linux/list.h>
  26. #include <linux/io.h>
  27. #define MAX_ENTRY_TYPE 255 /* Most of these aren't used, but we consider
  28. the top entry type is only 8 bits */
  29. struct dmi_sysfs_entry {
  30. struct dmi_header dh;
  31. struct kobject kobj;
  32. int instance;
  33. int position;
  34. struct list_head list;
  35. };
  36. /*
  37. * Global list of dmi_sysfs_entry. Even though this should only be
  38. * manipulated at setup and teardown, the lazy nature of the kobject
  39. * system means we get lazy removes.
  40. */
  41. static LIST_HEAD(entry_list);
  42. static DEFINE_SPINLOCK(entry_list_lock);
  43. /* dmi_sysfs_attribute - Top level attribute. used by all entries. */
  44. struct dmi_sysfs_attribute {
  45. struct attribute attr;
  46. ssize_t (*show)(struct dmi_sysfs_entry *entry, char *buf);
  47. };
  48. #define DMI_SYSFS_ATTR(_entry, _name) \
  49. struct dmi_sysfs_attribute dmi_sysfs_attr_##_entry##_##_name = { \
  50. .attr = {.name = __stringify(_name), .mode = 0400}, \
  51. .show = dmi_sysfs_##_entry##_##_name, \
  52. }
  53. /*
  54. * dmi_sysfs_mapped_attribute - Attribute where we require the entry be
  55. * mapped in. Use in conjunction with dmi_sysfs_specialize_attr_ops.
  56. */
  57. struct dmi_sysfs_mapped_attribute {
  58. struct attribute attr;
  59. ssize_t (*show)(struct dmi_sysfs_entry *entry,
  60. const struct dmi_header *dh,
  61. char *buf);
  62. };
  63. #define DMI_SYSFS_MAPPED_ATTR(_entry, _name) \
  64. struct dmi_sysfs_mapped_attribute dmi_sysfs_attr_##_entry##_##_name = { \
  65. .attr = {.name = __stringify(_name), .mode = 0400}, \
  66. .show = dmi_sysfs_##_entry##_##_name, \
  67. }
  68. /*************************************************
  69. * Generic DMI entry support.
  70. *************************************************/
  71. static struct dmi_sysfs_entry *to_entry(struct kobject *kobj)
  72. {
  73. return container_of(kobj, struct dmi_sysfs_entry, kobj);
  74. }
  75. static struct dmi_sysfs_attribute *to_attr(struct attribute *attr)
  76. {
  77. return container_of(attr, struct dmi_sysfs_attribute, attr);
  78. }
  79. static ssize_t dmi_sysfs_attr_show(struct kobject *kobj,
  80. struct attribute *_attr, char *buf)
  81. {
  82. struct dmi_sysfs_entry *entry = to_entry(kobj);
  83. struct dmi_sysfs_attribute *attr = to_attr(_attr);
  84. /* DMI stuff is only ever admin visible */
  85. if (!capable(CAP_SYS_ADMIN))
  86. return -EACCES;
  87. return attr->show(entry, buf);
  88. }
  89. static const struct sysfs_ops dmi_sysfs_attr_ops = {
  90. .show = dmi_sysfs_attr_show,
  91. };
  92. typedef ssize_t (*dmi_callback)(struct dmi_sysfs_entry *,
  93. const struct dmi_header *dh, void *);
  94. struct find_dmi_data {
  95. struct dmi_sysfs_entry *entry;
  96. dmi_callback callback;
  97. void *private;
  98. int instance_countdown;
  99. ssize_t ret;
  100. };
  101. static void find_dmi_entry_helper(const struct dmi_header *dh,
  102. void *_data)
  103. {
  104. struct find_dmi_data *data = _data;
  105. struct dmi_sysfs_entry *entry = data->entry;
  106. /* Is this the entry we want? */
  107. if (dh->type != entry->dh.type)
  108. return;
  109. if (data->instance_countdown != 0) {
  110. /* try the next instance? */
  111. data->instance_countdown--;
  112. return;
  113. }
  114. /*
  115. * Don't ever revisit the instance. Short circuit later
  116. * instances by letting the instance_countdown run negative
  117. */
  118. data->instance_countdown--;
  119. /* Found the entry */
  120. data->ret = data->callback(entry, dh, data->private);
  121. }
  122. /* State for passing the read parameters through dmi_find_entry() */
  123. struct dmi_read_state {
  124. char *buf;
  125. loff_t pos;
  126. size_t count;
  127. };
  128. static ssize_t find_dmi_entry(struct dmi_sysfs_entry *entry,
  129. dmi_callback callback, void *private)
  130. {
  131. struct find_dmi_data data = {
  132. .entry = entry,
  133. .callback = callback,
  134. .private = private,
  135. .instance_countdown = entry->instance,
  136. .ret = -EIO, /* To signal the entry disappeared */
  137. };
  138. int ret;
  139. ret = dmi_walk(find_dmi_entry_helper, &data);
  140. /* This shouldn't happen, but just in case. */
  141. if (ret)
  142. return -EINVAL;
  143. return data.ret;
  144. }
  145. /*
  146. * Calculate and return the byte length of the dmi entry identified by
  147. * dh. This includes both the formatted portion as well as the
  148. * unformatted string space, including the two trailing nul characters.
  149. */
  150. static size_t dmi_entry_length(const struct dmi_header *dh)
  151. {
  152. const char *p = (const char *)dh;
  153. p += dh->length;
  154. while (p[0] || p[1])
  155. p++;
  156. return 2 + p - (const char *)dh;
  157. }
  158. /*************************************************
  159. * Generic DMI entry support.
  160. *************************************************/
  161. static ssize_t dmi_sysfs_entry_length(struct dmi_sysfs_entry *entry, char *buf)
  162. {
  163. return sprintf(buf, "%d\n", entry->dh.length);
  164. }
  165. static ssize_t dmi_sysfs_entry_handle(struct dmi_sysfs_entry *entry, char *buf)
  166. {
  167. return sprintf(buf, "%d\n", entry->dh.handle);
  168. }
  169. static ssize_t dmi_sysfs_entry_type(struct dmi_sysfs_entry *entry, char *buf)
  170. {
  171. return sprintf(buf, "%d\n", entry->dh.type);
  172. }
  173. static ssize_t dmi_sysfs_entry_instance(struct dmi_sysfs_entry *entry,
  174. char *buf)
  175. {
  176. return sprintf(buf, "%d\n", entry->instance);
  177. }
  178. static ssize_t dmi_sysfs_entry_position(struct dmi_sysfs_entry *entry,
  179. char *buf)
  180. {
  181. return sprintf(buf, "%d\n", entry->position);
  182. }
  183. static DMI_SYSFS_ATTR(entry, length);
  184. static DMI_SYSFS_ATTR(entry, handle);
  185. static DMI_SYSFS_ATTR(entry, type);
  186. static DMI_SYSFS_ATTR(entry, instance);
  187. static DMI_SYSFS_ATTR(entry, position);
  188. static struct attribute *dmi_sysfs_entry_attrs[] = {
  189. &dmi_sysfs_attr_entry_length.attr,
  190. &dmi_sysfs_attr_entry_handle.attr,
  191. &dmi_sysfs_attr_entry_type.attr,
  192. &dmi_sysfs_attr_entry_instance.attr,
  193. &dmi_sysfs_attr_entry_position.attr,
  194. NULL,
  195. };
  196. static ssize_t dmi_entry_raw_read_helper(struct dmi_sysfs_entry *entry,
  197. const struct dmi_header *dh,
  198. void *_state)
  199. {
  200. struct dmi_read_state *state = _state;
  201. size_t entry_length;
  202. entry_length = dmi_entry_length(dh);
  203. return memory_read_from_buffer(state->buf, state->count,
  204. &state->pos, dh, entry_length);
  205. }
  206. static ssize_t dmi_entry_raw_read(struct file *filp,
  207. struct kobject *kobj,
  208. struct bin_attribute *bin_attr,
  209. char *buf, loff_t pos, size_t count)
  210. {
  211. struct dmi_sysfs_entry *entry = to_entry(kobj);
  212. struct dmi_read_state state = {
  213. .buf = buf,
  214. .pos = pos,
  215. .count = count,
  216. };
  217. return find_dmi_entry(entry, dmi_entry_raw_read_helper, &state);
  218. }
  219. static const struct bin_attribute dmi_entry_raw_attr = {
  220. .attr = {.name = "raw", .mode = 0400},
  221. .read = dmi_entry_raw_read,
  222. };
  223. static void dmi_sysfs_entry_release(struct kobject *kobj)
  224. {
  225. struct dmi_sysfs_entry *entry = to_entry(kobj);
  226. sysfs_remove_bin_file(&entry->kobj, &dmi_entry_raw_attr);
  227. spin_lock(&entry_list_lock);
  228. list_del(&entry->list);
  229. spin_unlock(&entry_list_lock);
  230. kfree(entry);
  231. }
  232. static struct kobj_type dmi_sysfs_entry_ktype = {
  233. .release = dmi_sysfs_entry_release,
  234. .sysfs_ops = &dmi_sysfs_attr_ops,
  235. .default_attrs = dmi_sysfs_entry_attrs,
  236. };
  237. static struct kobject *dmi_kobj;
  238. static struct kset *dmi_kset;
  239. /* Global count of all instances seen. Only for setup */
  240. static int __initdata instance_counts[MAX_ENTRY_TYPE + 1];
  241. /* Global positional count of all entries seen. Only for setup */
  242. static int __initdata position_count;
  243. static void __init dmi_sysfs_register_handle(const struct dmi_header *dh,
  244. void *_ret)
  245. {
  246. struct dmi_sysfs_entry *entry;
  247. int *ret = _ret;
  248. /* If a previous entry saw an error, short circuit */
  249. if (*ret)
  250. return;
  251. /* Allocate and register a new entry into the entries set */
  252. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  253. if (!entry) {
  254. *ret = -ENOMEM;
  255. return;
  256. }
  257. /* Set the key */
  258. entry->dh = *dh;
  259. entry->instance = instance_counts[dh->type]++;
  260. entry->position = position_count++;
  261. entry->kobj.kset = dmi_kset;
  262. *ret = kobject_init_and_add(&entry->kobj, &dmi_sysfs_entry_ktype, NULL,
  263. "%d-%d", dh->type, entry->instance);
  264. if (*ret) {
  265. kfree(entry);
  266. return;
  267. }
  268. /* Thread on the global list for cleanup */
  269. spin_lock(&entry_list_lock);
  270. list_add_tail(&entry->list, &entry_list);
  271. spin_unlock(&entry_list_lock);
  272. /* Create the raw binary file to access the entry */
  273. *ret = sysfs_create_bin_file(&entry->kobj, &dmi_entry_raw_attr);
  274. if (*ret)
  275. goto out_err;
  276. return;
  277. out_err:
  278. kobject_put(&entry->kobj);
  279. return;
  280. }
  281. static void cleanup_entry_list(void)
  282. {
  283. struct dmi_sysfs_entry *entry, *next;
  284. /* No locks, we are on our way out */
  285. list_for_each_entry_safe(entry, next, &entry_list, list) {
  286. kobject_put(&entry->kobj);
  287. }
  288. }
  289. static int __init dmi_sysfs_init(void)
  290. {
  291. int error = -ENOMEM;
  292. int val;
  293. /* Set up our directory */
  294. dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
  295. if (!dmi_kobj)
  296. goto err;
  297. dmi_kset = kset_create_and_add("entries", NULL, dmi_kobj);
  298. if (!dmi_kset)
  299. goto err;
  300. val = 0;
  301. error = dmi_walk(dmi_sysfs_register_handle, &val);
  302. if (error)
  303. goto err;
  304. if (val) {
  305. error = val;
  306. goto err;
  307. }
  308. pr_debug("dmi-sysfs: loaded.\n");
  309. return 0;
  310. err:
  311. cleanup_entry_list();
  312. kset_unregister(dmi_kset);
  313. kobject_put(dmi_kobj);
  314. return error;
  315. }
  316. /* clean up everything. */
  317. static void __exit dmi_sysfs_exit(void)
  318. {
  319. pr_debug("dmi-sysfs: unloading.\n");
  320. cleanup_entry_list();
  321. kset_unregister(dmi_kset);
  322. kobject_put(dmi_kobj);
  323. }
  324. module_init(dmi_sysfs_init);
  325. module_exit(dmi_sysfs_exit);
  326. MODULE_AUTHOR("Mike Waychison <mikew@google.com>");
  327. MODULE_DESCRIPTION("DMI sysfs support");
  328. MODULE_LICENSE("GPL");