blob: c4a73855e38c9b7acbb77069510b11abdea14402 [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>
7
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009static char * __init dmi_string(struct dmi_header *dm, u8 s)
10{
Andrey Panin1249c512005-06-25 14:54:47 -070011 u8 *bp = ((u8 *) dm) + dm->length;
Andrey Paninc3c71202005-09-06 15:18:28 -070012 char *str = "";
Andrey Panin1249c512005-06-25 14:54:47 -070013
Andrey Paninc3c71202005-09-06 15:18:28 -070014 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 s--;
Andrey Paninc3c71202005-09-06 15:18:28 -070016 while (s > 0 && *bp) {
17 bp += strlen(bp) + 1;
18 s--;
19 }
20
21 if (*bp != 0) {
22 str = alloc_bootmem(strlen(bp) + 1);
23 if (str != NULL)
24 strcpy(str, bp);
25 else
26 printk(KERN_ERR "dmi_string: out of memory.\n");
27 }
28 }
29
30 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
33/*
34 * We have to be cautious here. We have seen BIOSes with DMI pointers
35 * pointing to completely the wrong place for example
36 */
Andrey Panin1249c512005-06-25 14:54:47 -070037static int __init dmi_table(u32 base, int len, int num,
38 void (*decode)(struct dmi_header *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Andrey Panin1249c512005-06-25 14:54:47 -070040 u8 *buf, *data;
41 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 buf = bt_ioremap(base, len);
Andrey Panin1249c512005-06-25 14:54:47 -070044 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return -1;
46
47 data = buf;
48
49 /*
50 * Stop when we see all the items the table claimed to have
51 * OR we run off the end of the table (also happens)
52 */
Andrey Panin1249c512005-06-25 14:54:47 -070053 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
54 struct dmi_header *dm = (struct dmi_header *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /*
56 * We want to know the total length (formated area and strings)
57 * before decoding to make sure we won't run off the table in
58 * dmi_decode or dmi_string
59 */
Andrey Panin1249c512005-06-25 14:54:47 -070060 data += dm->length;
61 while ((data - buf < len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 data++;
Andrey Panin1249c512005-06-25 14:54:47 -070063 if (data - buf < len - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 decode(dm);
Andrey Panin1249c512005-06-25 14:54:47 -070065 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 i++;
67 }
68 bt_iounmap(buf, len);
69 return 0;
70}
71
Andrey Panin1249c512005-06-25 14:54:47 -070072static int __init dmi_checksum(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Andrey Panin1249c512005-06-25 14:54:47 -070074 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int a;
76
Andrey Panin1249c512005-06-25 14:54:47 -070077 for (a = 0; a < 15; a++)
78 sum += buf[a];
79
80 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -070084static LIST_HEAD(dmi_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Save a DMI string
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
90{
Andrey Paninc3c71202005-09-06 15:18:28 -070091 char *p, *d = (char*) dm;
Andrey Panin1249c512005-06-25 14:54:47 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (dmi_ident[slot])
94 return;
Andrey Panin1249c512005-06-25 14:54:47 -070095
Andrey Paninc3c71202005-09-06 15:18:28 -070096 p = dmi_string(dm, d[string]);
97 if (p == NULL)
98 return;
99
100 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Andrey Paninebad6a42005-09-06 15:18:29 -0700103static void __init dmi_save_devices(struct dmi_header *dm)
104{
105 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
106 struct dmi_device *dev;
107
108 for (i = 0; i < count; i++) {
109 char *d = ((char *) dm) + (i * 2);
110
111 /* Skip disabled device */
112 if ((*d & 0x80) == 0)
113 continue;
114
115 dev = alloc_bootmem(sizeof(*dev));
116 if (!dev) {
117 printk(KERN_ERR "dmi_save_devices: out of memory.\n");
118 break;
119 }
120
121 dev->type = *d++ & 0x7f;
122 dev->name = dmi_string(dm, *d);
123 dev->device_data = NULL;
124
125 list_add(&dev->list, &dmi_devices);
126 }
127}
128
129static void __init dmi_save_ipmi_device(struct dmi_header *dm)
130{
131 struct dmi_device *dev;
132 void * data;
133
134 data = alloc_bootmem(dm->length);
135 if (data == NULL) {
136 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
137 return;
138 }
139
140 memcpy(data, dm, dm->length);
141
142 dev = alloc_bootmem(sizeof(*dev));
143 if (!dev) {
144 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
145 return;
146 }
147
148 dev->type = DMI_DEV_TYPE_IPMI;
149 dev->name = "IPMI controller";
150 dev->device_data = data;
151
152 list_add(&dev->list, &dmi_devices);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Process a DMI table entry. Right now all we care about are the BIOS
157 * and machine entries. For 2.5 we should pull the smbus controller info
158 * out of here.
159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void __init dmi_decode(struct dmi_header *dm)
161{
Andrey Panin1249c512005-06-25 14:54:47 -0700162 switch(dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700163 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700164 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700165 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700166 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
167 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700168 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700169 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700170 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700171 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700172 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
173 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700174 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700175 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700176 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700177 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
178 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700179 case 10: /* Onboard Devices Information */
180 dmi_save_devices(dm);
181 break;
182 case 38: /* IPMI Device Information */
183 dmi_save_ipmi_device(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185}
186
187void __init dmi_scan_machine(void)
188{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700189 u8 buf[15];
190 char __iomem *p, *q;
191
192 /*
193 * no iounmap() for that ioremap(); it would be a no-op, but it's
194 * so early in setup that sucker gets confused into doing what
195 * it shouldn't if we actually call it.
196 */
197 p = ioremap(0xF0000, 0x10000);
198 if (p == NULL)
199 goto out;
200
201 for (q = p; q < p + 0x10000; q += 16) {
202 memcpy_fromio(buf, q, 15);
203 if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
204 u16 num = (buf[13] << 8) | buf[12];
205 u16 len = (buf[7] << 8) | buf[6];
206 u32 base = (buf[11] << 24) | (buf[10] << 16) |
207 (buf[9] << 8) | buf[8];
208
209 /*
210 * DMI version 0.0 means that the real version is taken from
211 * the SMBIOS version, which we don't know at this point.
212 */
213 if (buf[14] != 0)
214 printk(KERN_INFO "DMI %d.%d present.\n",
215 buf[14] >> 4, buf[14] & 0xF);
216 else
217 printk(KERN_INFO "DMI present.\n");
218
Andrey Panin61e032fa2005-09-06 15:18:26 -0700219 if (dmi_table(base,len, num, dmi_decode) == 0)
220 return;
221 }
222 }
223
224out: printk(KERN_INFO "DMI not present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227
228/**
229 * dmi_check_system - check system DMI data
230 * @list: array of dmi_system_id structures to match against
231 *
232 * Walk the blacklist table running matching functions until someone
233 * returns non zero or we hit the end. Callback function is called for
234 * each successfull match. Returns the number of matches.
235 */
236int dmi_check_system(struct dmi_system_id *list)
237{
238 int i, count = 0;
239 struct dmi_system_id *d = list;
240
241 while (d->ident) {
242 for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
243 int s = d->matches[i].slot;
244 if (s == DMI_NONE)
245 continue;
246 if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
247 continue;
248 /* No match */
249 goto fail;
250 }
251 if (d->callback && d->callback(d))
252 break;
253 count++;
254fail: d++;
255 }
256
257 return count;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259EXPORT_SYMBOL(dmi_check_system);
260
261/**
262 * dmi_get_system_info - return DMI data value
263 * @field: data index (see enum dmi_filed)
264 *
265 * Returns one DMI data value, can be used to perform
266 * complex DMI data checks.
267 */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700268char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 return dmi_ident[field];
271}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700272EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700273
274/**
275 * dmi_find_device - find onboard device by type/name
276 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
277 * @desc: device name string or %NULL to match all
278 * @from: previous device found in search, or %NULL for new search.
279 *
280 * Iterates through the list of known onboard devices. If a device is
281 * found with a matching @vendor and @device, a pointer to its device
282 * structure is returned. Otherwise, %NULL is returned.
283 * A new search is initiated by passing %NULL to the @from argument.
284 * If @from is not %NULL, searches continue from next device.
285 */
286struct dmi_device * dmi_find_device(int type, const char *name,
287 struct dmi_device *from)
288{
289 struct list_head *d, *head = from ? &from->list : &dmi_devices;
290
291 for(d = head->next; d != &dmi_devices; d = d->next) {
292 struct dmi_device *dev = list_entry(d, struct dmi_device, list);
293
294 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
295 ((name == NULL) || (strcmp(dev->name, name) == 0)))
296 return dev;
297 }
298
299 return NULL;
300}
301EXPORT_SYMBOL(dmi_find_device);