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