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