Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #include <linux/string.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/dmi.h> |
| 6 | #include <linux/bootmem.h> |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 7 | #include <linux/slab.h> |
Andi Kleen | f2d3efe | 2006-03-25 16:30:22 +0100 | [diff] [blame^] | 8 | #include <asm/dmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | static char * __init dmi_string(struct dmi_header *dm, u8 s) |
| 11 | { |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 12 | u8 *bp = ((u8 *) dm) + dm->length; |
Andrey Panin | c3c7120 | 2005-09-06 15:18:28 -0700 | [diff] [blame] | 13 | char *str = ""; |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 14 | |
Andrey Panin | c3c7120 | 2005-09-06 15:18:28 -0700 | [diff] [blame] | 15 | if (s) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | s--; |
Andrey Panin | c3c7120 | 2005-09-06 15:18:28 -0700 | [diff] [blame] | 17 | while (s > 0 && *bp) { |
| 18 | bp += strlen(bp) + 1; |
| 19 | s--; |
| 20 | } |
| 21 | |
| 22 | if (*bp != 0) { |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 23 | str = dmi_alloc(strlen(bp) + 1); |
Andrey Panin | c3c7120 | 2005-09-06 15:18:28 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 38 | static int __init dmi_table(u32 base, int len, int num, |
| 39 | void (*decode)(struct dmi_header *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 41 | u8 *buf, *data; |
| 42 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 44 | buf = dmi_ioremap(base, len); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 45 | if (buf == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 54 | while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) { |
| 55 | struct dmi_header *dm = (struct dmi_header *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 61 | data += dm->length; |
| 62 | while ((data - buf < len - 1) && (data[0] || data[1])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | data++; |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 64 | if (data - buf < len - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | decode(dm); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 66 | data += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | i++; |
| 68 | } |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 69 | dmi_iounmap(buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 73 | static int __init dmi_checksum(u8 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 75 | u8 sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | int a; |
| 77 | |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 78 | for (a = 0; a < 15; a++) |
| 79 | sum += buf[a]; |
| 80 | |
| 81 | return sum == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static char *dmi_ident[DMI_STRING_MAX]; |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 85 | static LIST_HEAD(dmi_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Save a DMI string |
| 89 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string) |
| 91 | { |
Andrey Panin | c3c7120 | 2005-09-06 15:18:28 -0700 | [diff] [blame] | 92 | char *p, *d = (char*) dm; |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (dmi_ident[slot]) |
| 95 | return; |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 96 | |
Andrey Panin | c3c7120 | 2005-09-06 15:18:28 -0700 | [diff] [blame] | 97 | p = dmi_string(dm, d[string]); |
| 98 | if (p == NULL) |
| 99 | return; |
| 100 | |
| 101 | dmi_ident[slot] = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 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++) { |
Andrey Panin | bc83455 | 2006-03-25 03:06:31 -0800 | [diff] [blame] | 110 | char *d = (char *)(dm + 1) + (i * 2); |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 111 | |
| 112 | /* Skip disabled device */ |
| 113 | if ((*d & 0x80) == 0) |
| 114 | continue; |
| 115 | |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 116 | dev = dmi_alloc(sizeof(*dev)); |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 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 | |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 135 | data = dmi_alloc(dm->length); |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 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 | |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 143 | dev = dmi_alloc(sizeof(*dev)); |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | static void __init dmi_decode(struct dmi_header *dm) |
| 162 | { |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 163 | switch(dm->type) { |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 164 | case 0: /* BIOS Information */ |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 165 | dmi_save_ident(dm, DMI_BIOS_VENDOR, 4); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 166 | dmi_save_ident(dm, DMI_BIOS_VERSION, 5); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 167 | dmi_save_ident(dm, DMI_BIOS_DATE, 8); |
| 168 | break; |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 169 | case 1: /* System Information */ |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 170 | dmi_save_ident(dm, DMI_SYS_VENDOR, 4); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 171 | dmi_save_ident(dm, DMI_PRODUCT_NAME, 5); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 172 | dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 173 | dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7); |
| 174 | break; |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 175 | case 2: /* Base Board Information */ |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 176 | dmi_save_ident(dm, DMI_BOARD_VENDOR, 4); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 177 | dmi_save_ident(dm, DMI_BOARD_NAME, 5); |
Andrey Panin | 1249c51 | 2005-06-25 14:54:47 -0700 | [diff] [blame] | 178 | dmi_save_ident(dm, DMI_BOARD_VERSION, 6); |
| 179 | break; |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | void __init dmi_scan_machine(void) |
| 189 | { |
Andrey Panin | 61e032f | 2005-09-06 15:18:26 -0700 | [diff] [blame] | 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 | |
Andrey Panin | 61e032f | 2005-09-06 15:18:26 -0700 | [diff] [blame] | 220 | if (dmi_table(base,len, num, dmi_decode) == 0) |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | |
Andi Kleen | e992867 | 2006-01-11 22:43:33 +0100 | [diff] [blame] | 225 | out: printk(KERN_INFO "DMI not present or invalid.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Robert Love | 640e803 | 2005-09-06 15:18:30 -0700 | [diff] [blame] | 252 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | if (d->callback && d->callback(d)) |
| 254 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | fail: d++; |
| 256 | } |
| 257 | |
| 258 | return count; |
| 259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Dmitry Torokhov | e70c9d5 | 2005-06-25 14:54:25 -0700 | [diff] [blame] | 269 | char *dmi_get_system_info(int field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | return dmi_ident[field]; |
| 272 | } |
Dmitry Torokhov | e70c9d5 | 2005-06-25 14:54:25 -0700 | [diff] [blame] | 273 | EXPORT_SYMBOL(dmi_get_system_info); |
Andrey Panin | ebad6a4 | 2005-09-06 15:18:29 -0700 | [diff] [blame] | 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); |
Andi Kleen | f083a32 | 2006-03-25 16:30:19 +0100 | [diff] [blame] | 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 | } |