xref: /illumos-gate/usr/src/boot/libsa/smbios.c (revision 438283cf397cce47d80cc67b04bbfdfe73b0d142)
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 
29 #include <stand.h>
30 #include <sys/endian.h>
31 
32 #include "smbios.h"
33 
34 /*
35  * Detect SMBIOS and export information about the SMBIOS into the
36  * environment.
37  *
38  * System Management BIOS Reference Specification, v2.6 Final
39  * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
40  * System Management BIOS (SMBIOS) Reference Specification, v3.1.0
41  * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.0.pdf
42  */
43 
44 /*
45  * 2.1.1 SMBIOS Structure Table Entry Point
46  *
47  * "On non-EFI systems, the SMBIOS Entry Point structure, described below, can
48  * be located by application software by searching for the anchor-string on
49  * paragraph (16-byte) boundaries within the physical memory address range
50  * 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor
51  * string that is used by some existing DMI browsers."
52  */
53 #define	SMBIOS_START		0xf0000
54 #define	SMBIOS_LENGTH		0x10000
55 #define	SMBIOS_STEP		0x10
56 #define	SMBIOS_SIG		"_SM_"
57 #define	SMBIOS_SIG_LEN		(4)
58 #define	SMBIOS3_SIG		"_SM3_"
59 #define	SMBIOS3_SIG_LEN		(5)
60 #define	SMBIOS_DMI_SIG		"_DMI_"
61 #define	SMBIOS_DMI_SIG_LEN	(5)
62 
63 #define	SMBIOS_GET8(base, off)	(*(uint8_t *)((base) + (off)))
64 #define	SMBIOS_GET16(base, off)	(*(uint16_t *)((base) + (off)))
65 #define	SMBIOS_GET32(base, off)	(*(uint32_t *)((base) + (off)))
66 #define	SMBIOS_GET64(base, off)	(*(uint64_t *)((base) + (off)))
67 
68 #define	SMBIOS_GETLEN(base)	SMBIOS_GET8(base, 0x01)
69 #define	SMBIOS_GETSTR(base)	((base) + SMBIOS_GETLEN(base))
70 
71 struct smbios_attr {
72 	int		probed;
73 	caddr_t		addr;
74 	size_t		length;
75 	size_t		count;
76 	int		major;
77 	int		minor;
78 	int		ver;
79 	const char	*bios_vendor;
80 	const char	*maker;
81 	const char	*product;
82 	uint32_t	enabled_memory;
83 	uint32_t	old_enabled_memory;
84 	uint8_t		enabled_sockets;
85 	uint8_t		populated_sockets;
86 };
87 
88 static struct smbios_attr smbios;
89 
90 static uint8_t
91 smbios_checksum(const caddr_t addr, const uint8_t len)
92 {
93 	uint8_t		sum;
94 	int		i;
95 
96 	for (sum = 0, i = 0; i < len; i++)
97 		sum += SMBIOS_GET8(addr, i);
98 	return (sum);
99 }
100 
101 static caddr_t
102 smbios_sigsearch(const caddr_t addr, const uint32_t len)
103 {
104 	caddr_t		cp;
105 	uintptr_t	paddr;
106 
107 	/* Search on 16-byte boundaries. */
108 	for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) {
109 		if (strncmp(cp, SMBIOS_SIG, SMBIOS_SIG_LEN) == 0 &&
110 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
111 		    strncmp(cp + 0x10, SMBIOS_DMI_SIG,
112 		    SMBIOS_DMI_SIG_LEN) == 0 &&
113 		    smbios_checksum(cp + 0x10, 0x0f) == 0) {
114 
115 			/* Structure Table Length */
116 			smbios.length = SMBIOS_GET16(cp, 0x16);
117 			/* Structure Table Address */
118 			paddr = SMBIOS_GET32(cp, 0x18);
119 			/* No of SMBIOS Structures */
120 			smbios.count = SMBIOS_GET16(cp, 0x1c);
121 			/* SMBIOS BCD Revision */
122 			smbios.ver = SMBIOS_GET8(cp, 0x1e);
123 			if (smbios.ver != 0) {
124 				smbios.major = smbios.ver >> 4;
125 				smbios.minor = smbios.ver & 0x0f;
126 				if (smbios.major > 9 || smbios.minor > 9)
127 					smbios.ver = 0;
128 			}
129 			if (smbios.ver == 0) {
130 				/* SMBIOS Major Version */
131 				smbios.major = SMBIOS_GET8(cp, 0x06);
132 				/* SMBIOS Minor Version */
133 				smbios.minor = SMBIOS_GET8(cp, 0x07);
134 			}
135 			smbios.ver = (smbios.major << 8) | smbios.minor;
136 			smbios.addr = ptov(paddr);
137 			return (cp);
138 		}
139 #ifdef _LP64
140 		/*
141 		 * Check for the SMBIOS 64-bit entry point introduced in
142 		 * version 3.0.
143 		 *
144 		 * The table address is a 64-bit physical address that may
145 		 * appear at any 64-bit address. We only search for
146 		 * the 64-bit entry point when running a 64-bit application.
147 		 */
148 		if (strncmp(cp, SMBIOS3_SIG, SMBIOS3_SIG_LEN) == 0 &&
149 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x06)) == 0) {
150 
151 			/* SMBIOS Major Version */
152 			smbios.major = SMBIOS_GET8(cp, 0x07);
153 			/* SMBIOS Minor Version */
154 			smbios.minor = SMBIOS_GET8(cp, 0x08);
155 			/* Entry Point Revision */
156 			smbios.ver = SMBIOS_GET8(cp, 0x0a);
157 			/* Structure Table maximum size */
158 			smbios.length = SMBIOS_GET32(cp, 0x0c);
159 			/* Structure Table Address */
160 			paddr = SMBIOS_GET64(cp, 0x10);
161 			smbios.addr = ptov(paddr);
162 			/*
163 			 * Calculate upper limit for structure count,
164 			 * use size of table header (4 bytes).
165 			 */
166 			smbios.count = smbios.length / 4;
167 			return (cp);
168 		}
169 #endif
170 	}
171 	return (NULL);
172 }
173 
174 static const char *
175 smbios_getstring(caddr_t addr, const int offset)
176 {
177 	caddr_t		cp;
178 	int		i, idx;
179 
180 	idx = SMBIOS_GET8(addr, offset);
181 	if (idx != 0) {
182 		cp = SMBIOS_GETSTR(addr);
183 		for (i = 1; i < idx; i++)
184 			cp += strlen(cp) + 1;
185 		return (cp);
186 	}
187 	return (NULL);
188 }
189 
190 static void
191 smbios_setenv(const char *name, caddr_t addr, const int offset)
192 {
193 	const char	*val;
194 
195 	val = smbios_getstring(addr, offset);
196 	if (val != NULL)
197 		setenv(name, val, 1);
198 }
199 
200 #ifdef SMBIOS_SERIAL_NUMBERS
201 
202 #define	UUID_SIZE		16
203 #define	UUID_TYPE		uint32_t
204 #define	UUID_STEP		sizeof (UUID_TYPE)
205 #define	UUID_ALL_BITS		(UUID_SIZE / UUID_STEP)
206 #define	UUID_GET(base, off)	(*(UUID_TYPE *)((base) + (off)))
207 
208 static void
209 smbios_setuuid(const char *name, const caddr_t addr)
210 {
211 	char		uuid[37];
212 	int		byteorder, i, ones, zeros;
213 	UUID_TYPE	n;
214 	uint32_t	f1;
215 	uint16_t	f2, f3;
216 
217 	for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
218 		n = UUID_GET(addr, i) + 1;
219 		if (zeros == 0 && n == 0)
220 			ones++;
221 		else if (ones == 0 && n == 1)
222 			zeros++;
223 		else
224 			break;
225 	}
226 
227 	if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
228 		/*
229 		 * 3.3.2.1 System UUID
230 		 *
231 		 * "Although RFC 4122 recommends network byte order for all
232 		 * fields, the PC industry (including the ACPI, UEFI, and
233 		 * Microsoft specifications) has consistently used
234 		 * little-endian byte encoding for the first three fields:
235 		 * time_low, time_mid, time_hi_and_version. The same encoding,
236 		 * also known as wire format, should also be used for the
237 		 * SMBIOS representation of the UUID."
238 		 *
239 		 * Note: We use network byte order for backward compatibility
240 		 * unless SMBIOS version is 2.6+ or little-endian is forced.
241 		 */
242 #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
243 		byteorder = LITTLE_ENDIAN;
244 #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
245 		byteorder = BIG_ENDIAN;
246 #else
247 		byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
248 #endif
249 		if (byteorder != LITTLE_ENDIAN) {
250 			f1 = ntohl(SMBIOS_GET32(addr, 0));
251 			f2 = ntohs(SMBIOS_GET16(addr, 4));
252 			f3 = ntohs(SMBIOS_GET16(addr, 6));
253 		} else {
254 			f1 = le32toh(SMBIOS_GET32(addr, 0));
255 			f2 = le16toh(SMBIOS_GET16(addr, 4));
256 			f3 = le16toh(SMBIOS_GET16(addr, 6));
257 		}
258 		sprintf(uuid,
259 		    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
260 		    f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
261 		    SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
262 		    SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
263 		    SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
264 		setenv(name, uuid, 1);
265 	}
266 }
267 
268 #undef UUID_SIZE
269 #undef UUID_TYPE
270 #undef UUID_STEP
271 #undef UUID_ALL_BITS
272 #undef UUID_GET
273 
274 #endif
275 
276 static caddr_t
277 smbios_parse_table(const caddr_t addr)
278 {
279 	caddr_t		cp;
280 	int		proc, size, osize, type;
281 
282 	type = SMBIOS_GET8(addr, 0);	/* 3.1.2 Structure Header Format */
283 	switch (type) {
284 	case 0:		/* 3.3.1 BIOS Information (Type 0) */
285 		smbios_setenv("smbios.bios.vendor", addr, 0x04);
286 		smbios_setenv("smbios.bios.version", addr, 0x05);
287 		smbios_setenv("smbios.bios.reldate", addr, 0x08);
288 		break;
289 
290 	case 1:		/* 3.3.2 System Information (Type 1) */
291 		smbios_setenv("smbios.system.maker", addr, 0x04);
292 		smbios_setenv("smbios.system.product", addr, 0x05);
293 		smbios_setenv("smbios.system.version", addr, 0x06);
294 #ifdef SMBIOS_SERIAL_NUMBERS
295 		smbios_setenv("smbios.system.serial", addr, 0x07);
296 		smbios_setuuid("smbios.system.uuid", addr + 0x08);
297 #endif
298 		if (smbios.major > 2 ||
299 		    (smbios.major == 2 && smbios.minor >= 4)) {
300 			smbios_setenv("smbios.system.sku", addr, 0x19);
301 			smbios_setenv("smbios.system.family", addr, 0x1a);
302 		}
303 		break;
304 
305 	case 2:		/* 3.3.3 Base Board (or Module) Information (Type 2) */
306 		smbios_setenv("smbios.planar.maker", addr, 0x04);
307 		smbios_setenv("smbios.planar.product", addr, 0x05);
308 		smbios_setenv("smbios.planar.version", addr, 0x06);
309 #ifdef SMBIOS_SERIAL_NUMBERS
310 		smbios_setenv("smbios.planar.serial", addr, 0x07);
311 		smbios_setenv("smbios.planar.tag", addr, 0x08);
312 #endif
313 		smbios_setenv("smbios.planar.location", addr, 0x0a);
314 		break;
315 
316 	case 3:		/* 3.3.4 System Enclosure or Chassis (Type 3) */
317 		smbios_setenv("smbios.chassis.maker", addr, 0x04);
318 		smbios_setenv("smbios.chassis.version", addr, 0x06);
319 #ifdef SMBIOS_SERIAL_NUMBERS
320 		smbios_setenv("smbios.chassis.serial", addr, 0x07);
321 		smbios_setenv("smbios.chassis.tag", addr, 0x08);
322 #endif
323 		break;
324 
325 	case 4:		/* 3.3.5 Processor Information (Type 4) */
326 		/*
327 		 * Offset 18h: Processor Status
328 		 *
329 		 * Bit 7	Reserved, must be 0
330 		 * Bit 6	CPU Socket Populated
331 		 *		1 - CPU Socket Populated
332 		 *		0 - CPU Socket Unpopulated
333 		 * Bit 5:3	Reserved, must be zero
334 		 * Bit 2:0	CPU Status
335 		 *		0h - Unknown
336 		 *		1h - CPU Enabled
337 		 *		2h - CPU Disabled by User via BIOS Setup
338 		 *		3h - CPU Disabled by BIOS (POST Error)
339 		 *		4h - CPU is Idle, waiting to be enabled
340 		 *		5-6h - Reserved
341 		 *		7h - Other
342 		 */
343 		proc = SMBIOS_GET8(addr, 0x18);
344 		if ((proc & 0x07) == 1)
345 			smbios.enabled_sockets++;
346 		if ((proc & 0x40) != 0)
347 			smbios.populated_sockets++;
348 		break;
349 
350 	case 6:		/* 3.3.7 Memory Module Information (Type 6, Obsolete) */
351 		/*
352 		 * Offset 0Ah: Enabled Size
353 		 *
354 		 * Bit 7	Bank connection
355 		 *		1 - Double-bank connection
356 		 *		0 - Single-bank connection
357 		 * Bit 6:0	Size (n), where 2**n is the size in MB
358 		 *		7Dh - Not determinable (Installed Size only)
359 		 *		7Eh - Module is installed, but no memory
360 		 *		has been enabled
361 		 *		7Fh - Not installed
362 		 */
363 		osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
364 		if (osize > 0 && osize < 22)
365 			smbios.old_enabled_memory += 1 << (osize + 10);
366 		break;
367 
368 	case 17:	/* 3.3.18 Memory Device (Type 17) */
369 		/*
370 		 * Offset 0Ch: Size
371 		 *
372 		 * Bit 15	Granularity
373 		 *		1 - Value is in kilobytes units
374 		 *		0 - Value is in megabytes units
375 		 * Bit 14:0	Size
376 		 */
377 		size = SMBIOS_GET16(addr, 0x0c);
378 		if (size != 0 && size != 0xffff)
379 			smbios.enabled_memory += (size & 0x8000) != 0 ?
380 			    (size & 0x7fff) : (size << 10);
381 		break;
382 
383 	default:	/* skip other types */
384 		break;
385 	}
386 
387 	/* Find structure terminator. */
388 	cp = SMBIOS_GETSTR(addr);
389 	while (SMBIOS_GET16(cp, 0) != 0)
390 		cp++;
391 
392 	return (cp + 2);
393 }
394 
395 static caddr_t
396 smbios_find_struct(int type)
397 {
398 	caddr_t		dmi;
399 	size_t		i;
400 
401 	if (smbios.addr == NULL)
402 		return (NULL);
403 
404 	for (dmi = smbios.addr, i = 0;
405 	    dmi < smbios.addr + smbios.length && i < smbios.count; i++) {
406 		if (SMBIOS_GET8(dmi, 0) == type)
407 			return (dmi);
408 		/* Find structure terminator. */
409 		dmi = SMBIOS_GETSTR(dmi);
410 		while (SMBIOS_GET16(dmi, 0) != 0)
411 			dmi++;
412 		dmi += 2;
413 	}
414 
415 	return (NULL);
416 }
417 
418 static caddr_t
419 smbios_probe(const caddr_t addr)
420 {
421 	static caddr_t smbios_addr = NULL;
422 	caddr_t		info;
423 	const caddr_t	paddr = addr != NULL ? addr : ptov(SMBIOS_START);
424 
425 	if (smbios.probed)
426 		return (smbios_addr);
427 	smbios.probed = 1;
428 
429 	/* Search signatures and validate checksums. */
430 	smbios_addr = smbios_sigsearch(paddr, SMBIOS_LENGTH);
431 	if (smbios.addr != NULL) {
432 		/* Get system information from SMBIOS */
433 		info = smbios_find_struct(0x00);
434 		if (info != NULL) {
435 			smbios.bios_vendor = smbios_getstring(info, 0x04);
436 		}
437 		info = smbios_find_struct(0x01);
438 		if (info != NULL) {
439 			smbios.maker = smbios_getstring(info, 0x04);
440 			smbios.product = smbios_getstring(info, 0x05);
441 		}
442 	}
443 	return (smbios_addr);
444 }
445 
446 void
447 smbios_detect(const caddr_t addr)
448 {
449 	caddr_t		smbios_addr;
450 	char		buf[20];
451 	caddr_t		dmi;
452 	size_t		i;
453 
454 	smbios_addr = smbios_probe(addr);
455 	if (smbios_addr == NULL)
456 		return;
457 
458 	if (addr == 0) {
459 		snprintf(buf, sizeof (buf), "0x%08jx",
460 		    (uintmax_t)vtop(smbios_addr));
461 	} else {
462 		snprintf(buf, sizeof (buf), "0x%016jx",
463 		    (uintmax_t)vtop(smbios_addr));
464 	}
465 	env_setenv("smbios-address", EV_VOLATILE, buf, env_noset, env_nounset);
466 
467 	for (dmi = smbios.addr, i = 0;
468 	    dmi < smbios.addr + smbios.length && i < smbios.count; i++)
469 		dmi = smbios_parse_table(dmi);
470 
471 	sprintf(buf, "%d.%d", smbios.major, smbios.minor);
472 	setenv("smbios.version", buf, 1);
473 	if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
474 		sprintf(buf, "%u", smbios.enabled_memory > 0 ?
475 		    smbios.enabled_memory : smbios.old_enabled_memory);
476 		setenv("smbios.memory.enabled", buf, 1);
477 	}
478 	if (smbios.enabled_sockets > 0) {
479 		sprintf(buf, "%u", smbios.enabled_sockets);
480 		setenv("smbios.socket.enabled", buf, 1);
481 	}
482 	if (smbios.populated_sockets > 0) {
483 		sprintf(buf, "%u", smbios.populated_sockets);
484 		setenv("smbios.socket.populated", buf, 1);
485 	}
486 }
487 
488 static int
489 smbios_match_str(const char *s1, const char *s2)
490 {
491 	return (s1 == NULL || (s2 != NULL && strcmp(s1, s2) == 0));
492 }
493 
494 int
495 smbios_match(const char *bios_vendor, const char *maker,
496     const char *product)
497 {
498 	/* XXXRP currently, only called from non-EFI. */
499 	smbios_probe(NULL);
500 	return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
501 	    smbios_match_str(maker, smbios.maker) &&
502 	    smbios_match_str(product, smbios.product));
503 }
504