blob: a3cdf894302b0d5fe48ecfcd1581be41d99c8932 [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
Andrey Panin1249c512005-06-25 14:54:47 -07009struct dmi_header {
10 u8 type;
11 u8 length;
12 u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013};
14
15#undef DMI_DEBUG
16
17#ifdef DMI_DEBUG
18#define dmi_printk(x) printk x
19#else
20#define dmi_printk(x)
21#endif
22
23static char * __init dmi_string(struct dmi_header *dm, u8 s)
24{
Andrey Panin1249c512005-06-25 14:54:47 -070025 u8 *bp = ((u8 *) dm) + dm->length;
26
27 if (!s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return "";
29 s--;
Andrey Panin1249c512005-06-25 14:54:47 -070030 while (s > 0 && *bp) {
31 bp += strlen(bp) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 s--;
33 }
34 return bp;
35}
36
37/*
38 * We have to be cautious here. We have seen BIOSes with DMI pointers
39 * pointing to completely the wrong place for example
40 */
Andrey Panin1249c512005-06-25 14:54:47 -070041static int __init dmi_table(u32 base, int len, int num,
42 void (*decode)(struct dmi_header *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Andrey Panin1249c512005-06-25 14:54:47 -070044 u8 *buf, *data;
45 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 buf = bt_ioremap(base, len);
Andrey Panin1249c512005-06-25 14:54:47 -070048 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return -1;
50
51 data = buf;
52
53 /*
54 * Stop when we see all the items the table claimed to have
55 * OR we run off the end of the table (also happens)
56 */
Andrey Panin1249c512005-06-25 14:54:47 -070057 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
58 struct dmi_header *dm = (struct dmi_header *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /*
60 * We want to know the total length (formated area and strings)
61 * before decoding to make sure we won't run off the table in
62 * dmi_decode or dmi_string
63 */
Andrey Panin1249c512005-06-25 14:54:47 -070064 data += dm->length;
65 while ((data - buf < len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 data++;
Andrey Panin1249c512005-06-25 14:54:47 -070067 if (data - buf < len - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 decode(dm);
Andrey Panin1249c512005-06-25 14:54:47 -070069 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 i++;
71 }
72 bt_iounmap(buf, len);
73 return 0;
74}
75
Andrey Panin1249c512005-06-25 14:54:47 -070076static int __init dmi_checksum(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Andrey Panin1249c512005-06-25 14:54:47 -070078 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int a;
80
Andrey Panin1249c512005-06-25 14:54:47 -070081 for (a = 0; a < 15; a++)
82 sum += buf[a];
83
84 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static int __init dmi_iterate(void (*decode)(struct dmi_header *))
88{
89 u8 buf[15];
90 char __iomem *p, *q;
91
92 /*
93 * no iounmap() for that ioremap(); it would be a no-op, but it's
94 * so early in setup that sucker gets confused into doing what
95 * it shouldn't if we actually call it.
96 */
97 p = ioremap(0xF0000, 0x10000);
98 if (p == NULL)
99 return -1;
Andrey Panin1249c512005-06-25 14:54:47 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 for (q = p; q < p + 0x10000; q += 16) {
102 memcpy_fromio(buf, q, 15);
Andrey Panin1249c512005-06-25 14:54:47 -0700103 if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
104 u16 num = (buf[13] << 8) | buf[12];
105 u16 len = (buf[7] << 8) | buf[6];
106 u32 base = (buf[11] << 24) | (buf[10] << 16) |
107 (buf[9] << 8) | buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /*
110 * DMI version 0.0 means that the real version is taken from
111 * the SMBIOS version, which we don't know at this point.
112 */
Andrey Panin1249c512005-06-25 14:54:47 -0700113 if (buf[14] != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 printk(KERN_INFO "DMI %d.%d present.\n",
Andrey Panin1249c512005-06-25 14:54:47 -0700115 buf[14] >> 4, buf[14] & 0xF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 else
117 printk(KERN_INFO "DMI present.\n");
Andrey Panin1249c512005-06-25 14:54:47 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 dmi_printk((KERN_INFO "%d structures occupying %d bytes.\n",
120 num, len));
Andrey Panin1249c512005-06-25 14:54:47 -0700121 dmi_printk((KERN_INFO "DMI table at 0x%08X.\n", base));
122
123 if (dmi_table(base,len, num, decode) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125 }
126 }
127 return -1;
128}
129
130static char *dmi_ident[DMI_STRING_MAX];
131
132/*
133 * Save a DMI string
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
136{
137 char *d = (char*)dm;
138 char *p = dmi_string(dm, d[string]);
Andrey Panin1249c512005-06-25 14:54:47 -0700139
140 if (p == NULL || *p == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return;
142 if (dmi_ident[slot])
143 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700144
145 dmi_ident[slot] = alloc_bootmem(strlen(p) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if(dmi_ident[slot])
147 strcpy(dmi_ident[slot], p);
148 else
149 printk(KERN_ERR "dmi_save_ident: out of memory.\n");
150}
151
152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * Process a DMI table entry. Right now all we care about are the BIOS
154 * and machine entries. For 2.5 we should pull the smbus controller info
155 * out of here.
156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void __init dmi_decode(struct dmi_header *dm)
158{
Andrey Panin1249c512005-06-25 14:54:47 -0700159 u8 *data __attribute__((__unused__)) = (u8 *)dm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Andrey Panin1249c512005-06-25 14:54:47 -0700161 switch(dm->type) {
162 case 0:
163 dmi_printk(("BIOS Vendor: %s\n", dmi_string(dm, data[4])));
164 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
165 dmi_printk(("BIOS Version: %s\n", dmi_string(dm, data[5])));
166 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
167 dmi_printk(("BIOS Release: %s\n", dmi_string(dm, data[8])));
168 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
169 break;
170 case 1:
171 dmi_printk(("System Vendor: %s\n", dmi_string(dm, data[4])));
172 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
173 dmi_printk(("Product Name: %s\n", dmi_string(dm, data[5])));
174 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
175 dmi_printk(("Version: %s\n", dmi_string(dm, data[6])));
176 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
177 dmi_printk(("Serial Number: %s\n", dmi_string(dm, data[7])));
178 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
179 break;
180 case 2:
181 dmi_printk(("Board Vendor: %s\n", dmi_string(dm, data[4])));
182 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
183 dmi_printk(("Board Name: %s\n", dmi_string(dm, data[5])));
184 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
185 dmi_printk(("Board Version: %s\n", dmi_string(dm, data[6])));
186 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189}
190
191void __init dmi_scan_machine(void)
192{
Andrey Paninb6258832005-06-25 14:54:46 -0700193 if (dmi_iterate(dmi_decode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk(KERN_INFO "DMI not present.\n");
195}
196
197
198/**
199 * dmi_check_system - check system DMI data
200 * @list: array of dmi_system_id structures to match against
201 *
202 * Walk the blacklist table running matching functions until someone
203 * returns non zero or we hit the end. Callback function is called for
204 * each successfull match. Returns the number of matches.
205 */
206int dmi_check_system(struct dmi_system_id *list)
207{
208 int i, count = 0;
209 struct dmi_system_id *d = list;
210
211 while (d->ident) {
212 for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
213 int s = d->matches[i].slot;
214 if (s == DMI_NONE)
215 continue;
216 if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
217 continue;
218 /* No match */
219 goto fail;
220 }
221 if (d->callback && d->callback(d))
222 break;
223 count++;
224fail: d++;
225 }
226
227 return count;
228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229EXPORT_SYMBOL(dmi_check_system);
230
231/**
232 * dmi_get_system_info - return DMI data value
233 * @field: data index (see enum dmi_filed)
234 *
235 * Returns one DMI data value, can be used to perform
236 * complex DMI data checks.
237 */
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700238char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 return dmi_ident[field];
241}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700242EXPORT_SYMBOL(dmi_get_system_info);