xref: /freebsd/stand/libsa/smbios.c (revision 10ff414c)
1 /*-
2  * Copyright (c) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *	notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *	notice, this list of conditions and the following disclaimer in the
12  *	documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 #include <sys/endian.h>
32 
33 #define PTOV(x)		ptov(x)
34 
35 /*
36  * Detect SMBIOS and export information about the SMBIOS into the
37  * environment.
38  *
39  * System Management BIOS Reference Specification, v2.6 Final
40  * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
41  */
42 
43 /*
44  * 2.1.1 SMBIOS Structure Table Entry Point
45  *
46  * "On non-EFI systems, the SMBIOS Entry Point structure, described below, can
47  * be located by application software by searching for the anchor-string on
48  * paragraph (16-byte) boundaries within the physical memory address range
49  * 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor
50  * string that is used by some existing DMI browsers."
51  */
52 #define	SMBIOS_START		0xf0000
53 #define	SMBIOS_LENGTH		0x10000
54 #define	SMBIOS_STEP		0x10
55 #define	SMBIOS_SIG		"_SM_"
56 #define	SMBIOS_DMI_SIG		"_DMI_"
57 
58 #define	SMBIOS_GET8(base, off)	(*(uint8_t *)((base) + (off)))
59 #define	SMBIOS_GET16(base, off)	(*(uint16_t *)((base) + (off)))
60 #define	SMBIOS_GET32(base, off)	(*(uint32_t *)((base) + (off)))
61 
62 #define	SMBIOS_GETLEN(base)	SMBIOS_GET8(base, 0x01)
63 #define	SMBIOS_GETSTR(base)	((base) + SMBIOS_GETLEN(base))
64 
65 struct smbios_attr {
66 	int		probed;
67 	caddr_t 	addr;
68 	size_t		length;
69 	size_t		count;
70 	int		major;
71 	int		minor;
72 	int		ver;
73 	const char*	bios_vendor;
74 	const char*	maker;
75 	const char*	product;
76 	uint32_t	enabled_memory;
77 	uint32_t	old_enabled_memory;
78 	uint8_t		enabled_sockets;
79 	uint8_t		populated_sockets;
80 };
81 
82 static struct smbios_attr smbios;
83 
84 static uint8_t
85 smbios_checksum(const caddr_t addr, const uint8_t len)
86 {
87 	uint8_t		sum;
88 	int		i;
89 
90 	for (sum = 0, i = 0; i < len; i++)
91 		sum += SMBIOS_GET8(addr, i);
92 	return (sum);
93 }
94 
95 static caddr_t
96 smbios_sigsearch(const caddr_t addr, const uint32_t len)
97 {
98 	caddr_t		cp;
99 
100 	/* Search on 16-byte boundaries. */
101 	for (cp = addr; cp < addr + len; cp += SMBIOS_STEP)
102 		if (strncmp(cp, SMBIOS_SIG, 4) == 0 &&
103 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
104 		    strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&
105 		    smbios_checksum(cp + 0x10, 0x0f) == 0)
106 			return (cp);
107 	return (NULL);
108 }
109 
110 static const char*
111 smbios_getstring(caddr_t addr, const int offset)
112 {
113 	caddr_t		cp;
114 	int		i, idx;
115 
116 	idx = SMBIOS_GET8(addr, offset);
117 	if (idx != 0) {
118 		cp = SMBIOS_GETSTR(addr);
119 		for (i = 1; i < idx; i++)
120 			cp += strlen(cp) + 1;
121 		return cp;
122 	}
123 	return (NULL);
124 }
125 
126 static void
127 smbios_setenv(const char *name, caddr_t addr, const int offset)
128 {
129 	const char*	val;
130 
131 	val = smbios_getstring(addr, offset);
132 	if (val != NULL)
133 		setenv(name, val, 1);
134 }
135 
136 #ifdef SMBIOS_SERIAL_NUMBERS
137 
138 #define	UUID_SIZE		16
139 #define	UUID_TYPE		uint32_t
140 #define	UUID_STEP		sizeof(UUID_TYPE)
141 #define	UUID_ALL_BITS		(UUID_SIZE / UUID_STEP)
142 #define	UUID_GET(base, off)	(*(UUID_TYPE *)((base) + (off)))
143 
144 static void
145 smbios_setuuid(const char *name, const caddr_t addr, const int ver)
146 {
147 	char		uuid[37];
148 	int		byteorder, i, ones, zeros;
149 	UUID_TYPE	n;
150 	uint32_t	f1;
151 	uint16_t	f2, f3;
152 
153 	for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
154 		n = UUID_GET(addr, i) + 1;
155 		if (zeros == 0 && n == 0)
156 			ones++;
157 		else if (ones == 0 && n == 1)
158 			zeros++;
159 		else
160 			break;
161 	}
162 
163 	if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
164 		/*
165 		 * 3.3.2.1 System UUID
166 		 *
167 		 * "Although RFC 4122 recommends network byte order for all
168 		 * fields, the PC industry (including the ACPI, UEFI, and
169 		 * Microsoft specifications) has consistently used
170 		 * little-endian byte encoding for the first three fields:
171 		 * time_low, time_mid, time_hi_and_version. The same encoding,
172 		 * also known as wire format, should also be used for the
173 		 * SMBIOS representation of the UUID."
174 		 *
175 		 * Note: We use network byte order for backward compatibility
176 		 * unless SMBIOS version is 2.6+ or little-endian is forced.
177 		 */
178 #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
179 		byteorder = LITTLE_ENDIAN;
180 #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
181 		byteorder = BIG_ENDIAN;
182 #else
183 		byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
184 #endif
185 		if (byteorder != LITTLE_ENDIAN) {
186 			f1 = ntohl(SMBIOS_GET32(addr, 0));
187 			f2 = ntohs(SMBIOS_GET16(addr, 4));
188 			f3 = ntohs(SMBIOS_GET16(addr, 6));
189 		} else {
190 			f1 = le32toh(SMBIOS_GET32(addr, 0));
191 			f2 = le16toh(SMBIOS_GET16(addr, 4));
192 			f3 = le16toh(SMBIOS_GET16(addr, 6));
193 		}
194 		sprintf(uuid,
195 		    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
196 		    f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
197 		    SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
198 		    SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
199 		    SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
200 		setenv(name, uuid, 1);
201 	}
202 }
203 
204 #undef UUID_SIZE
205 #undef UUID_TYPE
206 #undef UUID_STEP
207 #undef UUID_ALL_BITS
208 #undef UUID_GET
209 
210 #endif
211 
212 static const char *
213 smbios_parse_chassis_type(caddr_t addr)
214 {
215 	int		type;
216 
217 	type = SMBIOS_GET8(addr, 0x5);
218 	switch (type) {
219 	case 0x1:
220 		return ("Other");
221 	case 0x2:
222 		return ("Unknown");
223 	case 0x3:
224 		return ("Desktop");
225 	case 0x4:
226 		return ("Low Profile Desktop");
227 	case 0x5:
228 		return ("Pizza Box");
229 	case 0x6:
230 		return ("Mini Tower");
231 	case 0x7:
232 		return ("Tower");
233 	case 0x8:
234 		return ("Portable");
235 	case 0x9:
236 		return ("Laptop");
237 	case 0xA:
238 		return ("Notebook");
239 	case 0xB:
240 		return ("Hand Held");
241 	case 0xC:
242 		return ("Docking Station");
243 	case 0xD:
244 		return ("All in One");
245 	case 0xE:
246 		return ("Sub Notebook");
247 	case 0xF:
248 		return ("Lunch Box");
249 	case 0x10:
250 		return ("Space-saving");
251 	case 0x11:
252 		return ("Main Server Chassis");
253 	case 0x12:
254 		return ("Expansion Chassis");
255 	case 0x13:
256 		return ("SubChassis");
257 	case 0x14:
258 		return ("Bus Expansion Chassis");
259 	case 0x15:
260 		return ("Peripheral Chassis");
261 	case 0x16:
262 		return ("RAID Chassis");
263 	case 0x17:
264 		return ("Rack Mount Chassis");
265 	case 0x18:
266 		return ("Sealed-case PC");
267 	case 0x19:
268 		return ("Multi-system chassis");
269 	case 0x1A:
270 		return ("Compact PCI");
271 	case 0x1B:
272 		return ("Advanced TCA");
273 	case 0x1C:
274 		return ("Blade");
275 	case 0x1D:
276 		return ("Blade Enclosure");
277 	case 0x1E:
278 		return ("Tablet");
279 	case 0x1F:
280 		return ("Convertible");
281 	case 0x20:
282 		return ("Detachable");
283 	case 0x21:
284 		return ("IoT Gateway");
285 	case 0x22:
286 		return ("Embedded PC");
287 	case 0x23:
288 		return ("Mini PC");
289 	case 0x24:
290 		return ("Stick PC");
291 	}
292 
293 	return ("Undefined");
294 }
295 
296 static caddr_t
297 smbios_parse_table(const caddr_t addr)
298 {
299 	caddr_t		cp;
300 	int		proc, size, osize, type;
301 
302 	type = SMBIOS_GET8(addr, 0);	/* 3.1.2 Structure Header Format */
303 	switch(type) {
304 	case 0:		/* 3.3.1 BIOS Information (Type 0) */
305 		smbios_setenv("smbios.bios.vendor", addr, 0x04);
306 		smbios_setenv("smbios.bios.version", addr, 0x05);
307 		smbios_setenv("smbios.bios.reldate", addr, 0x08);
308 		break;
309 
310 	case 1:		/* 3.3.2 System Information (Type 1) */
311 		smbios_setenv("smbios.system.maker", addr, 0x04);
312 		smbios_setenv("smbios.system.product", addr, 0x05);
313 		smbios_setenv("smbios.system.version", addr, 0x06);
314 #ifdef SMBIOS_SERIAL_NUMBERS
315 		smbios_setenv("smbios.system.serial", addr, 0x07);
316 		smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver);
317 #endif
318 		if (smbios.major > 2 ||
319 		    (smbios.major == 2 && smbios.minor >= 4)) {
320 			smbios_setenv("smbios.system.sku", addr, 0x19);
321 			smbios_setenv("smbios.system.family", addr, 0x1a);
322 		}
323 		break;
324 
325 	case 2:		/* 3.3.3 Base Board (or Module) Information (Type 2) */
326 		smbios_setenv("smbios.planar.maker", addr, 0x04);
327 		smbios_setenv("smbios.planar.product", addr, 0x05);
328 		smbios_setenv("smbios.planar.version", addr, 0x06);
329 #ifdef SMBIOS_SERIAL_NUMBERS
330 		smbios_setenv("smbios.planar.serial", addr, 0x07);
331 		smbios_setenv("smbios.planar.tag", addr, 0x08);
332 #endif
333 		smbios_setenv("smbios.planar.location", addr, 0x0a);
334 		break;
335 
336 	case 3:		/* 3.3.4 System Enclosure or Chassis (Type 3) */
337 		smbios_setenv("smbios.chassis.maker", addr, 0x04);
338 		setenv("smbios.chassis.type", smbios_parse_chassis_type(addr), 1);
339 		smbios_setenv("smbios.chassis.version", addr, 0x06);
340 #ifdef SMBIOS_SERIAL_NUMBERS
341 		smbios_setenv("smbios.chassis.serial", addr, 0x07);
342 		smbios_setenv("smbios.chassis.tag", addr, 0x08);
343 #endif
344 		break;
345 
346 	case 4:		/* 3.3.5 Processor Information (Type 4) */
347 		/*
348 		 * Offset 18h: Processor Status
349 		 *
350 		 * Bit 7	Reserved, must be 0
351 		 * Bit 6	CPU Socket Populated
352 		 *		1 - CPU Socket Populated
353 		 *		0 - CPU Socket Unpopulated
354 		 * Bit 5:3	Reserved, must be zero
355 		 * Bit 2:0	CPU Status
356 		 *		0h - Unknown
357 		 *		1h - CPU Enabled
358 		 *		2h - CPU Disabled by User via BIOS Setup
359 		 *		3h - CPU Disabled by BIOS (POST Error)
360 		 *		4h - CPU is Idle, waiting to be enabled
361 		 *		5-6h - Reserved
362 		 *		7h - Other
363 		 */
364 		proc = SMBIOS_GET8(addr, 0x18);
365 		if ((proc & 0x07) == 1)
366 			smbios.enabled_sockets++;
367 		if ((proc & 0x40) != 0)
368 			smbios.populated_sockets++;
369 		break;
370 
371 	case 6:		/* 3.3.7 Memory Module Information (Type 6, Obsolete) */
372 		/*
373 		 * Offset 0Ah: Enabled Size
374 		 *
375 		 * Bit 7	Bank connection
376 		 *		1 - Double-bank connection
377 		 *		0 - Single-bank connection
378 		 * Bit 6:0	Size (n), where 2**n is the size in MB
379 		 *		7Dh - Not determinable (Installed Size only)
380 		 *		7Eh - Module is installed, but no memory
381 		 *		      has been enabled
382 		 *		7Fh - Not installed
383 		 */
384 		osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
385 		if (osize > 0 && osize < 22)
386 			smbios.old_enabled_memory += 1 << (osize + 10);
387 		break;
388 
389 	case 17:	/* 3.3.18 Memory Device (Type 17) */
390 		/*
391 		 * Offset 0Ch: Size
392 		 *
393 		 * Bit 15	Granularity
394 		 *		1 - Value is in kilobytes units
395 		 *		0 - Value is in megabytes units
396 		 * Bit 14:0	Size
397 		 */
398 		size = SMBIOS_GET16(addr, 0x0c);
399 		if (size != 0 && size != 0xffff)
400 			smbios.enabled_memory += (size & 0x8000) != 0 ?
401 			    (size & 0x7fff) : (size << 10);
402 		break;
403 
404 	default:	/* skip other types */
405 		break;
406 	}
407 
408 	/* Find structure terminator. */
409 	cp = SMBIOS_GETSTR(addr);
410 	while (SMBIOS_GET16(cp, 0) != 0)
411 		cp++;
412 
413 	return (cp + 2);
414 }
415 
416 static caddr_t
417 smbios_find_struct(int type)
418 {
419 	caddr_t		dmi;
420 	size_t		i;
421 
422 	if (smbios.addr == NULL)
423 		return (NULL);
424 
425 	for (dmi = smbios.addr, i = 0;
426 	     dmi < smbios.addr + smbios.length && i < smbios.count; i++) {
427 		if (SMBIOS_GET8(dmi, 0) == type)
428 			return dmi;
429 		/* Find structure terminator. */
430 		dmi = SMBIOS_GETSTR(dmi);
431 		while (SMBIOS_GET16(dmi, 0) != 0)
432 			dmi++;
433 		dmi += 2;
434 	}
435 
436 	return (NULL);
437 }
438 
439 static void
440 smbios_probe(const caddr_t addr)
441 {
442 	caddr_t		saddr, info;
443 	uintptr_t	paddr;
444 
445 	if (smbios.probed)
446 		return;
447 	smbios.probed = 1;
448 
449 	/* Search signatures and validate checksums. */
450 	saddr = smbios_sigsearch(addr ? addr : PTOV(SMBIOS_START),
451 	    SMBIOS_LENGTH);
452 	if (saddr == NULL)
453 		return;
454 
455 	smbios.length = SMBIOS_GET16(saddr, 0x16);	/* Structure Table Length */
456 	paddr = SMBIOS_GET32(saddr, 0x18);		/* Structure Table Address */
457 	smbios.count = SMBIOS_GET16(saddr, 0x1c);	/* No of SMBIOS Structures */
458 	smbios.ver = SMBIOS_GET8(saddr, 0x1e);		/* SMBIOS BCD Revision */
459 
460 	if (smbios.ver != 0) {
461 		smbios.major = smbios.ver >> 4;
462 		smbios.minor = smbios.ver & 0x0f;
463 		if (smbios.major > 9 || smbios.minor > 9)
464 			smbios.ver = 0;
465 	}
466 	if (smbios.ver == 0) {
467 		smbios.major = SMBIOS_GET8(saddr, 0x06);/* SMBIOS Major Version */
468 		smbios.minor = SMBIOS_GET8(saddr, 0x07);/* SMBIOS Minor Version */
469 	}
470 	smbios.ver = (smbios.major << 8) | smbios.minor;
471 	smbios.addr = PTOV(paddr);
472 
473 	/* Get system information from SMBIOS */
474 	info = smbios_find_struct(0x00);
475 	if (info != NULL) {
476 		smbios.bios_vendor = smbios_getstring(info, 0x04);
477 	}
478 	info = smbios_find_struct(0x01);
479 	if (info != NULL) {
480 		smbios.maker = smbios_getstring(info, 0x04);
481 		smbios.product = smbios_getstring(info, 0x05);
482 	}
483 }
484 
485 void
486 smbios_detect(const caddr_t addr)
487 {
488 	char		buf[16];
489 	caddr_t		dmi;
490 	size_t		i;
491 
492 	smbios_probe(addr);
493 	if (smbios.addr == NULL)
494 		return;
495 
496 	for (dmi = smbios.addr, i = 0;
497 	     dmi < smbios.addr + smbios.length && i < smbios.count; i++)
498 		dmi = smbios_parse_table(dmi);
499 
500 	sprintf(buf, "%d.%d", smbios.major, smbios.minor);
501 	setenv("smbios.version", buf, 1);
502 	if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
503 		sprintf(buf, "%u", smbios.enabled_memory > 0 ?
504 		    smbios.enabled_memory : smbios.old_enabled_memory);
505 		setenv("smbios.memory.enabled", buf, 1);
506 	}
507 	if (smbios.enabled_sockets > 0) {
508 		sprintf(buf, "%u", smbios.enabled_sockets);
509 		setenv("smbios.socket.enabled", buf, 1);
510 	}
511 	if (smbios.populated_sockets > 0) {
512 		sprintf(buf, "%u", smbios.populated_sockets);
513 		setenv("smbios.socket.populated", buf, 1);
514 	}
515 }
516 
517 static int
518 smbios_match_str(const char* s1, const char* s2)
519 {
520 	return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2)));
521 }
522 
523 int
524 smbios_match(const char* bios_vendor, const char* maker,
525     const char* product)
526 {
527 	/* XXXRP currently, only called from non-EFI. */
528 	smbios_probe(NULL);
529 	return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
530 	    smbios_match_str(maker, smbios.maker) &&
531 	    smbios_match_str(product, smbios.product));
532 }
533