xref: /openbmc/linux/drivers/firmware/dmi_scan.c (revision 50c6c519)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/string.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/ctype.h>
7 #include <linux/dmi.h>
8 #include <linux/efi.h>
9 #include <linux/memblock.h>
10 #include <linux/random.h>
11 #include <asm/dmi.h>
12 #include <asm/unaligned.h>
13 
14 #ifndef SMBIOS_ENTRY_POINT_SCAN_START
15 #define SMBIOS_ENTRY_POINT_SCAN_START 0xF0000
16 #endif
17 
18 struct kobject *dmi_kobj;
19 EXPORT_SYMBOL_GPL(dmi_kobj);
20 
21 /*
22  * DMI stands for "Desktop Management Interface".  It is part
23  * of and an antecedent to, SMBIOS, which stands for System
24  * Management BIOS.  See further: https://www.dmtf.org/standards
25  */
26 static const char dmi_empty_string[] = "";
27 
28 static u32 dmi_ver __initdata;
29 static u32 dmi_len;
30 static u16 dmi_num;
31 static u8 smbios_entry_point[32];
32 static int smbios_entry_point_size;
33 
34 /* DMI system identification string used during boot */
35 static char dmi_ids_string[128] __initdata;
36 
37 static struct dmi_memdev_info {
38 	const char *device;
39 	const char *bank;
40 	u64 size;		/* bytes */
41 	u16 handle;
42 	u8 type;		/* DDR2, DDR3, DDR4 etc */
43 } *dmi_memdev;
44 static int dmi_memdev_nr;
45 
dmi_string_nosave(const struct dmi_header * dm,u8 s)46 static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
47 {
48 	const u8 *bp = ((u8 *) dm) + dm->length;
49 	const u8 *nsp;
50 
51 	if (s) {
52 		while (--s > 0 && *bp)
53 			bp += strlen(bp) + 1;
54 
55 		/* Strings containing only spaces are considered empty */
56 		nsp = bp;
57 		while (*nsp == ' ')
58 			nsp++;
59 		if (*nsp != '\0')
60 			return bp;
61 	}
62 
63 	return dmi_empty_string;
64 }
65 
dmi_string(const struct dmi_header * dm,u8 s)66 static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
67 {
68 	const char *bp = dmi_string_nosave(dm, s);
69 	char *str;
70 	size_t len;
71 
72 	if (bp == dmi_empty_string)
73 		return dmi_empty_string;
74 
75 	len = strlen(bp) + 1;
76 	str = dmi_alloc(len);
77 	if (str != NULL)
78 		strcpy(str, bp);
79 
80 	return str;
81 }
82 
83 /*
84  *	We have to be cautious here. We have seen BIOSes with DMI pointers
85  *	pointing to completely the wrong place for example
86  */
dmi_decode_table(u8 * buf,void (* decode)(const struct dmi_header *,void *),void * private_data)87 static void dmi_decode_table(u8 *buf,
88 			     void (*decode)(const struct dmi_header *, void *),
89 			     void *private_data)
90 {
91 	u8 *data = buf;
92 	int i = 0;
93 
94 	/*
95 	 * Stop when we have seen all the items the table claimed to have
96 	 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
97 	 * >= 3.0 only) OR we run off the end of the table (should never
98 	 * happen but sometimes does on bogus implementations.)
99 	 */
100 	while ((!dmi_num || i < dmi_num) &&
101 	       (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
102 		const struct dmi_header *dm = (const struct dmi_header *)data;
103 
104 		/*
105 		 * If a short entry is found (less than 4 bytes), not only it
106 		 * is invalid, but we cannot reliably locate the next entry.
107 		 */
108 		if (dm->length < sizeof(struct dmi_header)) {
109 			pr_warn(FW_BUG
110 				"Corrupted DMI table, offset %zd (only %d entries processed)\n",
111 				data - buf, i);
112 			break;
113 		}
114 
115 		/*
116 		 *  We want to know the total length (formatted area and
117 		 *  strings) before decoding to make sure we won't run off the
118 		 *  table in dmi_decode or dmi_string
119 		 */
120 		data += dm->length;
121 		while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
122 			data++;
123 		if (data - buf < dmi_len - 1)
124 			decode(dm, private_data);
125 
126 		data += 2;
127 		i++;
128 
129 		/*
130 		 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
131 		 * For tables behind a 64-bit entry point, we have no item
132 		 * count and no exact table length, so stop on end-of-table
133 		 * marker. For tables behind a 32-bit entry point, we have
134 		 * seen OEM structures behind the end-of-table marker on
135 		 * some systems, so don't trust it.
136 		 */
137 		if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
138 			break;
139 	}
140 
141 	/* Trim DMI table length if needed */
142 	if (dmi_len > data - buf)
143 		dmi_len = data - buf;
144 }
145 
146 static phys_addr_t dmi_base;
147 
dmi_walk_early(void (* decode)(const struct dmi_header *,void *))148 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
149 		void *))
150 {
151 	u8 *buf;
152 	u32 orig_dmi_len = dmi_len;
153 
154 	buf = dmi_early_remap(dmi_base, orig_dmi_len);
155 	if (buf == NULL)
156 		return -ENOMEM;
157 
158 	dmi_decode_table(buf, decode, NULL);
159 
160 	add_device_randomness(buf, dmi_len);
161 
162 	dmi_early_unmap(buf, orig_dmi_len);
163 	return 0;
164 }
165 
dmi_checksum(const u8 * buf,u8 len)166 static int __init dmi_checksum(const u8 *buf, u8 len)
167 {
168 	u8 sum = 0;
169 	int a;
170 
171 	for (a = 0; a < len; a++)
172 		sum += buf[a];
173 
174 	return sum == 0;
175 }
176 
177 static const char *dmi_ident[DMI_STRING_MAX];
178 static LIST_HEAD(dmi_devices);
179 int dmi_available;
180 EXPORT_SYMBOL_GPL(dmi_available);
181 
182 /*
183  *	Save a DMI string
184  */
dmi_save_ident(const struct dmi_header * dm,int slot,int string)185 static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
186 		int string)
187 {
188 	const char *d = (const char *) dm;
189 	const char *p;
190 
191 	if (dmi_ident[slot] || dm->length <= string)
192 		return;
193 
194 	p = dmi_string(dm, d[string]);
195 	if (p == NULL)
196 		return;
197 
198 	dmi_ident[slot] = p;
199 }
200 
dmi_save_release(const struct dmi_header * dm,int slot,int index)201 static void __init dmi_save_release(const struct dmi_header *dm, int slot,
202 		int index)
203 {
204 	const u8 *minor, *major;
205 	char *s;
206 
207 	/* If the table doesn't have the field, let's return */
208 	if (dmi_ident[slot] || dm->length < index)
209 		return;
210 
211 	minor = (u8 *) dm + index;
212 	major = (u8 *) dm + index - 1;
213 
214 	/* As per the spec, if the system doesn't support this field,
215 	 * the value is FF
216 	 */
217 	if (*major == 0xFF && *minor == 0xFF)
218 		return;
219 
220 	s = dmi_alloc(8);
221 	if (!s)
222 		return;
223 
224 	sprintf(s, "%u.%u", *major, *minor);
225 
226 	dmi_ident[slot] = s;
227 }
228 
dmi_save_uuid(const struct dmi_header * dm,int slot,int index)229 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
230 		int index)
231 {
232 	const u8 *d;
233 	char *s;
234 	int is_ff = 1, is_00 = 1, i;
235 
236 	if (dmi_ident[slot] || dm->length < index + 16)
237 		return;
238 
239 	d = (u8 *) dm + index;
240 	for (i = 0; i < 16 && (is_ff || is_00); i++) {
241 		if (d[i] != 0x00)
242 			is_00 = 0;
243 		if (d[i] != 0xFF)
244 			is_ff = 0;
245 	}
246 
247 	if (is_ff || is_00)
248 		return;
249 
250 	s = dmi_alloc(16*2+4+1);
251 	if (!s)
252 		return;
253 
254 	/*
255 	 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
256 	 * the UUID are supposed to be little-endian encoded.  The specification
257 	 * says that this is the defacto standard.
258 	 */
259 	if (dmi_ver >= 0x020600)
260 		sprintf(s, "%pUl", d);
261 	else
262 		sprintf(s, "%pUb", d);
263 
264 	dmi_ident[slot] = s;
265 }
266 
dmi_save_type(const struct dmi_header * dm,int slot,int index)267 static void __init dmi_save_type(const struct dmi_header *dm, int slot,
268 		int index)
269 {
270 	const u8 *d;
271 	char *s;
272 
273 	if (dmi_ident[slot] || dm->length <= index)
274 		return;
275 
276 	s = dmi_alloc(4);
277 	if (!s)
278 		return;
279 
280 	d = (u8 *) dm + index;
281 	sprintf(s, "%u", *d & 0x7F);
282 	dmi_ident[slot] = s;
283 }
284 
dmi_save_one_device(int type,const char * name)285 static void __init dmi_save_one_device(int type, const char *name)
286 {
287 	struct dmi_device *dev;
288 
289 	/* No duplicate device */
290 	if (dmi_find_device(type, name, NULL))
291 		return;
292 
293 	dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
294 	if (!dev)
295 		return;
296 
297 	dev->type = type;
298 	strcpy((char *)(dev + 1), name);
299 	dev->name = (char *)(dev + 1);
300 	dev->device_data = NULL;
301 	list_add(&dev->list, &dmi_devices);
302 }
303 
dmi_save_devices(const struct dmi_header * dm)304 static void __init dmi_save_devices(const struct dmi_header *dm)
305 {
306 	int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
307 
308 	for (i = 0; i < count; i++) {
309 		const char *d = (char *)(dm + 1) + (i * 2);
310 
311 		/* Skip disabled device */
312 		if ((*d & 0x80) == 0)
313 			continue;
314 
315 		dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
316 	}
317 }
318 
dmi_save_oem_strings_devices(const struct dmi_header * dm)319 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
320 {
321 	int i, count;
322 	struct dmi_device *dev;
323 
324 	if (dm->length < 0x05)
325 		return;
326 
327 	count = *(u8 *)(dm + 1);
328 	for (i = 1; i <= count; i++) {
329 		const char *devname = dmi_string(dm, i);
330 
331 		if (devname == dmi_empty_string)
332 			continue;
333 
334 		dev = dmi_alloc(sizeof(*dev));
335 		if (!dev)
336 			break;
337 
338 		dev->type = DMI_DEV_TYPE_OEM_STRING;
339 		dev->name = devname;
340 		dev->device_data = NULL;
341 
342 		list_add(&dev->list, &dmi_devices);
343 	}
344 }
345 
dmi_save_ipmi_device(const struct dmi_header * dm)346 static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
347 {
348 	struct dmi_device *dev;
349 	void *data;
350 
351 	data = dmi_alloc(dm->length);
352 	if (data == NULL)
353 		return;
354 
355 	memcpy(data, dm, dm->length);
356 
357 	dev = dmi_alloc(sizeof(*dev));
358 	if (!dev)
359 		return;
360 
361 	dev->type = DMI_DEV_TYPE_IPMI;
362 	dev->name = "IPMI controller";
363 	dev->device_data = data;
364 
365 	list_add_tail(&dev->list, &dmi_devices);
366 }
367 
dmi_save_dev_pciaddr(int instance,int segment,int bus,int devfn,const char * name,int type)368 static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus,
369 					int devfn, const char *name, int type)
370 {
371 	struct dmi_dev_onboard *dev;
372 
373 	/* Ignore invalid values */
374 	if (type == DMI_DEV_TYPE_DEV_SLOT &&
375 	    segment == 0xFFFF && bus == 0xFF && devfn == 0xFF)
376 		return;
377 
378 	dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
379 	if (!dev)
380 		return;
381 
382 	dev->instance = instance;
383 	dev->segment = segment;
384 	dev->bus = bus;
385 	dev->devfn = devfn;
386 
387 	strcpy((char *)&dev[1], name);
388 	dev->dev.type = type;
389 	dev->dev.name = (char *)&dev[1];
390 	dev->dev.device_data = dev;
391 
392 	list_add(&dev->dev.list, &dmi_devices);
393 }
394 
dmi_save_extended_devices(const struct dmi_header * dm)395 static void __init dmi_save_extended_devices(const struct dmi_header *dm)
396 {
397 	const char *name;
398 	const u8 *d = (u8 *)dm;
399 
400 	if (dm->length < 0x0B)
401 		return;
402 
403 	/* Skip disabled device */
404 	if ((d[0x5] & 0x80) == 0)
405 		return;
406 
407 	name = dmi_string_nosave(dm, d[0x4]);
408 	dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name,
409 			     DMI_DEV_TYPE_DEV_ONBOARD);
410 	dmi_save_one_device(d[0x5] & 0x7f, name);
411 }
412 
dmi_save_system_slot(const struct dmi_header * dm)413 static void __init dmi_save_system_slot(const struct dmi_header *dm)
414 {
415 	const u8 *d = (u8 *)dm;
416 
417 	/* Need SMBIOS 2.6+ structure */
418 	if (dm->length < 0x11)
419 		return;
420 	dmi_save_dev_pciaddr(*(u16 *)(d + 0x9), *(u16 *)(d + 0xD), d[0xF],
421 			     d[0x10], dmi_string_nosave(dm, d[0x4]),
422 			     DMI_DEV_TYPE_DEV_SLOT);
423 }
424 
count_mem_devices(const struct dmi_header * dm,void * v)425 static void __init count_mem_devices(const struct dmi_header *dm, void *v)
426 {
427 	if (dm->type != DMI_ENTRY_MEM_DEVICE)
428 		return;
429 	dmi_memdev_nr++;
430 }
431 
save_mem_devices(const struct dmi_header * dm,void * v)432 static void __init save_mem_devices(const struct dmi_header *dm, void *v)
433 {
434 	const char *d = (const char *)dm;
435 	static int nr;
436 	u64 bytes;
437 	u16 size;
438 
439 	if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x13)
440 		return;
441 	if (nr >= dmi_memdev_nr) {
442 		pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
443 		return;
444 	}
445 	dmi_memdev[nr].handle = get_unaligned(&dm->handle);
446 	dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
447 	dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
448 	dmi_memdev[nr].type = d[0x12];
449 
450 	size = get_unaligned((u16 *)&d[0xC]);
451 	if (size == 0)
452 		bytes = 0;
453 	else if (size == 0xffff)
454 		bytes = ~0ull;
455 	else if (size & 0x8000)
456 		bytes = (u64)(size & 0x7fff) << 10;
457 	else if (size != 0x7fff || dm->length < 0x20)
458 		bytes = (u64)size << 20;
459 	else
460 		bytes = (u64)get_unaligned((u32 *)&d[0x1C]) << 20;
461 
462 	dmi_memdev[nr].size = bytes;
463 	nr++;
464 }
465 
dmi_memdev_walk(void)466 static void __init dmi_memdev_walk(void)
467 {
468 	if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
469 		dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
470 		if (dmi_memdev)
471 			dmi_walk_early(save_mem_devices);
472 	}
473 }
474 
475 /*
476  *	Process a DMI table entry. Right now all we care about are the BIOS
477  *	and machine entries. For 2.5 we should pull the smbus controller info
478  *	out of here.
479  */
dmi_decode(const struct dmi_header * dm,void * dummy)480 static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
481 {
482 	switch (dm->type) {
483 	case 0:		/* BIOS Information */
484 		dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
485 		dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
486 		dmi_save_ident(dm, DMI_BIOS_DATE, 8);
487 		dmi_save_release(dm, DMI_BIOS_RELEASE, 21);
488 		dmi_save_release(dm, DMI_EC_FIRMWARE_RELEASE, 23);
489 		break;
490 	case 1:		/* System Information */
491 		dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
492 		dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
493 		dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
494 		dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
495 		dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
496 		dmi_save_ident(dm, DMI_PRODUCT_SKU, 25);
497 		dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26);
498 		break;
499 	case 2:		/* Base Board Information */
500 		dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
501 		dmi_save_ident(dm, DMI_BOARD_NAME, 5);
502 		dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
503 		dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
504 		dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
505 		break;
506 	case 3:		/* Chassis Information */
507 		dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
508 		dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
509 		dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
510 		dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
511 		dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
512 		break;
513 	case 9:		/* System Slots */
514 		dmi_save_system_slot(dm);
515 		break;
516 	case 10:	/* Onboard Devices Information */
517 		dmi_save_devices(dm);
518 		break;
519 	case 11:	/* OEM Strings */
520 		dmi_save_oem_strings_devices(dm);
521 		break;
522 	case 38:	/* IPMI Device Information */
523 		dmi_save_ipmi_device(dm);
524 		break;
525 	case 41:	/* Onboard Devices Extended Information */
526 		dmi_save_extended_devices(dm);
527 	}
528 }
529 
print_filtered(char * buf,size_t len,const char * info)530 static int __init print_filtered(char *buf, size_t len, const char *info)
531 {
532 	int c = 0;
533 	const char *p;
534 
535 	if (!info)
536 		return c;
537 
538 	for (p = info; *p; p++)
539 		if (isprint(*p))
540 			c += scnprintf(buf + c, len - c, "%c", *p);
541 		else
542 			c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
543 	return c;
544 }
545 
dmi_format_ids(char * buf,size_t len)546 static void __init dmi_format_ids(char *buf, size_t len)
547 {
548 	int c = 0;
549 	const char *board;	/* Board Name is optional */
550 
551 	c += print_filtered(buf + c, len - c,
552 			    dmi_get_system_info(DMI_SYS_VENDOR));
553 	c += scnprintf(buf + c, len - c, " ");
554 	c += print_filtered(buf + c, len - c,
555 			    dmi_get_system_info(DMI_PRODUCT_NAME));
556 
557 	board = dmi_get_system_info(DMI_BOARD_NAME);
558 	if (board) {
559 		c += scnprintf(buf + c, len - c, "/");
560 		c += print_filtered(buf + c, len - c, board);
561 	}
562 	c += scnprintf(buf + c, len - c, ", BIOS ");
563 	c += print_filtered(buf + c, len - c,
564 			    dmi_get_system_info(DMI_BIOS_VERSION));
565 	c += scnprintf(buf + c, len - c, " ");
566 	c += print_filtered(buf + c, len - c,
567 			    dmi_get_system_info(DMI_BIOS_DATE));
568 }
569 
570 /*
571  * Check for DMI/SMBIOS headers in the system firmware image.  Any
572  * SMBIOS header must start 16 bytes before the DMI header, so take a
573  * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
574  * 0.  If the DMI header is present, set dmi_ver accordingly (SMBIOS
575  * takes precedence) and return 0.  Otherwise return 1.
576  */
dmi_present(const u8 * buf)577 static int __init dmi_present(const u8 *buf)
578 {
579 	u32 smbios_ver;
580 
581 	/*
582 	 * The size of this structure is 31 bytes, but we also accept value
583 	 * 30 due to a mistake in SMBIOS specification version 2.1.
584 	 */
585 	if (memcmp(buf, "_SM_", 4) == 0 &&
586 	    buf[5] >= 30 && buf[5] <= 32 &&
587 	    dmi_checksum(buf, buf[5])) {
588 		smbios_ver = get_unaligned_be16(buf + 6);
589 		smbios_entry_point_size = buf[5];
590 		memcpy(smbios_entry_point, buf, smbios_entry_point_size);
591 
592 		/* Some BIOS report weird SMBIOS version, fix that up */
593 		switch (smbios_ver) {
594 		case 0x021F:
595 		case 0x0221:
596 			pr_debug("SMBIOS version fixup (2.%d->2.%d)\n",
597 				 smbios_ver & 0xFF, 3);
598 			smbios_ver = 0x0203;
599 			break;
600 		case 0x0233:
601 			pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6);
602 			smbios_ver = 0x0206;
603 			break;
604 		}
605 	} else {
606 		smbios_ver = 0;
607 	}
608 
609 	buf += 16;
610 
611 	if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
612 		if (smbios_ver)
613 			dmi_ver = smbios_ver;
614 		else
615 			dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
616 		dmi_ver <<= 8;
617 		dmi_num = get_unaligned_le16(buf + 12);
618 		dmi_len = get_unaligned_le16(buf + 6);
619 		dmi_base = get_unaligned_le32(buf + 8);
620 
621 		if (dmi_walk_early(dmi_decode) == 0) {
622 			if (smbios_ver) {
623 				pr_info("SMBIOS %d.%d present.\n",
624 					dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
625 			} else {
626 				smbios_entry_point_size = 15;
627 				memcpy(smbios_entry_point, buf,
628 				       smbios_entry_point_size);
629 				pr_info("Legacy DMI %d.%d present.\n",
630 					dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
631 			}
632 			dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
633 			pr_info("DMI: %s\n", dmi_ids_string);
634 			return 0;
635 		}
636 	}
637 
638 	return 1;
639 }
640 
641 /*
642  * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
643  * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
644  */
dmi_smbios3_present(const u8 * buf)645 static int __init dmi_smbios3_present(const u8 *buf)
646 {
647 	if (memcmp(buf, "_SM3_", 5) == 0 &&
648 	    buf[6] >= 24 && buf[6] <= 32 &&
649 	    dmi_checksum(buf, buf[6])) {
650 		dmi_ver = get_unaligned_be24(buf + 7);
651 		dmi_num = 0;			/* No longer specified */
652 		dmi_len = get_unaligned_le32(buf + 12);
653 		dmi_base = get_unaligned_le64(buf + 16);
654 		smbios_entry_point_size = buf[6];
655 		memcpy(smbios_entry_point, buf, smbios_entry_point_size);
656 
657 		if (dmi_walk_early(dmi_decode) == 0) {
658 			pr_info("SMBIOS %d.%d.%d present.\n",
659 				dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
660 				dmi_ver & 0xFF);
661 			dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
662 			pr_info("DMI: %s\n", dmi_ids_string);
663 			return 0;
664 		}
665 	}
666 	return 1;
667 }
668 
dmi_scan_machine(void)669 static void __init dmi_scan_machine(void)
670 {
671 	char __iomem *p, *q;
672 	char buf[32];
673 
674 	if (efi_enabled(EFI_CONFIG_TABLES)) {
675 		/*
676 		 * According to the DMTF SMBIOS reference spec v3.0.0, it is
677 		 * allowed to define both the 64-bit entry point (smbios3) and
678 		 * the 32-bit entry point (smbios), in which case they should
679 		 * either both point to the same SMBIOS structure table, or the
680 		 * table pointed to by the 64-bit entry point should contain a
681 		 * superset of the table contents pointed to by the 32-bit entry
682 		 * point (section 5.2)
683 		 * This implies that the 64-bit entry point should have
684 		 * precedence if it is defined and supported by the OS. If we
685 		 * have the 64-bit entry point, but fail to decode it, fall
686 		 * back to the legacy one (if available)
687 		 */
688 		if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
689 			p = dmi_early_remap(efi.smbios3, 32);
690 			if (p == NULL)
691 				goto error;
692 			memcpy_fromio(buf, p, 32);
693 			dmi_early_unmap(p, 32);
694 
695 			if (!dmi_smbios3_present(buf)) {
696 				dmi_available = 1;
697 				return;
698 			}
699 		}
700 		if (efi.smbios == EFI_INVALID_TABLE_ADDR)
701 			goto error;
702 
703 		/* This is called as a core_initcall() because it isn't
704 		 * needed during early boot.  This also means we can
705 		 * iounmap the space when we're done with it.
706 		 */
707 		p = dmi_early_remap(efi.smbios, 32);
708 		if (p == NULL)
709 			goto error;
710 		memcpy_fromio(buf, p, 32);
711 		dmi_early_unmap(p, 32);
712 
713 		if (!dmi_present(buf)) {
714 			dmi_available = 1;
715 			return;
716 		}
717 	} else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
718 		p = dmi_early_remap(SMBIOS_ENTRY_POINT_SCAN_START, 0x10000);
719 		if (p == NULL)
720 			goto error;
721 
722 		/*
723 		 * Same logic as above, look for a 64-bit entry point
724 		 * first, and if not found, fall back to 32-bit entry point.
725 		 */
726 		memcpy_fromio(buf, p, 16);
727 		for (q = p + 16; q < p + 0x10000; q += 16) {
728 			memcpy_fromio(buf + 16, q, 16);
729 			if (!dmi_smbios3_present(buf)) {
730 				dmi_available = 1;
731 				dmi_early_unmap(p, 0x10000);
732 				return;
733 			}
734 			memcpy(buf, buf + 16, 16);
735 		}
736 
737 		/*
738 		 * Iterate over all possible DMI header addresses q.
739 		 * Maintain the 32 bytes around q in buf.  On the
740 		 * first iteration, substitute zero for the
741 		 * out-of-range bytes so there is no chance of falsely
742 		 * detecting an SMBIOS header.
743 		 */
744 		memset(buf, 0, 16);
745 		for (q = p; q < p + 0x10000; q += 16) {
746 			memcpy_fromio(buf + 16, q, 16);
747 			if (!dmi_present(buf)) {
748 				dmi_available = 1;
749 				dmi_early_unmap(p, 0x10000);
750 				return;
751 			}
752 			memcpy(buf, buf + 16, 16);
753 		}
754 		dmi_early_unmap(p, 0x10000);
755 	}
756  error:
757 	pr_info("DMI not present or invalid.\n");
758 }
759 
raw_table_read(struct file * file,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t pos,size_t count)760 static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
761 			      struct bin_attribute *attr, char *buf,
762 			      loff_t pos, size_t count)
763 {
764 	memcpy(buf, attr->private + pos, count);
765 	return count;
766 }
767 
768 static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
769 static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
770 
dmi_init(void)771 static int __init dmi_init(void)
772 {
773 	struct kobject *tables_kobj;
774 	u8 *dmi_table;
775 	int ret = -ENOMEM;
776 
777 	if (!dmi_available)
778 		return 0;
779 
780 	/*
781 	 * Set up dmi directory at /sys/firmware/dmi. This entry should stay
782 	 * even after farther error, as it can be used by other modules like
783 	 * dmi-sysfs.
784 	 */
785 	dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
786 	if (!dmi_kobj)
787 		goto err;
788 
789 	tables_kobj = kobject_create_and_add("tables", dmi_kobj);
790 	if (!tables_kobj)
791 		goto err;
792 
793 	dmi_table = dmi_remap(dmi_base, dmi_len);
794 	if (!dmi_table)
795 		goto err_tables;
796 
797 	bin_attr_smbios_entry_point.size = smbios_entry_point_size;
798 	bin_attr_smbios_entry_point.private = smbios_entry_point;
799 	ret = sysfs_create_bin_file(tables_kobj, &bin_attr_smbios_entry_point);
800 	if (ret)
801 		goto err_unmap;
802 
803 	bin_attr_DMI.size = dmi_len;
804 	bin_attr_DMI.private = dmi_table;
805 	ret = sysfs_create_bin_file(tables_kobj, &bin_attr_DMI);
806 	if (!ret)
807 		return 0;
808 
809 	sysfs_remove_bin_file(tables_kobj,
810 			      &bin_attr_smbios_entry_point);
811  err_unmap:
812 	dmi_unmap(dmi_table);
813  err_tables:
814 	kobject_del(tables_kobj);
815 	kobject_put(tables_kobj);
816  err:
817 	pr_err("dmi: Firmware registration failed.\n");
818 
819 	return ret;
820 }
821 subsys_initcall(dmi_init);
822 
823 /**
824  *	dmi_setup - scan and setup DMI system information
825  *
826  *	Scan the DMI system information. This setups DMI identifiers
827  *	(dmi_system_id) for printing it out on task dumps and prepares
828  *	DIMM entry information (dmi_memdev_info) from the SMBIOS table
829  *	for using this when reporting memory errors.
830  */
dmi_setup(void)831 void __init dmi_setup(void)
832 {
833 	dmi_scan_machine();
834 	if (!dmi_available)
835 		return;
836 
837 	dmi_memdev_walk();
838 	dump_stack_set_arch_desc("%s", dmi_ids_string);
839 }
840 
841 /**
842  *	dmi_matches - check if dmi_system_id structure matches system DMI data
843  *	@dmi: pointer to the dmi_system_id structure to check
844  */
dmi_matches(const struct dmi_system_id * dmi)845 static bool dmi_matches(const struct dmi_system_id *dmi)
846 {
847 	int i;
848 
849 	for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
850 		int s = dmi->matches[i].slot;
851 		if (s == DMI_NONE)
852 			break;
853 		if (s == DMI_OEM_STRING) {
854 			/* DMI_OEM_STRING must be exact match */
855 			const struct dmi_device *valid;
856 
857 			valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING,
858 						dmi->matches[i].substr, NULL);
859 			if (valid)
860 				continue;
861 		} else if (dmi_ident[s]) {
862 			if (dmi->matches[i].exact_match) {
863 				if (!strcmp(dmi_ident[s],
864 					    dmi->matches[i].substr))
865 					continue;
866 			} else {
867 				if (strstr(dmi_ident[s],
868 					   dmi->matches[i].substr))
869 					continue;
870 			}
871 		}
872 
873 		/* No match */
874 		return false;
875 	}
876 	return true;
877 }
878 
879 /**
880  *	dmi_is_end_of_table - check for end-of-table marker
881  *	@dmi: pointer to the dmi_system_id structure to check
882  */
dmi_is_end_of_table(const struct dmi_system_id * dmi)883 static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
884 {
885 	return dmi->matches[0].slot == DMI_NONE;
886 }
887 
888 /**
889  *	dmi_check_system - check system DMI data
890  *	@list: array of dmi_system_id structures to match against
891  *		All non-null elements of the list must match
892  *		their slot's (field index's) data (i.e., each
893  *		list string must be a substring of the specified
894  *		DMI slot's string data) to be considered a
895  *		successful match.
896  *
897  *	Walk the blacklist table running matching functions until someone
898  *	returns non zero or we hit the end. Callback function is called for
899  *	each successful match. Returns the number of matches.
900  *
901  *	dmi_setup must be called before this function is called.
902  */
dmi_check_system(const struct dmi_system_id * list)903 int dmi_check_system(const struct dmi_system_id *list)
904 {
905 	int count = 0;
906 	const struct dmi_system_id *d;
907 
908 	for (d = list; !dmi_is_end_of_table(d); d++)
909 		if (dmi_matches(d)) {
910 			count++;
911 			if (d->callback && d->callback(d))
912 				break;
913 		}
914 
915 	return count;
916 }
917 EXPORT_SYMBOL(dmi_check_system);
918 
919 /**
920  *	dmi_first_match - find dmi_system_id structure matching system DMI data
921  *	@list: array of dmi_system_id structures to match against
922  *		All non-null elements of the list must match
923  *		their slot's (field index's) data (i.e., each
924  *		list string must be a substring of the specified
925  *		DMI slot's string data) to be considered a
926  *		successful match.
927  *
928  *	Walk the blacklist table until the first match is found.  Return the
929  *	pointer to the matching entry or NULL if there's no match.
930  *
931  *	dmi_setup must be called before this function is called.
932  */
dmi_first_match(const struct dmi_system_id * list)933 const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
934 {
935 	const struct dmi_system_id *d;
936 
937 	for (d = list; !dmi_is_end_of_table(d); d++)
938 		if (dmi_matches(d))
939 			return d;
940 
941 	return NULL;
942 }
943 EXPORT_SYMBOL(dmi_first_match);
944 
945 /**
946  *	dmi_get_system_info - return DMI data value
947  *	@field: data index (see enum dmi_field)
948  *
949  *	Returns one DMI data value, can be used to perform
950  *	complex DMI data checks.
951  */
dmi_get_system_info(int field)952 const char *dmi_get_system_info(int field)
953 {
954 	return dmi_ident[field];
955 }
956 EXPORT_SYMBOL(dmi_get_system_info);
957 
958 /**
959  * dmi_name_in_serial - Check if string is in the DMI product serial information
960  * @str: string to check for
961  */
dmi_name_in_serial(const char * str)962 int dmi_name_in_serial(const char *str)
963 {
964 	int f = DMI_PRODUCT_SERIAL;
965 	if (dmi_ident[f] && strstr(dmi_ident[f], str))
966 		return 1;
967 	return 0;
968 }
969 
970 /**
971  *	dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
972  *	@str: Case sensitive Name
973  */
dmi_name_in_vendors(const char * str)974 int dmi_name_in_vendors(const char *str)
975 {
976 	static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
977 	int i;
978 	for (i = 0; fields[i] != DMI_NONE; i++) {
979 		int f = fields[i];
980 		if (dmi_ident[f] && strstr(dmi_ident[f], str))
981 			return 1;
982 	}
983 	return 0;
984 }
985 EXPORT_SYMBOL(dmi_name_in_vendors);
986 
987 /**
988  *	dmi_find_device - find onboard device by type/name
989  *	@type: device type or %DMI_DEV_TYPE_ANY to match all device types
990  *	@name: device name string or %NULL to match all
991  *	@from: previous device found in search, or %NULL for new search.
992  *
993  *	Iterates through the list of known onboard devices. If a device is
994  *	found with a matching @type and @name, a pointer to its device
995  *	structure is returned.  Otherwise, %NULL is returned.
996  *	A new search is initiated by passing %NULL as the @from argument.
997  *	If @from is not %NULL, searches continue from next device.
998  */
dmi_find_device(int type,const char * name,const struct dmi_device * from)999 const struct dmi_device *dmi_find_device(int type, const char *name,
1000 				    const struct dmi_device *from)
1001 {
1002 	const struct list_head *head = from ? &from->list : &dmi_devices;
1003 	struct list_head *d;
1004 
1005 	for (d = head->next; d != &dmi_devices; d = d->next) {
1006 		const struct dmi_device *dev =
1007 			list_entry(d, struct dmi_device, list);
1008 
1009 		if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
1010 		    ((name == NULL) || (strcmp(dev->name, name) == 0)))
1011 			return dev;
1012 	}
1013 
1014 	return NULL;
1015 }
1016 EXPORT_SYMBOL(dmi_find_device);
1017 
1018 /**
1019  *	dmi_get_date - parse a DMI date
1020  *	@field:	data index (see enum dmi_field)
1021  *	@yearp: optional out parameter for the year
1022  *	@monthp: optional out parameter for the month
1023  *	@dayp: optional out parameter for the day
1024  *
1025  *	The date field is assumed to be in the form resembling
1026  *	[mm[/dd]]/yy[yy] and the result is stored in the out
1027  *	parameters any or all of which can be omitted.
1028  *
1029  *	If the field doesn't exist, all out parameters are set to zero
1030  *	and false is returned.  Otherwise, true is returned with any
1031  *	invalid part of date set to zero.
1032  *
1033  *	On return, year, month and day are guaranteed to be in the
1034  *	range of [0,9999], [0,12] and [0,31] respectively.
1035  */
dmi_get_date(int field,int * yearp,int * monthp,int * dayp)1036 bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
1037 {
1038 	int year = 0, month = 0, day = 0;
1039 	bool exists;
1040 	const char *s, *y;
1041 	char *e;
1042 
1043 	s = dmi_get_system_info(field);
1044 	exists = s;
1045 	if (!exists)
1046 		goto out;
1047 
1048 	/*
1049 	 * Determine year first.  We assume the date string resembles
1050 	 * mm/dd/yy[yy] but the original code extracted only the year
1051 	 * from the end.  Keep the behavior in the spirit of no
1052 	 * surprises.
1053 	 */
1054 	y = strrchr(s, '/');
1055 	if (!y)
1056 		goto out;
1057 
1058 	y++;
1059 	year = simple_strtoul(y, &e, 10);
1060 	if (y != e && year < 100) {	/* 2-digit year */
1061 		year += 1900;
1062 		if (year < 1996)	/* no dates < spec 1.0 */
1063 			year += 100;
1064 	}
1065 	if (year > 9999)		/* year should fit in %04d */
1066 		year = 0;
1067 
1068 	/* parse the mm and dd */
1069 	month = simple_strtoul(s, &e, 10);
1070 	if (s == e || *e != '/' || !month || month > 12) {
1071 		month = 0;
1072 		goto out;
1073 	}
1074 
1075 	s = e + 1;
1076 	day = simple_strtoul(s, &e, 10);
1077 	if (s == y || s == e || *e != '/' || day > 31)
1078 		day = 0;
1079 out:
1080 	if (yearp)
1081 		*yearp = year;
1082 	if (monthp)
1083 		*monthp = month;
1084 	if (dayp)
1085 		*dayp = day;
1086 	return exists;
1087 }
1088 EXPORT_SYMBOL(dmi_get_date);
1089 
1090 /**
1091  *	dmi_get_bios_year - get a year out of DMI_BIOS_DATE field
1092  *
1093  *	Returns year on success, -ENXIO if DMI is not selected,
1094  *	or a different negative error code if DMI field is not present
1095  *	or not parseable.
1096  */
dmi_get_bios_year(void)1097 int dmi_get_bios_year(void)
1098 {
1099 	bool exists;
1100 	int year;
1101 
1102 	exists = dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
1103 	if (!exists)
1104 		return -ENODATA;
1105 
1106 	return year ? year : -ERANGE;
1107 }
1108 EXPORT_SYMBOL(dmi_get_bios_year);
1109 
1110 /**
1111  *	dmi_walk - Walk the DMI table and get called back for every record
1112  *	@decode: Callback function
1113  *	@private_data: Private data to be passed to the callback function
1114  *
1115  *	Returns 0 on success, -ENXIO if DMI is not selected or not present,
1116  *	or a different negative error code if DMI walking fails.
1117  */
dmi_walk(void (* decode)(const struct dmi_header *,void *),void * private_data)1118 int dmi_walk(void (*decode)(const struct dmi_header *, void *),
1119 	     void *private_data)
1120 {
1121 	u8 *buf;
1122 
1123 	if (!dmi_available)
1124 		return -ENXIO;
1125 
1126 	buf = dmi_remap(dmi_base, dmi_len);
1127 	if (buf == NULL)
1128 		return -ENOMEM;
1129 
1130 	dmi_decode_table(buf, decode, private_data);
1131 
1132 	dmi_unmap(buf);
1133 	return 0;
1134 }
1135 EXPORT_SYMBOL_GPL(dmi_walk);
1136 
1137 /**
1138  * dmi_match - compare a string to the dmi field (if exists)
1139  * @f: DMI field identifier
1140  * @str: string to compare the DMI field to
1141  *
1142  * Returns true if the requested field equals to the str (including NULL).
1143  */
dmi_match(enum dmi_field f,const char * str)1144 bool dmi_match(enum dmi_field f, const char *str)
1145 {
1146 	const char *info = dmi_get_system_info(f);
1147 
1148 	if (info == NULL || str == NULL)
1149 		return info == str;
1150 
1151 	return !strcmp(info, str);
1152 }
1153 EXPORT_SYMBOL_GPL(dmi_match);
1154 
dmi_memdev_name(u16 handle,const char ** bank,const char ** device)1155 void dmi_memdev_name(u16 handle, const char **bank, const char **device)
1156 {
1157 	int n;
1158 
1159 	if (dmi_memdev == NULL)
1160 		return;
1161 
1162 	for (n = 0; n < dmi_memdev_nr; n++) {
1163 		if (handle == dmi_memdev[n].handle) {
1164 			*bank = dmi_memdev[n].bank;
1165 			*device = dmi_memdev[n].device;
1166 			break;
1167 		}
1168 	}
1169 }
1170 EXPORT_SYMBOL_GPL(dmi_memdev_name);
1171 
dmi_memdev_size(u16 handle)1172 u64 dmi_memdev_size(u16 handle)
1173 {
1174 	int n;
1175 
1176 	if (dmi_memdev) {
1177 		for (n = 0; n < dmi_memdev_nr; n++) {
1178 			if (handle == dmi_memdev[n].handle)
1179 				return dmi_memdev[n].size;
1180 		}
1181 	}
1182 	return ~0ull;
1183 }
1184 EXPORT_SYMBOL_GPL(dmi_memdev_size);
1185 
1186 /**
1187  * dmi_memdev_type - get the memory type
1188  * @handle: DMI structure handle
1189  *
1190  * Return the DMI memory type of the module in the slot associated with the
1191  * given DMI handle, or 0x0 if no such DMI handle exists.
1192  */
dmi_memdev_type(u16 handle)1193 u8 dmi_memdev_type(u16 handle)
1194 {
1195 	int n;
1196 
1197 	if (dmi_memdev) {
1198 		for (n = 0; n < dmi_memdev_nr; n++) {
1199 			if (handle == dmi_memdev[n].handle)
1200 				return dmi_memdev[n].type;
1201 		}
1202 	}
1203 	return 0x0;	/* Not a valid value */
1204 }
1205 EXPORT_SYMBOL_GPL(dmi_memdev_type);
1206 
1207 /**
1208  *	dmi_memdev_handle - get the DMI handle of a memory slot
1209  *	@slot: slot number
1210  *
1211  *	Return the DMI handle associated with a given memory slot, or %0xFFFF
1212  *      if there is no such slot.
1213  */
dmi_memdev_handle(int slot)1214 u16 dmi_memdev_handle(int slot)
1215 {
1216 	if (dmi_memdev && slot >= 0 && slot < dmi_memdev_nr)
1217 		return dmi_memdev[slot].handle;
1218 
1219 	return 0xffff;	/* Not a valid value */
1220 }
1221 EXPORT_SYMBOL_GPL(dmi_memdev_handle);
1222