ebc8dc116c430425dd0051734d5db552f8bde4df
[safe/jmp/linux-2.6] / arch / i386 / kernel / dmi_scan.c
1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/dmi.h>
6 #include <linux/bootmem.h>
7 #include <linux/slab.h>
8 #include <asm/dmi.h>
9
10 static char * __init dmi_string(struct dmi_header *dm, u8 s)
11 {
12         u8 *bp = ((u8 *) dm) + dm->length;
13         char *str = "";
14
15         if (s) {
16                 s--;
17                 while (s > 0 && *bp) {
18                         bp += strlen(bp) + 1;
19                         s--;
20                 }
21
22                 if (*bp != 0) {
23                         str = dmi_alloc(strlen(bp) + 1);
24                         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;
32 }
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  */
38 static int __init dmi_table(u32 base, int len, int num,
39                             void (*decode)(struct dmi_header *))
40 {
41         u8 *buf, *data;
42         int i = 0;
43                 
44         buf = dmi_ioremap(base, len);
45         if (buf == NULL)
46                 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          */
54         while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
55                 struct dmi_header *dm = (struct dmi_header *)data;
56                 /*
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                  */
61                 data += dm->length;
62                 while ((data - buf < len - 1) && (data[0] || data[1]))
63                         data++;
64                 if (data - buf < len - 1)
65                         decode(dm);
66                 data += 2;
67                 i++;
68         }
69         dmi_iounmap(buf, len);
70         return 0;
71 }
72
73 static int __init dmi_checksum(u8 *buf)
74 {
75         u8 sum = 0;
76         int a;
77         
78         for (a = 0; a < 15; a++)
79                 sum += buf[a];
80
81         return sum == 0;
82 }
83
84 static char *dmi_ident[DMI_STRING_MAX];
85 static LIST_HEAD(dmi_devices);
86
87 /*
88  *      Save a DMI string
89  */
90 static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
91 {
92         char *p, *d = (char*) dm;
93
94         if (dmi_ident[slot])
95                 return;
96
97         p = dmi_string(dm, d[string]);
98         if (p == NULL)
99                 return;
100
101         dmi_ident[slot] = p;
102 }
103
104 static 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++) {
110                 char *d = (char *)(dm + 1) + (i * 2);
111
112                 /* Skip disabled device */
113                 if ((*d & 0x80) == 0)
114                         continue;
115
116                 dev = dmi_alloc(sizeof(*dev));
117                 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
130 static void __init dmi_save_ipmi_device(struct dmi_header *dm)
131 {
132         struct dmi_device *dev;
133         void * data;
134
135         data = dmi_alloc(dm->length);
136         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
143         dev = dmi_alloc(sizeof(*dev));
144         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
156 /*
157  *      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  */
161 static void __init dmi_decode(struct dmi_header *dm)
162 {
163         switch(dm->type) {
164         case 0:         /* BIOS Information */
165                 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
166                 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
167                 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
168                 break;
169         case 1:         /* System Information */
170                 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
171                 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
172                 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
173                 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
174                 break;
175         case 2:         /* Base Board Information */
176                 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
177                 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
178                 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
179                 break;
180         case 10:        /* Onboard Devices Information */
181                 dmi_save_devices(dm);
182                 break;
183         case 38:        /* IPMI Device Information */
184                 dmi_save_ipmi_device(dm);
185         }
186 }
187
188 void __init dmi_scan_machine(void)
189 {
190         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
220                         if (dmi_table(base,len, num, dmi_decode) == 0)
221                                 return;
222                 }
223         }
224
225 out:    printk(KERN_INFO "DMI not present or invalid.\n");
226 }
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  */
237 int 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                 }
252                 count++;
253                 if (d->callback && d->callback(d))
254                         break;
255 fail:           d++;
256         }
257
258         return count;
259 }
260 EXPORT_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  */
269 char *dmi_get_system_info(int field)
270 {
271         return dmi_ident[field];
272 }
273 EXPORT_SYMBOL(dmi_get_system_info);
274
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  */
287 struct 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 }
302 EXPORT_SYMBOL(dmi_find_device);
303
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  */
310 int 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 }