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