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