dmi-id.c 6.7 KB

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