xref: /freebsd/stand/libsa/smbios.c (revision b5c3ade7657a0ebb0c0f829d90919858a39dba94)
1a64f0b83SWarner Losh /*-
2a64f0b83SWarner Losh  * Copyright (c) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
3a64f0b83SWarner Losh  * All rights reserved.
4a64f0b83SWarner Losh  *
5a64f0b83SWarner Losh  * Redistribution and use in source and binary forms, with or without
6a64f0b83SWarner Losh  * modification, are permitted provided that the following conditions
7a64f0b83SWarner Losh  * are met:
8a64f0b83SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9a64f0b83SWarner Losh  *	notice, this list of conditions and the following disclaimer.
10a64f0b83SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11a64f0b83SWarner Losh  *	notice, this list of conditions and the following disclaimer in the
12a64f0b83SWarner Losh  *	documentation and/or other materials provided with the distribution.
13a64f0b83SWarner Losh  *
14a64f0b83SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a64f0b83SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a64f0b83SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a64f0b83SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a64f0b83SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a64f0b83SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a64f0b83SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a64f0b83SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a64f0b83SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a64f0b83SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a64f0b83SWarner Losh  * SUCH DAMAGE.
25a64f0b83SWarner Losh  */
26a64f0b83SWarner Losh 
27a64f0b83SWarner Losh #include <sys/cdefs.h>
28a64f0b83SWarner Losh __FBSDID("$FreeBSD$");
29a64f0b83SWarner Losh 
30a64f0b83SWarner Losh #include <stand.h>
31a64f0b83SWarner Losh #include <sys/endian.h>
32a64f0b83SWarner Losh 
33a64f0b83SWarner Losh #define PTOV(x)		ptov(x)
34a64f0b83SWarner Losh 
35ee97f198SJohn-Mark Gurney /* Only enable 64-bit entry point if it makes sense */
36ee97f198SJohn-Mark Gurney #if __SIZEOF_POINTER__ > 4
37ee97f198SJohn-Mark Gurney #define	HAS_SMBV3	1
38ee97f198SJohn-Mark Gurney #endif
39ee97f198SJohn-Mark Gurney 
40a64f0b83SWarner Losh /*
41a64f0b83SWarner Losh  * Detect SMBIOS and export information about the SMBIOS into the
42a64f0b83SWarner Losh  * environment.
43a64f0b83SWarner Losh  *
44a64f0b83SWarner Losh  * System Management BIOS Reference Specification, v2.6 Final
45a64f0b83SWarner Losh  * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
46*b5c3ade7SStephen J. Kiernan  *
47*b5c3ade7SStephen J. Kiernan  * System Management BIOS (SMBIOS) Reference Specification, 3.6.0
48*b5c3ade7SStephen J. Kiernan  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.6.0.pdf
49a64f0b83SWarner Losh  */
50a64f0b83SWarner Losh 
51a64f0b83SWarner Losh /*
52*b5c3ade7SStephen J. Kiernan  * The first quoted paragraph below can also be found in section 2.1.1 SMBIOS
53*b5c3ade7SStephen J. Kiernan  * Structure Table Entry Point of System Management BIOS Reference
54*b5c3ade7SStephen J. Kiernan  * Specification, v2.6 Final
55a64f0b83SWarner Losh  *
56*b5c3ade7SStephen J. Kiernan  * (From System Management BIOS (SMBIOS) Reference Specification, 3.6.0)
57*b5c3ade7SStephen J. Kiernan  * 5.2.1 SMBIOS 2.1 (32-bit) Entry Point
58*b5c3ade7SStephen J. Kiernan  *
59*b5c3ade7SStephen J. Kiernan  * "On non-UEFI systems, the 32-bit SMBIOS Entry Point structure, can be
60*b5c3ade7SStephen J. Kiernan  * located by application software by searching for the anchor-string on
61*b5c3ade7SStephen J. Kiernan  * paragraph (16-byte) boundaries within the physical memory address
62*b5c3ade7SStephen J. Kiernan  * range 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate
63*b5c3ade7SStephen J. Kiernan  * anchor string that is used by some existing DMI browsers.
64*b5c3ade7SStephen J. Kiernan  *
65*b5c3ade7SStephen J. Kiernan  * On UEFI-based systems, the SMBIOS Entry Point structure can be located by
66*b5c3ade7SStephen J. Kiernan  * looking in the EFI Configuration Table for the SMBIOS GUID
67*b5c3ade7SStephen J. Kiernan  * (SMBIOS_TABLE_GUID, {EB9D2D31-2D88-11D3-9A16-0090273FC14D}) and using the
68*b5c3ade7SStephen J. Kiernan  * associated pointer. See section 4.6 of the UEFI Specification for details.
69*b5c3ade7SStephen J. Kiernan  * See section 2.3 of the UEFI Specification for how to report the containing
70*b5c3ade7SStephen J. Kiernan  * memory type.
71*b5c3ade7SStephen J. Kiernan  *
72*b5c3ade7SStephen J. Kiernan  * NOTE While the SMBIOS Major and Minor Versions (offsets 06h and 07h)
73*b5c3ade7SStephen J. Kiernan  * currently duplicate the information that is present in the SMBIOS BCD
74*b5c3ade7SStephen J. Kiernan  * Revision (offset 1Eh), they provide a path for future growth in this
75*b5c3ade7SStephen J. Kiernan  * specification. The BCD Revision, for example, provides only a single digit
76*b5c3ade7SStephen J. Kiernan  * for each of the major and minor version numbers."
77*b5c3ade7SStephen J. Kiernan  *
78*b5c3ade7SStephen J. Kiernan  * 5.2.2 SMBIOS 860 3.0 (64-bit) Entry Point
79*b5c3ade7SStephen J. Kiernan  *
80*b5c3ade7SStephen J. Kiernan  * "On non-UEFI systems, the 64-bit SMBIOS Entry Point structure can be located
81*b5c3ade7SStephen J. Kiernan  * by application software by searching for the anchor-string on paragraph
82*b5c3ade7SStephen J. Kiernan  * (16-byte) boundaries within the physical memory address range 000F0000h to
83*b5c3ade7SStephen J. Kiernan  * 000FFFFFh.
84*b5c3ade7SStephen J. Kiernan  *
85*b5c3ade7SStephen J. Kiernan  * On UEFI-based systems, the SMBIOS Entry Point structure can be located by
86*b5c3ade7SStephen J. Kiernan  * looking in the EFI Configuration Table for the SMBIOS 3.x GUID
87*b5c3ade7SStephen J. Kiernan  * (SMBIOS3_TABLE_GUID, {F2FD1544-9794-4A2C-992E-E5BBCF20E394}) and using the
88*b5c3ade7SStephen J. Kiernan  * associated pointer. See section 4.6 of the UEFI Specification for details.
89*b5c3ade7SStephen J. Kiernan  * See section 2.3 of the UEFI Specification for how to report the containing
90*b5c3ade7SStephen J. Kiernan  * memory type."
91a64f0b83SWarner Losh  */
92a64f0b83SWarner Losh #define	SMBIOS_START		0xf0000
93a64f0b83SWarner Losh #define	SMBIOS_LENGTH		0x10000
94a64f0b83SWarner Losh #define	SMBIOS_STEP		0x10
95a64f0b83SWarner Losh #define	SMBIOS_SIG		"_SM_"
96ee97f198SJohn-Mark Gurney #define	SMBIOS3_SIG		"_SM3_"
97a64f0b83SWarner Losh #define	SMBIOS_DMI_SIG		"_DMI_"
98a64f0b83SWarner Losh 
99a64f0b83SWarner Losh #define	SMBIOS_GET8(base, off)	(*(uint8_t *)((base) + (off)))
100a64f0b83SWarner Losh #define	SMBIOS_GET16(base, off)	(*(uint16_t *)((base) + (off)))
101a64f0b83SWarner Losh #define	SMBIOS_GET32(base, off)	(*(uint32_t *)((base) + (off)))
102ee97f198SJohn-Mark Gurney #define	SMBIOS_GET64(base, off)	(*(uint64_t *)((base) + (off)))
103a64f0b83SWarner Losh 
104a64f0b83SWarner Losh #define	SMBIOS_GETLEN(base)	SMBIOS_GET8(base, 0x01)
105a64f0b83SWarner Losh #define	SMBIOS_GETSTR(base)	((base) + SMBIOS_GETLEN(base))
106a64f0b83SWarner Losh 
107a64f0b83SWarner Losh struct smbios_attr {
108a64f0b83SWarner Losh 	int		probed;
109a64f0b83SWarner Losh 	caddr_t 	addr;
110a64f0b83SWarner Losh 	size_t		length;
111a64f0b83SWarner Losh 	size_t		count;
112a64f0b83SWarner Losh 	int		major;
113a64f0b83SWarner Losh 	int		minor;
114a64f0b83SWarner Losh 	int		ver;
115a64f0b83SWarner Losh 	const char*	bios_vendor;
116a64f0b83SWarner Losh 	const char*	maker;
117a64f0b83SWarner Losh 	const char*	product;
118a64f0b83SWarner Losh 	uint32_t	enabled_memory;
119a64f0b83SWarner Losh 	uint32_t	old_enabled_memory;
120a64f0b83SWarner Losh 	uint8_t		enabled_sockets;
121a64f0b83SWarner Losh 	uint8_t		populated_sockets;
122a64f0b83SWarner Losh };
123a64f0b83SWarner Losh 
124a64f0b83SWarner Losh static struct smbios_attr smbios;
125ee97f198SJohn-Mark Gurney #ifdef HAS_SMBV3
126ee97f198SJohn-Mark Gurney static int isv3;
127ee97f198SJohn-Mark Gurney #endif
128a64f0b83SWarner Losh 
129a64f0b83SWarner Losh static uint8_t
130a64f0b83SWarner Losh smbios_checksum(const caddr_t addr, const uint8_t len)
131a64f0b83SWarner Losh {
132a64f0b83SWarner Losh 	uint8_t		sum;
133a64f0b83SWarner Losh 	int		i;
134a64f0b83SWarner Losh 
135a64f0b83SWarner Losh 	for (sum = 0, i = 0; i < len; i++)
136a64f0b83SWarner Losh 		sum += SMBIOS_GET8(addr, i);
137a64f0b83SWarner Losh 	return (sum);
138a64f0b83SWarner Losh }
139a64f0b83SWarner Losh 
140a64f0b83SWarner Losh static caddr_t
141a64f0b83SWarner Losh smbios_sigsearch(const caddr_t addr, const uint32_t len)
142a64f0b83SWarner Losh {
143a64f0b83SWarner Losh 	caddr_t		cp;
144a64f0b83SWarner Losh 
145a64f0b83SWarner Losh 	/* Search on 16-byte boundaries. */
146ee97f198SJohn-Mark Gurney 	for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) {
147ee97f198SJohn-Mark Gurney 		/* v2.1, 32-bit Entry point */
148ee97f198SJohn-Mark Gurney 		if (strncmp(cp, SMBIOS_SIG, sizeof(SMBIOS_SIG) - 1) == 0 &&
149a64f0b83SWarner Losh 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
150a64f0b83SWarner Losh 		    strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&
151a64f0b83SWarner Losh 		    smbios_checksum(cp + 0x10, 0x0f) == 0)
152a64f0b83SWarner Losh 			return (cp);
153ee97f198SJohn-Mark Gurney 
154ee97f198SJohn-Mark Gurney #ifdef HAS_SMBV3
155ee97f198SJohn-Mark Gurney 		/* v3.0, 64-bit Entry point */
156ee97f198SJohn-Mark Gurney 		if (strncmp(cp, SMBIOS3_SIG, sizeof(SMBIOS3_SIG) - 1) == 0 &&
157ee97f198SJohn-Mark Gurney 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x06)) == 0) {
158ee97f198SJohn-Mark Gurney 			isv3 = 1;
159ee97f198SJohn-Mark Gurney 			return (cp);
160ee97f198SJohn-Mark Gurney 		}
161ee97f198SJohn-Mark Gurney #endif
162ee97f198SJohn-Mark Gurney 	}
163a64f0b83SWarner Losh 	return (NULL);
164a64f0b83SWarner Losh }
165a64f0b83SWarner Losh 
166a64f0b83SWarner Losh static const char*
167a64f0b83SWarner Losh smbios_getstring(caddr_t addr, const int offset)
168a64f0b83SWarner Losh {
169a64f0b83SWarner Losh 	caddr_t		cp;
170a64f0b83SWarner Losh 	int		i, idx;
171a64f0b83SWarner Losh 
172a64f0b83SWarner Losh 	idx = SMBIOS_GET8(addr, offset);
173a64f0b83SWarner Losh 	if (idx != 0) {
174a64f0b83SWarner Losh 		cp = SMBIOS_GETSTR(addr);
175a64f0b83SWarner Losh 		for (i = 1; i < idx; i++)
176a64f0b83SWarner Losh 			cp += strlen(cp) + 1;
177a64f0b83SWarner Losh 		return cp;
178a64f0b83SWarner Losh 	}
179a64f0b83SWarner Losh 	return (NULL);
180a64f0b83SWarner Losh }
181a64f0b83SWarner Losh 
182a64f0b83SWarner Losh static void
183a64f0b83SWarner Losh smbios_setenv(const char *name, caddr_t addr, const int offset)
184a64f0b83SWarner Losh {
185a64f0b83SWarner Losh 	const char*	val;
186a64f0b83SWarner Losh 
187a64f0b83SWarner Losh 	val = smbios_getstring(addr, offset);
188a64f0b83SWarner Losh 	if (val != NULL)
189a64f0b83SWarner Losh 		setenv(name, val, 1);
190a64f0b83SWarner Losh }
191a64f0b83SWarner Losh 
192a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
193a64f0b83SWarner Losh 
194a64f0b83SWarner Losh #define	UUID_SIZE		16
195a64f0b83SWarner Losh #define	UUID_TYPE		uint32_t
196a64f0b83SWarner Losh #define	UUID_STEP		sizeof(UUID_TYPE)
197a64f0b83SWarner Losh #define	UUID_ALL_BITS		(UUID_SIZE / UUID_STEP)
198a64f0b83SWarner Losh #define	UUID_GET(base, off)	(*(UUID_TYPE *)((base) + (off)))
199a64f0b83SWarner Losh 
200a64f0b83SWarner Losh static void
201a64f0b83SWarner Losh smbios_setuuid(const char *name, const caddr_t addr, const int ver)
202a64f0b83SWarner Losh {
203a64f0b83SWarner Losh 	char		uuid[37];
204a64f0b83SWarner Losh 	int		byteorder, i, ones, zeros;
205a64f0b83SWarner Losh 	UUID_TYPE	n;
206a64f0b83SWarner Losh 	uint32_t	f1;
207a64f0b83SWarner Losh 	uint16_t	f2, f3;
208a64f0b83SWarner Losh 
209a64f0b83SWarner Losh 	for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
210a64f0b83SWarner Losh 		n = UUID_GET(addr, i) + 1;
211a64f0b83SWarner Losh 		if (zeros == 0 && n == 0)
212a64f0b83SWarner Losh 			ones++;
213a64f0b83SWarner Losh 		else if (ones == 0 && n == 1)
214a64f0b83SWarner Losh 			zeros++;
215a64f0b83SWarner Losh 		else
216a64f0b83SWarner Losh 			break;
217a64f0b83SWarner Losh 	}
218a64f0b83SWarner Losh 
219a64f0b83SWarner Losh 	if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
220a64f0b83SWarner Losh 		/*
221a64f0b83SWarner Losh 		 * 3.3.2.1 System UUID
222a64f0b83SWarner Losh 		 *
223a64f0b83SWarner Losh 		 * "Although RFC 4122 recommends network byte order for all
224a64f0b83SWarner Losh 		 * fields, the PC industry (including the ACPI, UEFI, and
225a64f0b83SWarner Losh 		 * Microsoft specifications) has consistently used
226a64f0b83SWarner Losh 		 * little-endian byte encoding for the first three fields:
227a64f0b83SWarner Losh 		 * time_low, time_mid, time_hi_and_version. The same encoding,
228a64f0b83SWarner Losh 		 * also known as wire format, should also be used for the
229a64f0b83SWarner Losh 		 * SMBIOS representation of the UUID."
230a64f0b83SWarner Losh 		 *
231a64f0b83SWarner Losh 		 * Note: We use network byte order for backward compatibility
232a64f0b83SWarner Losh 		 * unless SMBIOS version is 2.6+ or little-endian is forced.
233a64f0b83SWarner Losh 		 */
234a64f0b83SWarner Losh #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
235a64f0b83SWarner Losh 		byteorder = LITTLE_ENDIAN;
236a64f0b83SWarner Losh #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
237a64f0b83SWarner Losh 		byteorder = BIG_ENDIAN;
238a64f0b83SWarner Losh #else
239a64f0b83SWarner Losh 		byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
240a64f0b83SWarner Losh #endif
241a64f0b83SWarner Losh 		if (byteorder != LITTLE_ENDIAN) {
242a64f0b83SWarner Losh 			f1 = ntohl(SMBIOS_GET32(addr, 0));
243a64f0b83SWarner Losh 			f2 = ntohs(SMBIOS_GET16(addr, 4));
244a64f0b83SWarner Losh 			f3 = ntohs(SMBIOS_GET16(addr, 6));
245a64f0b83SWarner Losh 		} else {
246a64f0b83SWarner Losh 			f1 = le32toh(SMBIOS_GET32(addr, 0));
247a64f0b83SWarner Losh 			f2 = le16toh(SMBIOS_GET16(addr, 4));
248a64f0b83SWarner Losh 			f3 = le16toh(SMBIOS_GET16(addr, 6));
249a64f0b83SWarner Losh 		}
250a64f0b83SWarner Losh 		sprintf(uuid,
251a64f0b83SWarner Losh 		    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
252a64f0b83SWarner Losh 		    f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
253a64f0b83SWarner Losh 		    SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
254a64f0b83SWarner Losh 		    SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
255a64f0b83SWarner Losh 		    SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
256a64f0b83SWarner Losh 		setenv(name, uuid, 1);
257a64f0b83SWarner Losh 	}
258a64f0b83SWarner Losh }
259a64f0b83SWarner Losh 
260a64f0b83SWarner Losh #undef UUID_SIZE
261a64f0b83SWarner Losh #undef UUID_TYPE
262a64f0b83SWarner Losh #undef UUID_STEP
263a64f0b83SWarner Losh #undef UUID_ALL_BITS
264a64f0b83SWarner Losh #undef UUID_GET
265a64f0b83SWarner Losh 
266a64f0b83SWarner Losh #endif
267a64f0b83SWarner Losh 
2689060f2c3SEmmanuel Vadot static const char *
2699060f2c3SEmmanuel Vadot smbios_parse_chassis_type(caddr_t addr)
2709060f2c3SEmmanuel Vadot {
2719060f2c3SEmmanuel Vadot 	int		type;
2729060f2c3SEmmanuel Vadot 
2739060f2c3SEmmanuel Vadot 	type = SMBIOS_GET8(addr, 0x5);
2749060f2c3SEmmanuel Vadot 	switch (type) {
2759060f2c3SEmmanuel Vadot 	case 0x1:
2769060f2c3SEmmanuel Vadot 		return ("Other");
2779060f2c3SEmmanuel Vadot 	case 0x2:
2789060f2c3SEmmanuel Vadot 		return ("Unknown");
2799060f2c3SEmmanuel Vadot 	case 0x3:
2809060f2c3SEmmanuel Vadot 		return ("Desktop");
2819060f2c3SEmmanuel Vadot 	case 0x4:
2829060f2c3SEmmanuel Vadot 		return ("Low Profile Desktop");
2839060f2c3SEmmanuel Vadot 	case 0x5:
2849060f2c3SEmmanuel Vadot 		return ("Pizza Box");
2859060f2c3SEmmanuel Vadot 	case 0x6:
2869060f2c3SEmmanuel Vadot 		return ("Mini Tower");
2879060f2c3SEmmanuel Vadot 	case 0x7:
2889060f2c3SEmmanuel Vadot 		return ("Tower");
2899060f2c3SEmmanuel Vadot 	case 0x8:
2909060f2c3SEmmanuel Vadot 		return ("Portable");
2919060f2c3SEmmanuel Vadot 	case 0x9:
2929060f2c3SEmmanuel Vadot 		return ("Laptop");
2939060f2c3SEmmanuel Vadot 	case 0xA:
2949060f2c3SEmmanuel Vadot 		return ("Notebook");
2959060f2c3SEmmanuel Vadot 	case 0xB:
2969060f2c3SEmmanuel Vadot 		return ("Hand Held");
2979060f2c3SEmmanuel Vadot 	case 0xC:
2989060f2c3SEmmanuel Vadot 		return ("Docking Station");
2999060f2c3SEmmanuel Vadot 	case 0xD:
3009060f2c3SEmmanuel Vadot 		return ("All in One");
3019060f2c3SEmmanuel Vadot 	case 0xE:
3029060f2c3SEmmanuel Vadot 		return ("Sub Notebook");
3039060f2c3SEmmanuel Vadot 	case 0xF:
3049060f2c3SEmmanuel Vadot 		return ("Lunch Box");
3059060f2c3SEmmanuel Vadot 	case 0x10:
3069060f2c3SEmmanuel Vadot 		return ("Space-saving");
3079060f2c3SEmmanuel Vadot 	case 0x11:
3089060f2c3SEmmanuel Vadot 		return ("Main Server Chassis");
3099060f2c3SEmmanuel Vadot 	case 0x12:
3109060f2c3SEmmanuel Vadot 		return ("Expansion Chassis");
3119060f2c3SEmmanuel Vadot 	case 0x13:
3129060f2c3SEmmanuel Vadot 		return ("SubChassis");
3139060f2c3SEmmanuel Vadot 	case 0x14:
3149060f2c3SEmmanuel Vadot 		return ("Bus Expansion Chassis");
3159060f2c3SEmmanuel Vadot 	case 0x15:
3169060f2c3SEmmanuel Vadot 		return ("Peripheral Chassis");
3179060f2c3SEmmanuel Vadot 	case 0x16:
3189060f2c3SEmmanuel Vadot 		return ("RAID Chassis");
3199060f2c3SEmmanuel Vadot 	case 0x17:
3209060f2c3SEmmanuel Vadot 		return ("Rack Mount Chassis");
3219060f2c3SEmmanuel Vadot 	case 0x18:
3229060f2c3SEmmanuel Vadot 		return ("Sealed-case PC");
3239060f2c3SEmmanuel Vadot 	case 0x19:
3249060f2c3SEmmanuel Vadot 		return ("Multi-system chassis");
3259060f2c3SEmmanuel Vadot 	case 0x1A:
3269060f2c3SEmmanuel Vadot 		return ("Compact PCI");
3279060f2c3SEmmanuel Vadot 	case 0x1B:
3289060f2c3SEmmanuel Vadot 		return ("Advanced TCA");
3299060f2c3SEmmanuel Vadot 	case 0x1C:
3309060f2c3SEmmanuel Vadot 		return ("Blade");
3319060f2c3SEmmanuel Vadot 	case 0x1D:
3329060f2c3SEmmanuel Vadot 		return ("Blade Enclosure");
3339060f2c3SEmmanuel Vadot 	case 0x1E:
3349060f2c3SEmmanuel Vadot 		return ("Tablet");
3359060f2c3SEmmanuel Vadot 	case 0x1F:
3369060f2c3SEmmanuel Vadot 		return ("Convertible");
3379060f2c3SEmmanuel Vadot 	case 0x20:
3389060f2c3SEmmanuel Vadot 		return ("Detachable");
3399060f2c3SEmmanuel Vadot 	case 0x21:
3409060f2c3SEmmanuel Vadot 		return ("IoT Gateway");
3419060f2c3SEmmanuel Vadot 	case 0x22:
3429060f2c3SEmmanuel Vadot 		return ("Embedded PC");
3439060f2c3SEmmanuel Vadot 	case 0x23:
3449060f2c3SEmmanuel Vadot 		return ("Mini PC");
3459060f2c3SEmmanuel Vadot 	case 0x24:
3469060f2c3SEmmanuel Vadot 		return ("Stick PC");
3479060f2c3SEmmanuel Vadot 	}
3489060f2c3SEmmanuel Vadot 
3499060f2c3SEmmanuel Vadot 	return ("Undefined");
3509060f2c3SEmmanuel Vadot }
3519060f2c3SEmmanuel Vadot 
352a64f0b83SWarner Losh static caddr_t
353a64f0b83SWarner Losh smbios_parse_table(const caddr_t addr)
354a64f0b83SWarner Losh {
355a64f0b83SWarner Losh 	caddr_t		cp;
356a64f0b83SWarner Losh 	int		proc, size, osize, type;
35766c73af7SKornel Dulęba 	uint8_t		bios_minor, bios_major;
35866c73af7SKornel Dulęba 	char		buf[16];
359a64f0b83SWarner Losh 
360a64f0b83SWarner Losh 	type = SMBIOS_GET8(addr, 0);	/* 3.1.2 Structure Header Format */
361a64f0b83SWarner Losh 	switch(type) {
362a64f0b83SWarner Losh 	case 0:		/* 3.3.1 BIOS Information (Type 0) */
363a64f0b83SWarner Losh 		smbios_setenv("smbios.bios.vendor", addr, 0x04);
364a64f0b83SWarner Losh 		smbios_setenv("smbios.bios.version", addr, 0x05);
365a64f0b83SWarner Losh 		smbios_setenv("smbios.bios.reldate", addr, 0x08);
36666c73af7SKornel Dulęba 		bios_major = SMBIOS_GET8(addr, 0x14);
36766c73af7SKornel Dulęba 		bios_minor = SMBIOS_GET8(addr, 0x15);
36866c73af7SKornel Dulęba 		if (bios_minor != 0xFF && bios_major != 0xFF) {
36966c73af7SKornel Dulęba 			snprintf(buf, sizeof(buf), "%u.%u",
37066c73af7SKornel Dulęba 			    bios_major, bios_minor);
37166c73af7SKornel Dulęba 			setenv("smbios.bios.revision", buf, 1);
37266c73af7SKornel Dulęba 		}
373a64f0b83SWarner Losh 		break;
374a64f0b83SWarner Losh 
375a64f0b83SWarner Losh 	case 1:		/* 3.3.2 System Information (Type 1) */
376a64f0b83SWarner Losh 		smbios_setenv("smbios.system.maker", addr, 0x04);
377a64f0b83SWarner Losh 		smbios_setenv("smbios.system.product", addr, 0x05);
378a64f0b83SWarner Losh 		smbios_setenv("smbios.system.version", addr, 0x06);
379a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
380a64f0b83SWarner Losh 		smbios_setenv("smbios.system.serial", addr, 0x07);
381a64f0b83SWarner Losh 		smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver);
382a64f0b83SWarner Losh #endif
383a64f0b83SWarner Losh 		if (smbios.major > 2 ||
384a64f0b83SWarner Losh 		    (smbios.major == 2 && smbios.minor >= 4)) {
385a64f0b83SWarner Losh 			smbios_setenv("smbios.system.sku", addr, 0x19);
386a64f0b83SWarner Losh 			smbios_setenv("smbios.system.family", addr, 0x1a);
387a64f0b83SWarner Losh 		}
388a64f0b83SWarner Losh 		break;
389a64f0b83SWarner Losh 
390a64f0b83SWarner Losh 	case 2:		/* 3.3.3 Base Board (or Module) Information (Type 2) */
391a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.maker", addr, 0x04);
392a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.product", addr, 0x05);
393a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.version", addr, 0x06);
394a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
395a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.serial", addr, 0x07);
396a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.tag", addr, 0x08);
397a64f0b83SWarner Losh #endif
398a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.location", addr, 0x0a);
399a64f0b83SWarner Losh 		break;
400a64f0b83SWarner Losh 
401a64f0b83SWarner Losh 	case 3:		/* 3.3.4 System Enclosure or Chassis (Type 3) */
402a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.maker", addr, 0x04);
4039060f2c3SEmmanuel Vadot 		setenv("smbios.chassis.type", smbios_parse_chassis_type(addr), 1);
404a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.version", addr, 0x06);
405a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
406a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.serial", addr, 0x07);
407a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.tag", addr, 0x08);
408a64f0b83SWarner Losh #endif
409a64f0b83SWarner Losh 		break;
410a64f0b83SWarner Losh 
411a64f0b83SWarner Losh 	case 4:		/* 3.3.5 Processor Information (Type 4) */
412a64f0b83SWarner Losh 		/*
413a64f0b83SWarner Losh 		 * Offset 18h: Processor Status
414a64f0b83SWarner Losh 		 *
415a64f0b83SWarner Losh 		 * Bit 7	Reserved, must be 0
416a64f0b83SWarner Losh 		 * Bit 6	CPU Socket Populated
417a64f0b83SWarner Losh 		 *		1 - CPU Socket Populated
418a64f0b83SWarner Losh 		 *		0 - CPU Socket Unpopulated
419a64f0b83SWarner Losh 		 * Bit 5:3	Reserved, must be zero
420a64f0b83SWarner Losh 		 * Bit 2:0	CPU Status
421a64f0b83SWarner Losh 		 *		0h - Unknown
422a64f0b83SWarner Losh 		 *		1h - CPU Enabled
423a64f0b83SWarner Losh 		 *		2h - CPU Disabled by User via BIOS Setup
424a64f0b83SWarner Losh 		 *		3h - CPU Disabled by BIOS (POST Error)
425a64f0b83SWarner Losh 		 *		4h - CPU is Idle, waiting to be enabled
426a64f0b83SWarner Losh 		 *		5-6h - Reserved
427a64f0b83SWarner Losh 		 *		7h - Other
428a64f0b83SWarner Losh 		 */
429a64f0b83SWarner Losh 		proc = SMBIOS_GET8(addr, 0x18);
430a64f0b83SWarner Losh 		if ((proc & 0x07) == 1)
431a64f0b83SWarner Losh 			smbios.enabled_sockets++;
432a64f0b83SWarner Losh 		if ((proc & 0x40) != 0)
433a64f0b83SWarner Losh 			smbios.populated_sockets++;
434a64f0b83SWarner Losh 		break;
435a64f0b83SWarner Losh 
436a64f0b83SWarner Losh 	case 6:		/* 3.3.7 Memory Module Information (Type 6, Obsolete) */
437a64f0b83SWarner Losh 		/*
438a64f0b83SWarner Losh 		 * Offset 0Ah: Enabled Size
439a64f0b83SWarner Losh 		 *
440a64f0b83SWarner Losh 		 * Bit 7	Bank connection
441a64f0b83SWarner Losh 		 *		1 - Double-bank connection
442a64f0b83SWarner Losh 		 *		0 - Single-bank connection
443a64f0b83SWarner Losh 		 * Bit 6:0	Size (n), where 2**n is the size in MB
444a64f0b83SWarner Losh 		 *		7Dh - Not determinable (Installed Size only)
445a64f0b83SWarner Losh 		 *		7Eh - Module is installed, but no memory
446a64f0b83SWarner Losh 		 *		      has been enabled
447a64f0b83SWarner Losh 		 *		7Fh - Not installed
448a64f0b83SWarner Losh 		 */
449a64f0b83SWarner Losh 		osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
450a64f0b83SWarner Losh 		if (osize > 0 && osize < 22)
451a64f0b83SWarner Losh 			smbios.old_enabled_memory += 1 << (osize + 10);
452a64f0b83SWarner Losh 		break;
453a64f0b83SWarner Losh 
454a64f0b83SWarner Losh 	case 17:	/* 3.3.18 Memory Device (Type 17) */
455a64f0b83SWarner Losh 		/*
456a64f0b83SWarner Losh 		 * Offset 0Ch: Size
457a64f0b83SWarner Losh 		 *
458a64f0b83SWarner Losh 		 * Bit 15	Granularity
459a64f0b83SWarner Losh 		 *		1 - Value is in kilobytes units
460a64f0b83SWarner Losh 		 *		0 - Value is in megabytes units
461a64f0b83SWarner Losh 		 * Bit 14:0	Size
462a64f0b83SWarner Losh 		 */
463a64f0b83SWarner Losh 		size = SMBIOS_GET16(addr, 0x0c);
464a64f0b83SWarner Losh 		if (size != 0 && size != 0xffff)
465a64f0b83SWarner Losh 			smbios.enabled_memory += (size & 0x8000) != 0 ?
466a64f0b83SWarner Losh 			    (size & 0x7fff) : (size << 10);
467a64f0b83SWarner Losh 		break;
468a64f0b83SWarner Losh 
469a64f0b83SWarner Losh 	default:	/* skip other types */
470a64f0b83SWarner Losh 		break;
471a64f0b83SWarner Losh 	}
472a64f0b83SWarner Losh 
473a64f0b83SWarner Losh 	/* Find structure terminator. */
474a64f0b83SWarner Losh 	cp = SMBIOS_GETSTR(addr);
475a64f0b83SWarner Losh 	while (SMBIOS_GET16(cp, 0) != 0)
476a64f0b83SWarner Losh 		cp++;
477a64f0b83SWarner Losh 
478a64f0b83SWarner Losh 	return (cp + 2);
479a64f0b83SWarner Losh }
480a64f0b83SWarner Losh 
481a64f0b83SWarner Losh static caddr_t
482a64f0b83SWarner Losh smbios_find_struct(int type)
483a64f0b83SWarner Losh {
484a64f0b83SWarner Losh 	caddr_t		dmi;
485a64f0b83SWarner Losh 	size_t		i;
486a64f0b83SWarner Losh 
487a64f0b83SWarner Losh 	if (smbios.addr == NULL)
488a64f0b83SWarner Losh 		return (NULL);
489a64f0b83SWarner Losh 
490a64f0b83SWarner Losh 	for (dmi = smbios.addr, i = 0;
491a64f0b83SWarner Losh 	     dmi < smbios.addr + smbios.length && i < smbios.count; i++) {
492a64f0b83SWarner Losh 		if (SMBIOS_GET8(dmi, 0) == type)
493a64f0b83SWarner Losh 			return dmi;
494a64f0b83SWarner Losh 		/* Find structure terminator. */
495a64f0b83SWarner Losh 		dmi = SMBIOS_GETSTR(dmi);
496a64f0b83SWarner Losh 		while (SMBIOS_GET16(dmi, 0) != 0)
497a64f0b83SWarner Losh 			dmi++;
498a64f0b83SWarner Losh 		dmi += 2;
499a64f0b83SWarner Losh 	}
500a64f0b83SWarner Losh 
501a64f0b83SWarner Losh 	return (NULL);
502a64f0b83SWarner Losh }
503a64f0b83SWarner Losh 
504a64f0b83SWarner Losh static void
505a64f0b83SWarner Losh smbios_probe(const caddr_t addr)
506a64f0b83SWarner Losh {
507a64f0b83SWarner Losh 	caddr_t		saddr, info;
508a64f0b83SWarner Losh 	uintptr_t	paddr;
509ee97f198SJohn-Mark Gurney 	int		maj_off;
510ee97f198SJohn-Mark Gurney 	int		min_off;
511a64f0b83SWarner Losh 
512a64f0b83SWarner Losh 	if (smbios.probed)
513a64f0b83SWarner Losh 		return;
514a64f0b83SWarner Losh 	smbios.probed = 1;
515a64f0b83SWarner Losh 
516a64f0b83SWarner Losh 	/* Search signatures and validate checksums. */
517a64f0b83SWarner Losh 	saddr = smbios_sigsearch(addr ? addr : PTOV(SMBIOS_START),
518a64f0b83SWarner Losh 	    SMBIOS_LENGTH);
519a64f0b83SWarner Losh 	if (saddr == NULL)
520a64f0b83SWarner Losh 		return;
521a64f0b83SWarner Losh 
522ee97f198SJohn-Mark Gurney #ifdef HAS_SMBV3
523ee97f198SJohn-Mark Gurney 	if (isv3) {
52413597be9SJohn-Mark Gurney 		smbios.length = SMBIOS_GET32(saddr, 0x0c);	/* Structure Table Length */
525ee97f198SJohn-Mark Gurney 		paddr = SMBIOS_GET64(saddr, 0x10);		/* Structure Table Address */
526ee97f198SJohn-Mark Gurney 		smbios.count = -1;				/* not present in V3 */
527ee97f198SJohn-Mark Gurney 		smbios.ver = 0;					/* not present in V3 */
528ee97f198SJohn-Mark Gurney 		maj_off = 0x07;
529ee97f198SJohn-Mark Gurney 		min_off = 0x08;
530ee97f198SJohn-Mark Gurney 	} else
531ee97f198SJohn-Mark Gurney #endif
532ee97f198SJohn-Mark Gurney 	{
533a64f0b83SWarner Losh 		smbios.length = SMBIOS_GET16(saddr, 0x16);	/* Structure Table Length */
534a64f0b83SWarner Losh 		paddr = SMBIOS_GET32(saddr, 0x18);		/* Structure Table Address */
535a64f0b83SWarner Losh 		smbios.count = SMBIOS_GET16(saddr, 0x1c);	/* No of SMBIOS Structures */
536a64f0b83SWarner Losh 		smbios.ver = SMBIOS_GET8(saddr, 0x1e);		/* SMBIOS BCD Revision */
537ee97f198SJohn-Mark Gurney 		maj_off = 0x06;
538ee97f198SJohn-Mark Gurney 		min_off = 0x07;
539ee97f198SJohn-Mark Gurney 	}
540ee97f198SJohn-Mark Gurney 
541a64f0b83SWarner Losh 
542a64f0b83SWarner Losh 	if (smbios.ver != 0) {
543a64f0b83SWarner Losh 		smbios.major = smbios.ver >> 4;
544a64f0b83SWarner Losh 		smbios.minor = smbios.ver & 0x0f;
545a64f0b83SWarner Losh 		if (smbios.major > 9 || smbios.minor > 9)
546a64f0b83SWarner Losh 			smbios.ver = 0;
547a64f0b83SWarner Losh 	}
548a64f0b83SWarner Losh 	if (smbios.ver == 0) {
549ee97f198SJohn-Mark Gurney 		smbios.major = SMBIOS_GET8(saddr, maj_off);/* SMBIOS Major Version */
550ee97f198SJohn-Mark Gurney 		smbios.minor = SMBIOS_GET8(saddr, min_off);/* SMBIOS Minor Version */
551a64f0b83SWarner Losh 	}
552a64f0b83SWarner Losh 	smbios.ver = (smbios.major << 8) | smbios.minor;
553a64f0b83SWarner Losh 	smbios.addr = PTOV(paddr);
554a64f0b83SWarner Losh 
555a64f0b83SWarner Losh 	/* Get system information from SMBIOS */
556a64f0b83SWarner Losh 	info = smbios_find_struct(0x00);
557a64f0b83SWarner Losh 	if (info != NULL) {
558a64f0b83SWarner Losh 		smbios.bios_vendor = smbios_getstring(info, 0x04);
559a64f0b83SWarner Losh 	}
560a64f0b83SWarner Losh 	info = smbios_find_struct(0x01);
561a64f0b83SWarner Losh 	if (info != NULL) {
562a64f0b83SWarner Losh 		smbios.maker = smbios_getstring(info, 0x04);
563a64f0b83SWarner Losh 		smbios.product = smbios_getstring(info, 0x05);
564a64f0b83SWarner Losh 	}
565a64f0b83SWarner Losh }
566a64f0b83SWarner Losh 
567a64f0b83SWarner Losh void
568a64f0b83SWarner Losh smbios_detect(const caddr_t addr)
569a64f0b83SWarner Losh {
570a64f0b83SWarner Losh 	char		buf[16];
571a64f0b83SWarner Losh 	caddr_t		dmi;
572a64f0b83SWarner Losh 	size_t		i;
573a64f0b83SWarner Losh 
574a64f0b83SWarner Losh 	smbios_probe(addr);
575a64f0b83SWarner Losh 	if (smbios.addr == NULL)
576a64f0b83SWarner Losh 		return;
577a64f0b83SWarner Losh 
578a64f0b83SWarner Losh 	for (dmi = smbios.addr, i = 0;
579a64f0b83SWarner Losh 	     dmi < smbios.addr + smbios.length && i < smbios.count; i++)
580a64f0b83SWarner Losh 		dmi = smbios_parse_table(dmi);
581a64f0b83SWarner Losh 
582a64f0b83SWarner Losh 	sprintf(buf, "%d.%d", smbios.major, smbios.minor);
583a64f0b83SWarner Losh 	setenv("smbios.version", buf, 1);
584a64f0b83SWarner Losh 	if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
585a64f0b83SWarner Losh 		sprintf(buf, "%u", smbios.enabled_memory > 0 ?
586a64f0b83SWarner Losh 		    smbios.enabled_memory : smbios.old_enabled_memory);
587a64f0b83SWarner Losh 		setenv("smbios.memory.enabled", buf, 1);
588a64f0b83SWarner Losh 	}
589a64f0b83SWarner Losh 	if (smbios.enabled_sockets > 0) {
590a64f0b83SWarner Losh 		sprintf(buf, "%u", smbios.enabled_sockets);
591a64f0b83SWarner Losh 		setenv("smbios.socket.enabled", buf, 1);
592a64f0b83SWarner Losh 	}
593a64f0b83SWarner Losh 	if (smbios.populated_sockets > 0) {
594a64f0b83SWarner Losh 		sprintf(buf, "%u", smbios.populated_sockets);
595a64f0b83SWarner Losh 		setenv("smbios.socket.populated", buf, 1);
596a64f0b83SWarner Losh 	}
597a64f0b83SWarner Losh }
598a64f0b83SWarner Losh 
599a64f0b83SWarner Losh static int
600a64f0b83SWarner Losh smbios_match_str(const char* s1, const char* s2)
601a64f0b83SWarner Losh {
602a64f0b83SWarner Losh 	return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2)));
603a64f0b83SWarner Losh }
604a64f0b83SWarner Losh 
605a64f0b83SWarner Losh int
606a64f0b83SWarner Losh smbios_match(const char* bios_vendor, const char* maker,
607a64f0b83SWarner Losh     const char* product)
608a64f0b83SWarner Losh {
609a64f0b83SWarner Losh 	/* XXXRP currently, only called from non-EFI. */
610a64f0b83SWarner Losh 	smbios_probe(NULL);
611a64f0b83SWarner Losh 	return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
612a64f0b83SWarner Losh 	    smbios_match_str(maker, smbios.maker) &&
613a64f0b83SWarner Losh 	    smbios_match_str(product, smbios.product));
614a64f0b83SWarner Losh }
615