blob: ebc8dc116c430425dd0051734d5db552f8bde4df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/string.h>
3#include <linux/init.h>
4#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/dmi.h>
6#include <linux/bootmem.h>
Andi Kleene9928672006-01-11 22:43:33 +01007#include <linux/slab.h>
Andi Kleenf2d3efe2006-03-25 16:30:22 +01008#include <asm/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010static char * __init dmi_string(struct dmi_header *dm, u8 s)
11{
Andrey Panin1249c512005-06-25 14:54:47 -070012 u8 *bp = ((u8 *) dm) + dm->length;
Andrey Paninc3c71202005-09-06 15:18:28 -070013 char *str = "";
Andrey Panin1249c512005-06-25 14:54:47 -070014
Andrey Paninc3c71202005-09-06 15:18:28 -070015 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 s--;
Andrey Paninc3c71202005-09-06 15:18:28 -070017 while (s > 0 && *bp) {
18 bp += strlen(bp) + 1;
19 s--;
20 }
21
22 if (*bp != 0) {
Andi Kleene9928672006-01-11 22:43:33 +010023 str = dmi_alloc(strlen(bp) + 1);
Andrey Paninc3c71202005-09-06 15:18:28 -070024 if (str != NULL)
25 strcpy(str, bp);
26 else
27 printk(KERN_ERR "dmi_string: out of memory.\n");
28 }
29 }
30
31 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
34/*
35 * We have to be cautious here. We have seen BIOSes with DMI pointers
36 * pointing to completely the wrong place for example
37 */
Andrey Panin1249c512005-06-25 14:54:47 -070038static int __init dmi_table(u32 base, int len, int num,
39 void (*decode)(struct dmi_header *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Andrey Panin1249c512005-06-25 14:54:47 -070041 u8 *buf, *data;
42 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andi Kleene9928672006-01-11 22:43:33 +010044 buf = dmi_ioremap(base, len);
Andrey Panin1249c512005-06-25 14:54:47 -070045 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return -1;
47
48 data = buf;
49
50 /*
51 * Stop when we see all the items the table claimed to have
52 * OR we run off the end of the table (also happens)
53 */
Andrey Panin1249c512005-06-25 14:54:47 -070054 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
55 struct dmi_header *dm = (struct dmi_header *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /*
57 * We want to know the total length (formated area and strings)
58 * before decoding to make sure we won't run off the table in
59 * dmi_decode or dmi_string
60 */
Andrey Panin1249c512005-06-25 14:54:47 -070061 data += dm->length;
62 while ((data - buf < len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 data++;
Andrey Panin1249c512005-06-25 14:54:47 -070064 if (data - buf < len - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 decode(dm);
Andrey Panin1249c512005-06-25 14:54:47 -070066 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 i++;
68 }
Andi Kleene9928672006-01-11 22:43:33 +010069 dmi_iounmap(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return 0;
71}
72
Andrey Panin1249c512005-06-25 14:54:47 -070073static int __init dmi_checksum(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Andrey Panin1249c512005-06-25 14:54:47 -070075 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int a;
77
Andrey Panin1249c512005-06-25 14:54:47 -070078 for (a = 0; a < 15; a++)
79 sum += buf[a];
80
81 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -070085static LIST_HEAD(dmi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Save a DMI string
89 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
91{
Andrey Paninc3c71202005-09-06 15:18:28 -070092 char *p, *d = (char*) dm;
Andrey Panin1249c512005-06-25 14:54:47 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (dmi_ident[slot])
95 return;
Andrey Panin1249c512005-06-25 14:54:47 -070096
Andrey Paninc3c71202005-09-06 15:18:28 -070097 p = dmi_string(dm, d[string]);
98 if (p == NULL)
99 return;
100
101 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Andrey Paninebad6a42005-09-06 15:18:29 -0700104static void __init dmi_save_devices(struct dmi_header *dm)
105{
106 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
107 struct dmi_device *dev;
108
109 for (i = 0; i < count; i++) {
Andrey Paninbc834552006-03-25 03:06:31 -0800110 char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700111
112 /* Skip disabled device */
113 if ((*d & 0x80) == 0)
114 continue;
115
Andi Kleene9928672006-01-11 22:43:33 +0100116 dev = dmi_alloc(sizeof(*dev));
Andrey Paninebad6a42005-09-06 15:18:29 -0700117 if (!dev) {
118 printk(KERN_ERR "dmi_save_devices: out of memory.\n");
119 break;
120 }
121
122 dev->type = *d++ & 0x7f;
123 dev->name = dmi_string(dm, *d);
124 dev->device_data = NULL;
125
126 list_add(&dev->list, &dmi_devices);
127 }
128}
129
130static void __init dmi_save_ipmi_device(struct dmi_header *dm)
131{
132 struct dmi_device *dev;
133 void * data;
134
Andi Kleene9928672006-01-11 22:43:33 +0100135 data = dmi_alloc(dm->length);
Andrey Paninebad6a42005-09-06 15:18:29 -0700136 if (data == NULL) {
137 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
138 return;
139 }
140
141 memcpy(data, dm, dm->length);
142
Andi Kleene9928672006-01-11 22:43:33 +0100143 dev = dmi_alloc(sizeof(*dev));
Andrey Paninebad6a42005-09-06 15:18:29 -0700144 if (!dev) {
145 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
146 return;
147 }
148
149 dev->type = DMI_DEV_TYPE_IPMI;
150 dev->name = "IPMI controller";
151 dev->device_data = data;
152
153 list_add(&dev->list, &dmi_devices);
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * Process a DMI table entry. Right now all we care about are the BIOS
158 * and machine entries. For 2.5 we should pull the smbus controller info
159 * out of here.
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static void __init dmi_decode(struct dmi_header *dm)
162{
Andrey Panin1249c512005-06-25 14:54:47 -0700163 switch(dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700164 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700165 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700166 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700167 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
168 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700169 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700170 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700171 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700172 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700173 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
174 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700175 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700176 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700177 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700178 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
179 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700180 case 10: /* Onboard Devices Information */
181 dmi_save_devices(dm);
182 break;
183 case 38: /* IPMI Device Information */
184 dmi_save_ipmi_device(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186}
187
188void __init dmi_scan_machine(void)
189{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700190 u8 buf[15];
191 char __iomem *p, *q;
192
193 /*
194 * no iounmap() for that ioremap(); it would be a no-op, but it's
195 * so early in setup that sucker gets confused into doing what
196 * it shouldn't if we actually call it.
197 */
198 p = ioremap(0xF0000, 0x10000);
199 if (p == NULL)
200 goto out;
201
202 for (q = p; q < p + 0x10000; q += 16) {
203 memcpy_fromio(buf, q, 15);
204 if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
205 u16 num = (buf[13] << 8) | buf[12];
206 u16 len = (buf[7] << 8) | buf[6];
207 u32 base = (buf[11] << 24) | (buf[10] << 16) |
208 (buf[9] << 8) | buf[8];
209
210 /*
211 * DMI version 0.0 means that the real version is taken from
212 * the SMBIOS version, which we don't know at this point.
213 */
214 if (buf[14] != 0)
215 printk(KERN_INFO "DMI %d.%d present.\n",
216 buf[14] >> 4, buf[14] & 0xF);
217 else
218 printk(KERN_INFO "DMI present.\n");
219
Andrey Panin61e032fa2005-09-06 15:18:26 -0700220 if (dmi_table(base,len, num, dmi_decode) == 0)
221 return;
222 }
223 }
224
Andi Kleene9928672006-01-11 22:43:33 +0100225out: printk(KERN_INFO "DMI not present or invalid.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228
229/**
230 * dmi_check_system - check system DMI data
231 * @list: array of dmi_system_id structures to match against
232 *
233 * Walk the blacklist table running matching functions until someone
234 * returns non zero or we hit the end. Callback function is called for
235 * each successfull match. Returns the number of matches.
236 */
237int dmi_check_system(struct dmi_system_id *list)
238{
239 int i, count = 0;
240 struct dmi_system_id *d = list;
241
242 while (d->ident) {
243 for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
244 int s = d->matches[i].slot;
245 if (s == DMI_NONE)
246 continue;
247 if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
248 continue;
249 /* No match */
250 goto fail;
251 }
Robert Love640e8032005-09-06 15:18:30 -0700252 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (d->callback && d->callback(d))
254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255fail: d++;
256 }
257
258 return count;
259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260EXPORT_SYMBOL(dmi_check_system);
261
262/**
263 * dmi_get_system_info - return DMI data value
264 * @field: data index (see enum dmi_filed)
265 *
266 * Returns one DMI data value, can be used to perform
267 * complex DMI data checks.
268 */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700269char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 return dmi_ident[field];
272}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700273EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700274
275/**
276 * dmi_find_device - find onboard device by type/name
277 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
278 * @desc: device name string or %NULL to match all
279 * @from: previous device found in search, or %NULL for new search.
280 *
281 * Iterates through the list of known onboard devices. If a device is
282 * found with a matching @vendor and @device, a pointer to its device
283 * structure is returned. Otherwise, %NULL is returned.
284 * A new search is initiated by passing %NULL to the @from argument.
285 * If @from is not %NULL, searches continue from next device.
286 */
287struct dmi_device * dmi_find_device(int type, const char *name,
288 struct dmi_device *from)
289{
290 struct list_head *d, *head = from ? &from->list : &dmi_devices;
291
292 for(d = head->next; d != &dmi_devices; d = d->next) {
293 struct dmi_device *dev = list_entry(d, struct dmi_device, list);
294
295 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
296 ((name == NULL) || (strcmp(dev->name, name) == 0)))
297 return dev;
298 }
299
300 return NULL;
301}
302EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100303
304/**
305 * dmi_get_year - Return year of a DMI date
306 * @field: data index (like dmi_get_system_info)
307 *
308 * Returns -1 when the field doesn't exist. 0 when it is broken.
309 */
310int dmi_get_year(int field)
311{
312 int year;
313 char *s = dmi_get_system_info(field);
314
315 if (!s)
316 return -1;
317 if (*s == '\0')
318 return 0;
319 s = strrchr(s, '/');
320 if (!s)
321 return 0;
322
323 s += 1;
324 year = simple_strtoul(s, NULL, 0);
325 if (year && year < 100) { /* 2-digit year */
326 year += 1900;
327 if (year < 1996) /* no dates < spec 1.0 */
328 year += 100;
329 }
330
331 return year;
332}