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