dmi-id.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Export SMBIOS/DMI info via sysfs to userspace
  3. *
  4. * Copyright 2007, Lennart Poettering
  5. *
  6. * Licensed under GPLv2
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/dmi.h>
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. struct dmi_device_attribute{
  15. struct device_attribute dev_attr;
  16. int field;
  17. };
  18. #define to_dmi_dev_attr(_dev_attr) \
  19. container_of(_dev_attr, struct dmi_device_attribute, dev_attr)
  20. static ssize_t sys_dmi_field_show(struct device *dev,
  21. struct device_attribute *attr,
  22. char *page)
  23. {
  24. int field = to_dmi_dev_attr(attr)->field;
  25. ssize_t len;
  26. len = scnprintf(page, PAGE_SIZE, "%s\n", dmi_get_system_info(field));
  27. page[len-1] = '\n';
  28. return len;
  29. }
  30. #define DMI_ATTR(_name, _mode, _show, _field) \
  31. { .dev_attr = __ATTR(_name, _mode, _show, NULL), \
  32. .field = _field }
  33. #define DEFINE_DMI_ATTR_WITH_SHOW(_name, _mode, _field) \
  34. static struct dmi_device_attribute sys_dmi_##_name##_attr = \
  35. DMI_ATTR(_name, _mode, sys_dmi_field_show, _field);
  36. DEFINE_DMI_ATTR_WITH_SHOW(bios_vendor, 0444, DMI_BIOS_VENDOR);
  37. DEFINE_DMI_ATTR_WITH_SHOW(bios_version, 0444, DMI_BIOS_VERSION);
  38. DEFINE_DMI_ATTR_WITH_SHOW(bios_date, 0444, DMI_BIOS_DATE);
  39. DEFINE_DMI_ATTR_WITH_SHOW(sys_vendor, 0444, DMI_SYS_VENDOR);
  40. DEFINE_DMI_ATTR_WITH_SHOW(product_name, 0444, DMI_PRODUCT_NAME);
  41. DEFINE_DMI_ATTR_WITH_SHOW(product_version, 0444, DMI_PRODUCT_VERSION);
  42. DEFINE_DMI_ATTR_WITH_SHOW(product_serial, 0400, DMI_PRODUCT_SERIAL);
  43. DEFINE_DMI_ATTR_WITH_SHOW(product_uuid, 0400, DMI_PRODUCT_UUID);
  44. DEFINE_DMI_ATTR_WITH_SHOW(board_vendor, 0444, DMI_BOARD_VENDOR);
  45. DEFINE_DMI_ATTR_WITH_SHOW(board_name, 0444, DMI_BOARD_NAME);
  46. DEFINE_DMI_ATTR_WITH_SHOW(board_version, 0444, DMI_BOARD_VERSION);
  47. DEFINE_DMI_ATTR_WITH_SHOW(board_serial, 0400, DMI_BOARD_SERIAL);
  48. DEFINE_DMI_ATTR_WITH_SHOW(board_asset_tag, 0444, DMI_BOARD_ASSET_TAG);
  49. DEFINE_DMI_ATTR_WITH_SHOW(chassis_vendor, 0444, DMI_CHASSIS_VENDOR);
  50. DEFINE_DMI_ATTR_WITH_SHOW(chassis_type, 0444, DMI_CHASSIS_TYPE);
  51. DEFINE_DMI_ATTR_WITH_SHOW(chassis_version, 0444, DMI_CHASSIS_VERSION);
  52. DEFINE_DMI_ATTR_WITH_SHOW(chassis_serial, 0400, DMI_CHASSIS_SERIAL);
  53. DEFINE_DMI_ATTR_WITH_SHOW(chassis_asset_tag, 0444, DMI_CHASSIS_ASSET_TAG);
  54. static void ascii_filter(char *d, const char *s)
  55. {
  56. /* Filter out characters we don't want to see in the modalias string */
  57. for (; *s; s++)
  58. if (*s > ' ' && *s < 127 && *s != ':')
  59. *(d++) = *s;
  60. *d = 0;
  61. }
  62. static ssize_t get_modalias(char *buffer, size_t buffer_size)
  63. {
  64. static const struct mafield {
  65. const char *prefix;
  66. int field;
  67. } fields[] = {
  68. { "bvn", DMI_BIOS_VENDOR },
  69. { "bvr", DMI_BIOS_VERSION },
  70. { "bd", DMI_BIOS_DATE },
  71. { "svn", DMI_SYS_VENDOR },
  72. { "pn", DMI_PRODUCT_NAME },
  73. { "pvr", DMI_PRODUCT_VERSION },
  74. { "rvn", DMI_BOARD_VENDOR },
  75. { "rn", DMI_BOARD_NAME },
  76. { "rvr", DMI_BOARD_VERSION },
  77. { "cvn", DMI_CHASSIS_VENDOR },
  78. { "ct", DMI_CHASSIS_TYPE },
  79. { "cvr", DMI_CHASSIS_VERSION },
  80. { NULL, DMI_NONE }
  81. };
  82. ssize_t l, left;
  83. char *p;
  84. const struct mafield *f;
  85. strcpy(buffer, "dmi");
  86. p = buffer + 3; left = buffer_size - 4;
  87. for (f = fields; f->prefix && left > 0; f++) {
  88. const char *c;
  89. char *t;
  90. c = dmi_get_system_info(f->field);
  91. if (!c)
  92. continue;
  93. t = kmalloc(strlen(c) + 1, GFP_KERNEL);
  94. if (!t)
  95. break;
  96. ascii_filter(t, c);
  97. l = scnprintf(p, left, ":%s%s", f->prefix, t);
  98. kfree(t);
  99. p += l;
  100. left -= l;
  101. }
  102. p[0] = ':';
  103. p[1] = 0;
  104. return p - buffer + 1;
  105. }
  106. static ssize_t sys_dmi_modalias_show(struct device *dev,
  107. struct device_attribute *attr, char *page)
  108. {
  109. ssize_t r;
  110. r = get_modalias(page, PAGE_SIZE-1);
  111. page[r] = '\n';
  112. page[r+1] = 0;
  113. return r+1;
  114. }
  115. static struct device_attribute sys_dmi_modalias_attr =
  116. __ATTR(modalias, 0444, sys_dmi_modalias_show, NULL);
  117. static struct attribute *sys_dmi_attributes[DMI_STRING_MAX+2];
  118. static struct attribute_group sys_dmi_attribute_group = {
  119. .attrs = sys_dmi_attributes,
  120. };
  121. static const struct attribute_group* sys_dmi_attribute_groups[] = {
  122. &sys_dmi_attribute_group,
  123. NULL
  124. };
  125. static int dmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  126. {
  127. ssize_t len;
  128. if (add_uevent_var(env, "MODALIAS="))
  129. return -ENOMEM;
  130. len = get_modalias(&env->buf[env->buflen - 1],
  131. sizeof(env->buf) - env->buflen);
  132. if (len >= (sizeof(env->buf) - env->buflen))
  133. return -ENOMEM;
  134. env->buflen += len;
  135. return 0;
  136. }
  137. static struct class dmi_class = {
  138. .name = "dmi",
  139. .dev_release = (void(*)(struct device *)) kfree,
  140. .dev_uevent = dmi_dev_uevent,
  141. };
  142. static struct device *dmi_dev;
  143. /* Initialization */
  144. #define ADD_DMI_ATTR(_name, _field) \
  145. if (dmi_get_system_info(_field)) \
  146. sys_dmi_attributes[i++] = &sys_dmi_##_name##_attr.dev_attr.attr;
  147. /* In a separate function to keep gcc 3.2 happy - do NOT merge this in
  148. dmi_id_init! */
  149. static void __init dmi_id_init_attr_table(void)
  150. {
  151. int i;
  152. /* Not necessarily all DMI fields are available on all
  153. * systems, hence let's built an attribute table of just
  154. * what's available */
  155. i = 0;
  156. ADD_DMI_ATTR(bios_vendor, DMI_BIOS_VENDOR);
  157. ADD_DMI_ATTR(bios_version, DMI_BIOS_VERSION);
  158. ADD_DMI_ATTR(bios_date, DMI_BIOS_DATE);
  159. ADD_DMI_ATTR(sys_vendor, DMI_SYS_VENDOR);
  160. ADD_DMI_ATTR(product_name, DMI_PRODUCT_NAME);
  161. ADD_DMI_ATTR(product_version, DMI_PRODUCT_VERSION);
  162. ADD_DMI_ATTR(product_serial, DMI_PRODUCT_SERIAL);
  163. ADD_DMI_ATTR(product_uuid, DMI_PRODUCT_UUID);
  164. ADD_DMI_ATTR(board_vendor, DMI_BOARD_VENDOR);
  165. ADD_DMI_ATTR(board_name, DMI_BOARD_NAME);
  166. ADD_DMI_ATTR(board_version, DMI_BOARD_VERSION);
  167. ADD_DMI_ATTR(board_serial, DMI_BOARD_SERIAL);
  168. ADD_DMI_ATTR(board_asset_tag, DMI_BOARD_ASSET_TAG);
  169. ADD_DMI_ATTR(chassis_vendor, DMI_CHASSIS_VENDOR);
  170. ADD_DMI_ATTR(chassis_type, DMI_CHASSIS_TYPE);
  171. ADD_DMI_ATTR(chassis_version, DMI_CHASSIS_VERSION);
  172. ADD_DMI_ATTR(chassis_serial, DMI_CHASSIS_SERIAL);
  173. ADD_DMI_ATTR(chassis_asset_tag, DMI_CHASSIS_ASSET_TAG);
  174. sys_dmi_attributes[i++] = &sys_dmi_modalias_attr.attr;
  175. }
  176. static int __init dmi_id_init(void)
  177. {
  178. int ret;
  179. if (!dmi_available)
  180. return -ENODEV;
  181. dmi_id_init_attr_table();
  182. ret = class_register(&dmi_class);
  183. if (ret)
  184. return ret;
  185. dmi_dev = kzalloc(sizeof(*dmi_dev), GFP_KERNEL);
  186. if (!dmi_dev) {
  187. ret = -ENOMEM;
  188. goto fail_class_unregister;
  189. }
  190. dmi_dev->class = &dmi_class;
  191. dev_set_name(dmi_dev, "id");
  192. dmi_dev->groups = sys_dmi_attribute_groups;
  193. ret = device_register(dmi_dev);
  194. if (ret)
  195. goto fail_free_dmi_dev;
  196. return 0;
  197. fail_free_dmi_dev:
  198. kfree(dmi_dev);
  199. fail_class_unregister:
  200. class_unregister(&dmi_class);
  201. return ret;
  202. }
  203. arch_initcall(dmi_id_init);