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